io.dropwizard.jdbi.DBIFactory Java Examples

The following examples show how to use io.dropwizard.jdbi.DBIFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestJdbiDynamicAttributes.java    From soabase with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception
{
    DBIFactory factory = new DBIFactory();
    Environment environment = new Environment("test", new ObjectMapper(), null, new MetricRegistry(), ClassLoader.getSystemClassLoader());
    DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setUrl("jdbc:hsqldb:mem:soa-jdbi;shutdown=true");
    dataSourceFactory.setDriverClass("org.hsqldb.jdbc.JDBCDriver");
    dataSourceFactory.setLogValidationErrors(true);
    dataSourceFactory.setUser("SA");
    dataSourceFactory.setValidationQuery("SELECT * FROM INFORMATION_SCHEMA.SYSTEM_TABLES");
    DBI jdbi = factory.build(environment, dataSourceFactory, "test");
    dynamicAttributes = new JdbiDynamicAttributes(jdbi, Collections.singletonList("test"));

    dynamicAttributes.getDao().createTable();
    dynamicAttributes.start();
}
 
Example #2
Source File: EventStoreFactory.java    From cqrs-eventsourcing-kafka with Apache License 2.0 5 votes vote down vote up
private EventStore buildJdbcEventStore(EventPublisher publisher, Environment environment) {
    ManagedDataSource ds = database.build(environment.metrics(), "eventstore");
    DBI jdbi = new DBIFactory().build(environment, database, "eventstore");
    updateDatabaseSchema(ds);
    EventStore eventStore = new JdbcEventStore(jdbi, environment.getObjectMapper(), publisher);
    return eventStore;
}
 
Example #3
Source File: ReaperApplication.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
private IStorage initializeStorage(ReaperApplicationConfiguration config, Environment environment)
    throws ReaperException {
  IStorage storage;

  if ("memory".equalsIgnoreCase(config.getStorageType())) {
    storage = new MemoryStorage();
  } else if ("cassandra".equalsIgnoreCase(config.getStorageType())) {
    storage = new CassandraStorage(context.reaperInstanceId, config, environment);
  } else if ("postgres".equalsIgnoreCase(config.getStorageType())
      || "h2".equalsIgnoreCase(config.getStorageType())
      || "database".equalsIgnoreCase(config.getStorageType())) {
    // create DBI instance
    final DBIFactory factory = new DBIFactory();
    if (StringUtils.isEmpty(config.getDataSourceFactory().getDriverClass())
        && "postgres".equalsIgnoreCase(config.getStorageType())) {
      config.getDataSourceFactory().setDriverClass("org.postgresql.Driver");
    } else if (StringUtils.isEmpty(config.getDataSourceFactory().getDriverClass())
        && "h2".equalsIgnoreCase(config.getStorageType())) {
      config.getDataSourceFactory().setDriverClass("org.h2.Driver");
    }
    // instantiate store
    storage = new PostgresStorage(
        context.reaperInstanceId,
        factory.build(environment, config.getDataSourceFactory(), "postgresql")
    );
    initDatabase(config);
  } else {
    LOG.error("invalid storageType: {}", config.getStorageType());
    throw new ReaperException("invalid storage type: " + config.getStorageType());
  }
  Preconditions.checkState(storage.isStorageConnected(), "Failed to connect storage");
  return storage;
}
 
Example #4
Source File: DBIProvider.java    From monasca-persister with Apache License 2.0 5 votes vote down vote up
@Override
public DBI get() {
  try {
    return new DBIFactory().build(environment, configuration.getDataSourceFactory(), "vertica");
  } catch (ClassNotFoundException e) {
    throw new ProvisionException("Failed to provision DBI", e);
  }
}
 
Example #5
Source File: H2Test.java    From breakerbox with Apache License 2.0 5 votes vote down vote up
@Before
public void setupH2Test() throws Exception {
    liquibase = new CloseableLiquibaseWithClassPathMigrationsFile(hsqlConfig
            .getDataSourceFactory()
            .build(new MetricRegistry(), "liquibase"), JdbiStore.MIGRATIONS_FILENAME);
    liquibase.update("");
    database = new DBIFactory().build(environment(), hsqlConfig.getDataSourceFactory(), "h2test");
    database.registerArgumentFactory(new DependencyIdArgumentFactory());
    database.registerArgumentFactory(new ServiceIdArgumentFactory());
    database.registerArgumentFactory(new TenacityConfigurationArgumentFactory(Jackson.newObjectMapper()));
    database.registerArgumentFactory(new DateTimeArgumentFactory());
}
 
Example #6
Source File: DatabaseModule.java    From monasca-common with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
  bind(DataSourceFactory.class).toInstance(config);
  bind(DBI.class).toProvider(new Provider<DBI>() {
    @Override
    public DBI get() {
      try {
        return new DBIFactory().build(environment, config, "platform");
      } catch (ClassNotFoundException e) {
        throw new ProvisionException("Failed to provision DBI", e);
      }
    }
  }).in(Scopes.SINGLETON);
}
 
