Example usage for org.hibernate.cfg AvailableSettings DRIVER

List of usage examples for org.hibernate.cfg AvailableSettings DRIVER

Introduction

In this page you can find the example usage for org.hibernate.cfg AvailableSettings DRIVER.

Prototype

String DRIVER

To view the source code for org.hibernate.cfg AvailableSettings DRIVER.

Click Source Link

Document

Names the JDBC driver class

Usage

From source file:com.github.carlomicieli.config.ApplicationConfig.java

License:Apache License

/**
 * Return the data source bean.//w w  w.j ava2s.c o m
 * @return the data source bean.
 */
public @Bean DataSource dataSource() {
    log.info(Arrays.asList(env.getActiveProfiles()).toString());

    ComboPooledDataSource ds = new ComboPooledDataSource();

    try {
        ds.setDriverClass(env.getProperty(AvailableSettings.DRIVER));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    ds.setJdbcUrl(env.getProperty(AvailableSettings.URL));
    ds.setUser(env.getProperty(AvailableSettings.USER));
    ds.setPassword(env.getProperty(AvailableSettings.PASS));

    return ds;
}

From source file:com.vmware.photon.controller.apife.db.HibernateTestModule.java

License:Open Source License

@Provides
@Singleton/*from   w w w.ja  va  2 s .  c om*/
public SessionFactory getSessionFactory() {
    Configuration configuration = new Configuration();

    Reflections reflections = new Reflections("com.vmware.photon.controller.apife.entities");
    Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);

    Reflections baseReflections = new Reflections("com.vmware.photon.controller.apife.entities.base");
    classes.addAll(baseReflections.getTypesAnnotatedWith(Entity.class));

    Reflections commonReflections = new Reflections("com.vmware.photon.controller.api.common");
    classes.addAll(commonReflections.getTypesAnnotatedWith(Entity.class));

    for (final Class<?> clazz : classes) {
        configuration.addAnnotatedClass(clazz);
    }

    configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed");
    configuration.setProperty(AvailableSettings.DIALECT, CustomH2Dialect.class.getName());
    configuration.setProperty(AvailableSettings.DRIVER, "org.h2.Driver");
    // in memory DB, wait up to 10 seconds after last connection closed before deleting data
    configuration.setProperty(AvailableSettings.URL, "jdbc:h2:mem:test;DB_CLOSE_DELAY=10");
    configuration.setProperty(AvailableSettings.HBM2DDL_AUTO, "create");
    configuration.setProperty(AvailableSettings.SHOW_SQL, "true");
    configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);

    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
            .build();

    return configuration.buildSessionFactory(serviceRegistry);
}

From source file:com.vmware.photon.controller.apife.db.MigrationTest.java

License:Open Source License

@Test
public void testMigrations() throws SQLException, LiquibaseException {
    try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:migrations", "sa", "")) {
        Liquibase liquibase = new Liquibase("migrations.xml", new ClassLoaderResourceAccessor(),
                new JdbcConnection(connection));
        liquibase.update("");

        Configuration configuration = new Configuration();

        Reflections reflections = new Reflections("com.vmware.photon.controller.apife.entities");
        Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);

        Reflections commonReflections = new Reflections("com.vmware.photon.controller.api.common");
        classes.addAll(commonReflections.getTypesAnnotatedWith(Entity.class));
        for (final Class<?> clazz : classes) {
            configuration.addAnnotatedClass(clazz);
        }/*from ww  w.j  a v  a 2 s .c  o  m*/

        configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");
        configuration.setProperty(AvailableSettings.DIALECT, "org.hibernate.dialect.H2Dialect");
        configuration.setProperty(AvailableSettings.DRIVER, "org.h2.Driver");
        configuration.setProperty(AvailableSettings.URL, "jdbc:h2:mem:migrations");
        configuration.setProperty(AvailableSettings.USER, "sa");
        configuration.setProperty(AvailableSettings.PASS, "");
        configuration.setProperty(AvailableSettings.HBM2DDL_AUTO, "validate");
        configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);
        factory.close();
    }
}

From source file:com.zaxxer.hikari.hibernate.HikariConfigurationUtil.java

License:Apache License

