Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to auto-wire HibernateBundle with guice on dropwizard?

Im trying to configure hibernatebundle with guice/dropwizard and need help. Im using hubspot / dropwizard-guice / 0.7.0 3rd party library in addition to dropwizard lib.

The code below obviously wont work and need help on figuring it out. How do I rewrite this so that hibernatebundle and ultimately, session factory, be auto injected to whatever bean that needs it.

MyApplication.java

public class MyApplication extends Application<MyAppConfiguration> {

    private final HibernateBundle<MyAppConfiguration> hibernateBundle = new HibernateBundle<MyAppConfiguration>(MyModel.class) {
        @Override
        public DataSourceFactory getDataSourceFactory(MyAppConfiguration configuration) {
            return configuration.getDataSourceFactory();
        }
    };

    @Override
    public void initialize(Bootstrap<MyAppConfiguration> bootstrap) {
        bootstrap.addBundle(hibernateBundle);  // ???

        bootstrap.addBundle(
            GuiceBundle.<MyAppConfiguration>newBuilder()
                    .addModule(new MyAppModule())
                    .enableAutoConfig(getClass().getPackage().getName())
                    .setConfigClass(MyAppConfiguration.class)
                    .build()
        );
    }

}   

MyAppModule.java

public class MyAppModule extends AbstractModule {

    @Provides
    public SessionFactory provideSessionFactory(MyAppConfiguration configuration) {
            // really wrong as it creates new instance everytime.
        return configuration.getHibernateBundle().getSessionFactory(); // ???
    }

}

MyAppConfiguration.java

public class MyAppConfiguration extends Configuration {
    @Valid
    @NotNull
    private DataSourceFactory database = new DataSourceFactory();

    @JsonProperty("database")
    public DataSourceFactory getDataSourceFactory() {
        return database;
    }

    @JsonProperty("database")
    public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
        this.database = dataSourceFactory;
    }

        // ???
    public HibernateBundle<MyAppConfiguration> getHibernateBundle() {
        return new HibernateBundle<MyAppConfiguration>(MyModel.class) {
            @Override
            public DataSourceFactory getDataSourceFactory(MyAppConfiguration configuration) {
                return database;
            }
        };
    }

}  
like image 343
StephenNYC Avatar asked Apr 27 '14 08:04

StephenNYC


1 Answers

Here is how I end up doing. I never got an answer from here or mailing list so I would consider this hackish and probably not the proper way to do it but it works for me.

In my module (that extends abstractmodule) :

private final HibernateBundle<MyConfiguration> hibernateBundle =
        new HibernateBundle<MyConfiguration>(MyModel.class) {
            @Override
            public DataSourceFactory getDataSourceFactory(MyConfiguration configuration) {
                return configuration.getDataSourceFactory();
            }
        };

@Provides
public SessionFactory provideSessionFactory(MyConfiguration configuration,
                                            Environment environment) {

    SessionFactory sf = hibernateBundle.getSessionFactory();
    if (sf == null) {
        try {
            hibernateBundle.run(configuration, environment);
        } catch (Exception e) {
            logger.error("Unable to run hibernatebundle");
        }
    }

    return hibernateBundle.getSessionFactory();
}

revised:

public SessionFactory provideSessionFactory(MyConfiguration configuration,
                                            Environment environment) {

    SessionFactory sf = hibernateBundle.getSessionFactory();
    if (sf == null) {
        try {
            hibernateBundle.run(configuration, environment);
            return hibernateBundle.getSessionFactory();
        } catch (Exception e) {
            logger.error("Unable to run hibernatebundle");
        }
    } else {
        return sf;
    }
}
like image 126
StephenNYC Avatar answered Oct 19 '22 13:10

StephenNYC