Example usage for org.hibernate.boot.registry StandardServiceRegistryBuilder configure

List of usage examples for org.hibernate.boot.registry StandardServiceRegistryBuilder configure

Introduction

In this page you can find the example usage for org.hibernate.boot.registry StandardServiceRegistryBuilder configure.

Prototype

@SuppressWarnings({ "unchecked" })
    public StandardServiceRegistryBuilder configure(LoadedConfig loadedConfig) 

Source Link

Usage

From source file:com.tremolosecurity.idp.providers.OpenIDConnectIdP.java

License:Apache License

private void initializeHibernate(String driver, String user, String password, String url, String dialect,
        int maxCons, int maxIdleCons, String validationQuery, String mappingFile, String createSchema) {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();

    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", driver);
    config.setProperty("hibernate.connection.password", password);
    config.setProperty("hibernate.connection.url", url);
    config.setProperty("hibernate.connection.username", user);
    config.setProperty("hibernate.dialect", dialect);

    if (createSchema == null || createSchema.equalsIgnoreCase("true")) {
        config.setProperty("hibernate.hbm2ddl.auto", "update");
    }/* w w w .  j a va 2  s. c o  m*/

    config.setProperty("show_sql", "true");
    config.setProperty("hibernate.current_session_context_class", "thread");

    config.setProperty("hibernate.c3p0.max_size", Integer.toString(maxCons));
    config.setProperty("hibernate.c3p0.maxIdleTimeExcessConnections", Integer.toString(maxIdleCons));

    if (validationQuery != null && !validationQuery.isEmpty()) {
        config.setProperty("hibernate.c3p0.testConnectionOnCheckout", "true");
    }
    config.setProperty("hibernate.c3p0.autoCommitOnClose", "true");

    //config.setProperty("hibernate.c3p0.debugUnreturnedConnectionStackTraces", "true");
    //config.setProperty("hibernate.c3p0.unreturnedConnectionTimeout", "30");

    if (validationQuery == null) {
        validationQuery = "SELECT 1";
    }
    config.setProperty("hibernate.c3p0.preferredTestQuery", validationQuery);

    LoadedConfig lc = null;

    if (mappingFile == null || mappingFile.trim().isEmpty()) {
        JaxbCfgHibernateConfiguration jaxbCfg = new JaxbCfgHibernateConfiguration();
        jaxbCfg.setSessionFactory(new JaxbCfgSessionFactory());

        JaxbCfgMappingReferenceType mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(OIDCSession.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        lc = LoadedConfig.consume(jaxbCfg);
    } else {
        lc = LoadedConfig.baseline();
    }

    StandardServiceRegistry registry = builder.configure(lc).applySettings(config.getProperties()).build();
    try {
        if (mappingFile == null || mappingFile.trim().isEmpty()) {
            sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } else {
            sessionFactory = new MetadataSources(registry).addResource(mappingFile).buildMetadata()
                    .buildSessionFactory();
        }

        GlobalEntries.getGlobalEntries().getConfigManager().addThread(new StopableThread() {

            @Override
            public void run() {

            }

            @Override
            public void stop() {
                logger.info("Stopping hibernate");
                sessionFactory.close();

            }

        });
    } catch (Exception e) {
        e.printStackTrace();
        // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
        // so destroy it manually.
        StandardServiceRegistryBuilder.destroy(registry);
    }
}

From source file:com.tremolosecurity.provisioning.core.ProvisioningEngineImpl.java

License:Apache License

private void initializeHibernate(ApprovalDBType adbt) {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();

    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", adbt.getDriver());
    config.setProperty("hibernate.connection.password", adbt.getPassword());
    config.setProperty("hibernate.connection.url", adbt.getUrl());
    config.setProperty("hibernate.connection.username", adbt.getUser());
    config.setProperty("hibernate.dialect", adbt.getHibernateDialect());

    if (adbt.isHibernateCreateSchema() == null || adbt.isHibernateCreateSchema()) {
        config.setProperty("hibernate.hbm2ddl.auto", "update");
    }/* ww  w .  j a  va  2  s. co m*/
    config.setProperty("show_sql", "true");
    config.setProperty("hibernate.current_session_context_class", "thread");

    config.setProperty("hibernate.c3p0.max_size", Integer.toString(adbt.getMaxConns()));
    config.setProperty("hibernate.c3p0.maxIdleTimeExcessConnections", Integer.toString(adbt.getMaxIdleConns()));

    if (adbt.getValidationQuery() != null && !adbt.getValidationQuery().isEmpty()) {
        config.setProperty("hibernate.c3p0.testConnectionOnCheckout", "true");
    }
    config.setProperty("hibernate.c3p0.autoCommitOnClose", "true");

    if (adbt.getHibernateProperty() != null) {
        for (ParamType pt : adbt.getHibernateProperty()) {
            config.setProperty(pt.getName(), pt.getValue());
        }
    }

    //config.setProperty("hibernate.c3p0.debugUnreturnedConnectionStackTraces", "true");
    //config.setProperty("hibernate.c3p0.unreturnedConnectionTimeout", "30");

    String validationQuery = adbt.getValidationQuery();
    if (validationQuery == null) {
        validationQuery = "SELECT 1";
    }
    config.setProperty("hibernate.c3p0.preferredTestQuery", validationQuery);

    LoadedConfig lc = null;

    if (adbt.getHibernateConfig() == null || adbt.getHibernateConfig().trim().isEmpty()) {
        JaxbCfgHibernateConfiguration jaxbCfg = new JaxbCfgHibernateConfiguration();
        jaxbCfg.setSessionFactory(new JaxbCfgSessionFactory());

        JaxbCfgMappingReferenceType mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(AllowedApprovers.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Approvals.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(ApproverAttributes.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Approvers.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(AuditLogs.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(AuditLogType.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Escalation.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Targets.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(UserAttributes.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Users.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(WorkflowParameters.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(Workflows.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        lc = LoadedConfig.consume(jaxbCfg);
    } else {
        lc = LoadedConfig.baseline();
    }

    StandardServiceRegistry registry = builder.configure(lc).applySettings(config.getProperties()).build();
    try {
        sessionFactory = null;

        if (adbt.getHibernateConfig() == null || adbt.getHibernateConfig().trim().isEmpty()) {
            sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } else {

            sessionFactory = new MetadataSources(registry).addResource(adbt.getHibernateConfig())
                    .buildMetadata().buildSessionFactory();
        }

        this.cfgMgr.addThread(new StopableThread() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

            }

            @Override
            public void stop() {
                logger.info("Stopping hibernate");
                sessionFactory.close();

            }

        });

        org.hibernate.Session session = sessionFactory.openSession();

        this.auditLogTypes = new HashMap<String, AuditLogType>();

        List<AuditLogType> alts = session.createCriteria(AuditLogType.class).list();
        if (alts.size() == 0) {
            session.beginTransaction();
            AuditLogType alt = new AuditLogType();
            alt.setName("Add");
            session.save(alt);

            this.auditLogTypes.put("add", alt);

            alt = new AuditLogType();
            alt.setName("Delete");
            session.save(alt);

            this.auditLogTypes.put("delete", alt);

            alt = new AuditLogType();
            alt.setName("Replace");
            session.save(alt);

            this.auditLogTypes.put("replace", alt);

            session.getTransaction().commit();
        } else {
            for (AuditLogType alt : alts) {
                this.auditLogTypes.put(alt.getName().toLowerCase(), alt);
            }
        }

        session.close();

    } catch (Exception e) {
        e.printStackTrace();
        // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
        // so destroy it manually.
        StandardServiceRegistryBuilder.destroy(registry);
    }
}

From source file:com.tremolosecurity.proxy.auth.PasswordReset.java

License:Apache License

private void initializeHibernate(String driver, String user, String password, String url, String dialect,
        int maxCons, int maxIdleCons, String validationQuery, String mappingFile, String createSchema) {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();

    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", driver);
    config.setProperty("hibernate.connection.password", password);
    config.setProperty("hibernate.connection.url", url);
    config.setProperty("hibernate.connection.username", user);
    config.setProperty("hibernate.dialect", dialect);

    if (createSchema == null || createSchema.equalsIgnoreCase("true")) {
        config.setProperty("hibernate.hbm2ddl.auto", "update");
    }// w  w w  . j  a va  2 s . c  o  m

    config.setProperty("show_sql", "true");
    config.setProperty("hibernate.current_session_context_class", "thread");

    config.setProperty("hibernate.c3p0.max_size", Integer.toString(maxCons));
    config.setProperty("hibernate.c3p0.maxIdleTimeExcessConnections", Integer.toString(maxIdleCons));

    if (validationQuery != null && !validationQuery.isEmpty()) {
        config.setProperty("hibernate.c3p0.testConnectionOnCheckout", "true");
    }

    config.setProperty("hibernate.c3p0.autoCommitOnClose", "true");

    //config.setProperty("hibernate.c3p0.debugUnreturnedConnectionStackTraces", "true");
    //config.setProperty("hibernate.c3p0.unreturnedConnectionTimeout", "30");

    if (validationQuery == null) {
        validationQuery = "SELECT 1";
    }
    config.setProperty("hibernate.c3p0.preferredTestQuery", validationQuery);

    LoadedConfig lc = null;

    if (mappingFile == null || mappingFile.trim().isEmpty()) {

        JaxbCfgHibernateConfiguration jaxbCfg = new JaxbCfgHibernateConfiguration();
        jaxbCfg.setSessionFactory(new JaxbCfgSessionFactory());

        JaxbCfgMappingReferenceType mrt = new JaxbCfgMappingReferenceType();
        mrt.setClazz(PasswordResetRequest.class.getName());
        jaxbCfg.getSessionFactory().getMapping().add(mrt);

        lc = LoadedConfig.consume(jaxbCfg);
    } else {
        lc = LoadedConfig.baseline();
    }

    StandardServiceRegistry registry = builder.configure(lc).applySettings(config.getProperties()).build();
    try {
        sessionFactory = null;

        if (mappingFile == null || mappingFile.trim().isEmpty()) {
            sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } else {
            sessionFactory = new MetadataSources(registry).addResource(mappingFile).buildMetadata()
                    .buildSessionFactory();
        }

        this.cfgMgr.addThread(new StopableThread() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

            }

            @Override
            public void stop() {
                logger.info("Stopping hibernate");
                sessionFactory.close();

            }

        });
    } catch (Exception e) {
        e.printStackTrace();
        // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
        // so destroy it manually.
        StandardServiceRegistryBuilder.destroy(registry);
    }
}

From source file:com.tremolosecurity.unison.k8s.dataobjects.CreateLocalUsers.java

License:Apache License

@Override
public void configure(String name, Properties props, NameSpace nameSpace) throws LDAPException {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();

    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", props.getProperty("driver"));
    config.setProperty("hibernate.connection.password", props.getProperty("password"));
    config.setProperty("hibernate.connection.url", props.getProperty("url"));
    config.setProperty("hibernate.connection.username", props.getProperty("user"));
    config.setProperty("hibernate.dialect", props.getProperty("dialect"));
    config.setProperty("hibernate.hbm2ddl.auto", "update");
    config.setProperty("show_sql", "true");
    config.setProperty("hibernate.current_session_context_class", "thread");

    config.setProperty("hibernate.c3p0.max_size", Integer.toString(10));
    config.setProperty("hibernate.c3p0.maxIdleTimeExcessConnections", Integer.toString(10));

    JaxbCfgHibernateConfiguration jaxbCfg = new JaxbCfgHibernateConfiguration();
    jaxbCfg.setSessionFactory(new JaxbCfgSessionFactory());

    JaxbCfgMappingReferenceType mrt = new JaxbCfgMappingReferenceType();
    mrt.setClazz(LocalUser.class.getName());
    jaxbCfg.getSessionFactory().getMapping().add(mrt);

    mrt = new JaxbCfgMappingReferenceType();
    mrt.setClazz(LocalGroup.class.getName());
    jaxbCfg.getSessionFactory().getMapping().add(mrt);

    LoadedConfig lc = LoadedConfig.consume(jaxbCfg);
    StandardServiceRegistry registry = builder.configure(lc).applySettings(config.getProperties()).build();
    SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();

    Session session = sessionFactory.openSession();

    List<LocalGroup> groups = session.createCriteria(LocalGroup.class).list();
    if (groups.size() == 0) {
        session.beginTransaction();/* w w w  .j  av a 2  s  .c  o  m*/
        LocalGroup admins = new LocalGroup();
        admins.setName("administrators");
        admins.setDescription(
                "System administrators with approval access for new projects and new cluster admins");
        session.save(admins);

        LocalGroup sys = new LocalGroup();
        sys.setName("System");
        sys.setDescription("System level groups not assigned to local users");
        session.save(sys);

        LocalGroup users = new LocalGroup();
        users.setName("users");
        users.setDescription("All users are members");
        session.save(users);

        LocalUser sysUser = new LocalUser();
        sysUser.setSub("system");
        sysUser.setMail("");
        sysUser.setGroups(new ArrayList<LocalGroup>());
        sysUser.getGroups().add(sys);
        session.save(sysUser);

        session.getTransaction().commit();
    }

    sessionFactory.close();

}