/**
 * Create/load a HikariConfig from Hibernate properties.
 *
 * @param props a map of Hibernate properties
 * @return a HikariConfig/*ww w  .  j a va 2  s .  c o  m*/
 */
@SuppressWarnings("rawtypes")
public static HikariConfig loadConfiguration(Map props) {
    Properties hikariProps = new Properties();
    copyProperty(AvailableSettings.ISOLATION, props, "transactionIsolation", hikariProps);
    copyProperty(AvailableSettings.AUTOCOMMIT, props, "autoCommit", hikariProps);
    copyProperty(AvailableSettings.DRIVER, props, "driverClassName", hikariProps);
    copyProperty(AvailableSettings.URL, props, "jdbcUrl", hikariProps);
    copyProperty(AvailableSettings.USER, props, "username", hikariProps);
    copyProperty(AvailableSettings.PASS, props, "password", hikariProps);

    for (Object keyo : props.keySet()) {
        String key = (String) keyo;
        if (key.startsWith(CONFIG_PREFIX)) {
            hikariProps.setProperty(key.substring(CONFIG_PREFIX.length()), (String) props.get(key));
        }
    }

    return new HikariConfig(hikariProps);
}

From source file:name.livitski.tools.persista.StorageBootstrap.java

License:Open Source License

@SuppressWarnings("unchecked")
private void updateSchema() throws ApplicationBeanException {
    try {/*w  w w.  j  a  va  2 s.  com*/
        String user = readSetting(UpdaterUserNameSetting.class);
        String password;
        if (null != user)
            password = readSetting(UpdaterPasswordSetting.class);
        else {
            user = readSetting(UserNameSetting.class);
            password = readSetting(PasswordSetting.class);
        }

        org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
        cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "update");
        cfg.setProperty(AvailableSettings.DIALECT, readSetting(HibernateSQLDialectSetting.class).getName());
        cfg.setProperty(AvailableSettings.DRIVER, getJDBCDriverClass().getName());
        cfg.setProperty(AvailableSettings.URL, db.getMetaData().getURL());
        cfg.setProperty(AvailableSettings.USER, user);
        cfg.setProperty(AvailableSettings.PASS, password);

        for (Class<?> clazz : getEntityClasses())
            cfg.addAnnotatedClass(clazz);

        SchemaUpdate worker = new SchemaUpdate(cfg);
        worker.setDelimiter(";");
        worker.setHaltOnError(true);
        if (null != ddlDumpFile)
            worker.setOutputFile(ddlDumpFile.getAbsolutePath());
        worker.execute(true, true);
        List<Throwable> errs = (List<Throwable>) worker.getExceptions();
        if (null != errs && !errs.isEmpty())
            for (Iterator<Throwable> erri = errs.iterator();;) {
                Throwable err = erri.next();
                if (erri.hasNext())
                    log().error("", err);
                else
                    throw new SchemaUpdateException(this,
                            "Error(s) occured during the schema update, the last error is shown.", err);
            }
    } catch (ConfigurationException badConfig) {
        throw new StorageConfigurationException(this, badConfig);
    } catch (SQLException e) {
        throw new DatabaseException(this, e);
    }
}

From source file:net.lizalab.util.jasypt.h4.ext.connectionprovider.EncryptedC3P0ConnectionProvider.java

License:Apache License