Example #7
Source File: AirpalModule.java    From airpal with Apache License 2.0 5 votes vote down vote up
@Singleton
@Provides
public DBI provideDBI(ObjectMapper objectMapper)
        throws ClassNotFoundException
{
    final DBIFactory factory = new DBIFactory();
    final DBI dbi = factory.build(environment, config.getDataSourceFactory(), provideDbType().name());
    dbi.registerMapper(new TableRow.TableRowMapper(objectMapper));
    dbi.registerMapper(new QueryStoreMapper(objectMapper));
    dbi.registerArgumentFactory(new UUIDArgumentFactory());
    dbi.registerArgumentFactory(new URIArgumentFactory());

    return dbi;
}
 
Example #8
Source File: NiPingMonitorApplication.java    From SAPNetworkMonitor with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run(ServerConfiguration configuration, Environment environment) throws Exception {

    final DBIFactory factory = new DBIFactory();
    final DBI jdbi = factory.build(environment, configuration.getDataSourceFactory(), "sapData");

    ObjectMapper objectMapper = environment.getObjectMapper();
    SapConfiguration sapConfiguration = configuration.getSapConfig();
    JobConfiguration jobConfiguration = configuration.getJobConfig();
    NiPingServiceBinder niPingServiceBinder = new NiPingServiceBinder(jdbi, objectMapper, sapConfiguration, jobConfiguration);

    ServiceLocator serviceLocator = ServiceLocatorUtilities.bind(niPingServiceBinder);
    SapBasicAuthenticator sapBasicAuthenticator = ServiceLocatorUtilities.getService(serviceLocator, SapBasicAuthenticator.class
            .getName());
    SapOAuthenticator sapOAuthenticator = ServiceLocatorUtilities.getService(serviceLocator, SapOAuthenticator.class.getName());

    final BasicCredentialAuthFilter basicAuthFilter = new BasicCredentialAuthFilter.Builder<BasicAuthUser>()
            .setAuthenticator(sapBasicAuthenticator)
            .buildAuthFilter();
    final AuthFilter oAuthFilter = new OAuthCredentialAuthFilter.Builder<OAuthUser>()
            .setAuthenticator(sapOAuthenticator)
            .setPrefix("Bearer")
            .buildAuthFilter();

    final PolymorphicAuthDynamicFeature feature = new PolymorphicAuthDynamicFeature<UserPrincipal>(ImmutableMap.of(BasicAuthUser
            .class, basicAuthFilter, OAuthUser.class, oAuthFilter));
    final AbstractBinder binder = new PolymorphicAuthValueFactoryProvider.Binder<>(ImmutableSet.of(BasicAuthUser.class, OAuthUser
            .class));
    environment.jersey().register(new AuthFilterDynamicBinding());
    environment.jersey().register(feature);
    environment.jersey().register(binder);

    environment.jersey().register(niPingServiceBinder);
    environment.jersey().packages("com.cloudwise.sap.niping.auth");
    environment.jersey().packages("com.cloudwise.sap.niping.service");
    environment.jersey().packages("com.cloudwise.sap.niping.dao");
    environment.jersey().packages("com.cloudwise.sap.niping.common.vo.converter");
    environment.jersey().packages("com.cloudwise.sap.niping.resource");

    environment.jersey().register(SessionFactoryProvider.class);
    environment.servlets().setSessionHandler(new SessionHandler());
}
 
Example #9
Source File: RufusApplication.java    From rufus with MIT License 4 votes vote down vote up
@Override
public void run(RufusConfiguration conf, Environment env) throws Exception {
    final DBIFactory factory = new DBIFactory();
    final DBI jdbi = factory.build(env, conf.getDataSourceFactory(), DB_SOURCE);

    final UserDao userDao = jdbi.onDemand(UserDao.class);
    final ArticleDao articleDao = jdbi.onDemand(ArticleDao.class);

    final FeedProcessorImpl processor = FeedProcessorImpl.newInstance(articleDao);
    final FeedParser parser = new FeedParser(articleDao, processor);

    final JwtConsumer jwtConsumer = new JwtConsumerBuilder()
        .setAllowedClockSkewInSeconds(30)
        .setRequireExpirationTime()
        .setRequireSubject()
        .setVerificationKey(new HmacKey(VERIFICATION_KEY))
        .setRelaxVerificationKeyValidation()
        .build();
    final CachingJwtAuthenticator<User> cachingJwtAuthenticator = new CachingJwtAuthenticator<>(
        env.metrics(),
        new JwtAuthenticator(userDao),
        conf.getAuthenticationCachePolicy()
    );

    env.jersey().register(new ArticleResource(userDao, articleDao, processor, parser));
    env.jersey().register(
        new UserResource(
            new BasicAuthenticator(userDao),
            new TokenGenerator(VERIFICATION_KEY),
            userDao,
            articleDao
        )
    );

    //route source
    env.jersey().setUrlPattern(ROOT_PATH);

    env.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
    env.jersey().register(new AuthDynamicFeature(
        new JwtAuthFilter.Builder<User>()
            .setJwtConsumer(jwtConsumer)
            .setRealm(REALM)
            .setPrefix(BEARER)
            .setAuthenticator(cachingJwtAuthenticator)
            .buildAuthFilter()
    ));
}
 
Example #10
Source File: JdbiStore.java    From breakerbox with Apache License 2.0 4 votes vote down vote up
public JdbiStore(JdbiConfiguration storeConfiguration,
                 Environment environment) throws Exception {
    this(storeConfiguration, environment,
            new DBIFactory().build(environment, storeConfiguration.getDataSourceFactory(), "breakerbox"));
}