/**
 * Overrides parent method to scan and replace encrypted configuration values
 * with their corresponding decrypted values before invoking the parent with
 * them./*  w  w  w  .j av a2 s . com*/
 * <p>
 * Looks in the configuration for the name with which the PBE encryptor has been
 * registered with {@link HibernatePBEEncryptorRegistry} using the key 
 * {@link ParameterNaming#ENCRYPTOR_REGISTERED_NAME}.
 * Scans the configuration for supported connection configuration values, checks
 * to see if they are encrypted and replaces them with the decrypted values if
 * they are.
 * </p>
 * @throws EncryptionInitializationException If the PBE encryptor registered name
 * configuration cannot be found or if lookup by the registered name does not return
 * a valid encryptor.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void configure(Map configurationValues) {
    final String methodName = "configure : ";

    // Find encryptor registered for config.
    final String encryptorRegisteredName = ConfigurationHelper
            .getString(ParameterNaming.ENCRYPTOR_REGISTERED_NAME, configurationValues);
    if (encryptorRegisteredName == null) {
        throw new EncryptionInitializationException(
                "No encryptor registered in configuration! Encryptor must be registered with property name: "
                        + ParameterNaming.ENCRYPTOR_REGISTERED_NAME);
    }
    LOGGER.info("{} Using Encryptor registered with name: {}", methodName, encryptorRegisteredName);

    final HibernatePBEEncryptorRegistry encryptorRegistry = HibernatePBEEncryptorRegistry.getInstance();
    final PBEStringEncryptor encryptor = encryptorRegistry.getPBEStringEncryptor(encryptorRegisteredName);

    if (encryptor == null) {
        throw new EncryptionInitializationException(
                "No string encryptor registered for hibernate " + "with name: " + encryptorRegisteredName);
    }
    LOGGER.info("{} Found registered Encryptor, checking connection properties ..", methodName);

    configurationValues.put(AvailableSettings.DRIVER, decryptIfEncrypted(encryptor, AvailableSettings.DRIVER,
            ConfigurationHelper.getString(AvailableSettings.DRIVER, configurationValues)));
    configurationValues.put(AvailableSettings.URL, decryptIfEncrypted(encryptor, AvailableSettings.URL,
            ConfigurationHelper.getString(AvailableSettings.URL, configurationValues)));
    configurationValues.put(AvailableSettings.USER, decryptIfEncrypted(encryptor, AvailableSettings.USER,
            ConfigurationHelper.getString(AvailableSettings.USER, configurationValues)));
    configurationValues.put(AvailableSettings.PASS, decryptIfEncrypted(encryptor, AvailableSettings.PASS,
            ConfigurationHelper.getString(AvailableSettings.PASS, configurationValues)));
    // Let Hibernate do the rest.
    super.configure(configurationValues);
}

From source file:net.lizalab.util.jasypt.h4.ext.connectionprovider.EncryptedDriverManagerConnectionProviderImplTest.java

License:Apache License

/**
 * Verifies requirement for configuration of name encryptor is registered
 * with is enforced./* w w  w. j a  v  a2 s.  co m*/
 */
@Test(expected = EncryptionInitializationException.class)
public final void testEncryptorRegisteredNameReqd() {
    // Initialize config values without encryptor registered name.
    Map<String, String> testConfigValues = new HashMap<String, String>();
    testConfigValues.put(AvailableSettings.DRIVER, testEncryptedCfgDriver);
    testConfigValues.put(AvailableSettings.URL, testEncryptedCfgUrl);
    testConfigValues.put(AvailableSettings.USER, testEncryptedCfgUser);
    testConfigValues.put(AvailableSettings.PASS, testEncryptedCfgPass);
    // Initialize test instance and invoke target method which should 
    // fail with the expected exception.
    testCPImpl = new EncryptedDriverManagerConnectionProviderImpl();
    testCPImpl.configure(testConfigValues);
}

From source file:net.lizalab.util.jasypt.h4.ext.connectionprovider.EncryptedDriverManagerConnectionProviderImplTest.java

License:Apache License

/**
 * Verifies decryption of encrypted configuration values for supported
 * configuration parameters and confirms there are no other side effects.
 *///from ww  w  .  jav a  2s  .c  o  m
@Test
public final void testConfigureDecryption() {
    // Set mock expectations to be auto-validated.
    final Sequence decryptSequence = context.sequence("decryptSequence");
    context.checking(new Expectations() {
        {
            oneOf(mockEncryptor).decrypt(testEncryptedDriverText);
            inSequence(decryptSequence);
            will(returnValue(testDriver));
            oneOf(mockEncryptor).decrypt(testEncryptedUrlText);
            inSequence(decryptSequence);
            will(returnValue(testUrl));
            oneOf(mockEncryptor).decrypt(testEncryptedUserText);
            inSequence(decryptSequence);
            will(returnValue(testUser));
            oneOf(mockEncryptor).decrypt(testEncryptedPassText);
            inSequence(decryptSequence);
            will(returnValue(testPassword));
        }
    });
    // Setup test configuration.
    Map<String, String> testConfigValues = new HashMap<String, String>();
    testConfigValues.put(ParameterNaming.ENCRYPTOR_REGISTERED_NAME, testEncryptorCfgName);
    testConfigValues.put(AvailableSettings.DRIVER, testEncryptedCfgDriver);
    testConfigValues.put(AvailableSettings.URL, testEncryptedCfgUrl);
    testConfigValues.put(AvailableSettings.USER, testEncryptedCfgUser);
    testConfigValues.put(AvailableSettings.PASS, testEncryptedCfgPass);
    // Track values to be asserted later in the test.
    int cfgsCt = testConfigValues.size();
    // Initialize the test instance and invoke target method to trigger mock expectation assertion.
    testCPImpl = new EncryptedDriverManagerConnectionProviderImpl();
    testCPImpl.configure(testConfigValues);
    // Mock expectations should have already verified decryption, now proceed 
    // with manual assertions.
    // First, verify that the decrypted values were set in the cfg values map
    // since it gets passed to super to complete actual configuration.
    assertEquals("Cfg values map not updated with decrypted driver!", testDriver,
            testConfigValues.get(AvailableSettings.DRIVER));
    assertEquals("Cfg values map not updated with decrypted url!", testUrl,
            testConfigValues.get(AvailableSettings.URL));
    assertEquals("Cfg values map not updated with decrypted user!", testUser,
            testConfigValues.get(AvailableSettings.USER));
    assertEquals("Cfg values map not updated with decrypted password!", testPassword,
            testConfigValues.get(AvailableSettings.PASS));
    // Now verify there were no other modifications to config values.
    assertEquals("Unexpected no. of config values!", cfgsCt, testConfigValues.size());
    assertEquals("Config value other than encryptable set modified!", testEncryptorCfgName,
            testConfigValues.get(ParameterNaming.ENCRYPTOR_REGISTERED_NAME));
}

From source file:net.lizalab.util.jasypt.h4.ext.connectionprovider.EncryptedDriverManagerConnectionProviderImplTest.java

License:Apache License

/**
 * Verifies that unencrypted values for supported configuration parameters
 * are passed through unchanged. //from   w  w w .ja  va 2  s .c  o  m
 */
@Test
public final void testConfigurePassThrough() {
    // Setup test configuration.
    Map<String, String> testConfigValues = new HashMap<String, String>();
    testConfigValues.put(ParameterNaming.ENCRYPTOR_REGISTERED_NAME, testEncryptorCfgName);
    testConfigValues.put(AvailableSettings.DRIVER, testDriver);
    testConfigValues.put(AvailableSettings.URL, testUrl);
    testConfigValues.put(AvailableSettings.USER, testUser);
    testConfigValues.put(AvailableSettings.PASS, testPassword);
    // Track values to be asserted later in the test.
    int cfgsCt = testConfigValues.size();
    // Initialize the test instance and invoke target method. Mock 
    // expectations aren't set since encryptor should never be invoked
    // in this case.
    testCPImpl = new EncryptedDriverManagerConnectionProviderImpl();
    testCPImpl.configure(testConfigValues);
    // Verify that the specified values are unchanged.
    assertEquals("Cfg values map for driver modified!", testDriver,
            testConfigValues.get(AvailableSettings.DRIVER));
    assertEquals("Cfg values map for url modified!", testUrl, testConfigValues.get(AvailableSettings.URL));
    assertEquals("Cfg values map for user modified!", testUser, testConfigValues.get(AvailableSettings.USER));
    assertEquals("Cfg values map for password modified!", testPassword,
            testConfigValues.get(AvailableSettings.PASS));
    // Now verify there were no other modifications to config values.
    assertEquals("Unexpected no. of config values!", cfgsCt, testConfigValues.size());
    assertEquals("Config value other than encryptable set modified!", testEncryptorCfgName,
            testConfigValues.get(ParameterNaming.ENCRYPTOR_REGISTERED_NAME));
}

From source file:nl.rivm.cib.epidemes.geodb.jdbc.GeoJPAConfig.java

License:Apache License

@Key(AvailableSettings.DRIVER)
@DefaultValue("org.postgresql.ds.PGSimpleDataSource")
Class<? extends DataSource> jdbcDataSourceDriver();