Search in sources :

Example 1 with MultiTenancyImplementation

use of com.manydesigns.portofino.persistence.hibernate.multitenancy.MultiTenancyImplementation in project Portofino by ManyDesigns.

the class Persistence method initConnectionProvider.

protected void initConnectionProvider(Database database) {
    logger.info("Initializing connection provider for database " + database.getDatabaseName());
    try {
        ConnectionProvider connectionProvider = database.getConnectionProvider();
        connectionProvider.init(databasePlatformsRegistry);
        if (connectionProvider.getStatus().equals(ConnectionProvider.STATUS_CONNECTED)) {
            MultiTenancyImplementation implementation = getMultiTenancyImplementation(database);
            SessionFactoryBuilder builder = new SessionFactoryBuilder(database, configuration.getProperties(), implementation);
            SessionFactoryAndCodeBase sessionFactoryAndCodeBase = builder.buildSessionFactory();
            HibernateDatabaseSetup setup = new HibernateDatabaseSetup(database, sessionFactoryAndCodeBase.sessionFactory, sessionFactoryAndCodeBase.codeBase, builder.getEntityMode(), configuration.getProperties(), implementation);
            String databaseName = database.getDatabaseName();
            HibernateDatabaseSetup oldSetup = setups.get(databaseName);
            setups.put(databaseName, setup);
            if (oldSetup != null) {
                oldSetup.dispose();
                databaseSetupEvents.onNext(new DatabaseSetupEvent(oldSetup, setup));
            } else {
                databaseSetupEvents.onNext(new DatabaseSetupEvent(DatabaseSetupEvent.ADDED, setup));
            }
        }
    } catch (Exception e) {
        logger.error("Could not create connection provider for " + database, e);
    }
}
Also used : SessionFactoryAndCodeBase(com.manydesigns.portofino.persistence.hibernate.SessionFactoryAndCodeBase) MultiTenancyImplementation(com.manydesigns.portofino.persistence.hibernate.multitenancy.MultiTenancyImplementation) SessionFactoryBuilder(com.manydesigns.portofino.persistence.hibernate.SessionFactoryBuilder) HibernateDatabaseSetup(com.manydesigns.portofino.persistence.hibernate.HibernateDatabaseSetup) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException)

Example 2 with MultiTenancyImplementation

use of com.manydesigns.portofino.persistence.hibernate.multitenancy.MultiTenancyImplementation in project Portofino by ManyDesigns.

the class Persistence method getMultiTenancyImplementation.

protected MultiTenancyImplementation getMultiTenancyImplementation(Database database) {
    Optional<MultiTenant> multiTenant = database.getJavaAnnotation(MultiTenant.class);
    if (multiTenant.isPresent()) {
        Class<? extends MultiTenancyImplementation> implClass = multiTenant.get().strategy();
        // TODO injection?
        if (!MultiTenancyImplementation.class.isAssignableFrom(implClass)) {
            throw new ClassCastException(implClass + " does not extend " + MultiTenancyImplementation.class);
        }
        try {
            MultiTenancyImplementation implementation = multiTenancyImplementationFactory.make(implClass);
            MultiTenancyStrategy strategy = implementation.getStrategy();
            if (strategy.requiresMultiTenantConnectionProvider()) {
                return implementation;
            }
        } catch (Exception e) {
            throw new RuntimeException("Could not instantiate multi tenancy implementation " + implClass + " for " + database, e);
        }
    }
    return null;
}
Also used : MultiTenancyImplementation(com.manydesigns.portofino.persistence.hibernate.multitenancy.MultiTenancyImplementation) MultiTenancyStrategy(org.hibernate.MultiTenancyStrategy) MultiTenant(com.manydesigns.portofino.database.multitenancy.MultiTenant) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException)

Aggregations

MultiTenancyImplementation (com.manydesigns.portofino.persistence.hibernate.multitenancy.MultiTenancyImplementation)2 IOException (java.io.IOException)2 JAXBException (javax.xml.bind.JAXBException)2 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)2 FileSystemException (org.apache.commons.vfs2.FileSystemException)2 MultiTenant (com.manydesigns.portofino.database.multitenancy.MultiTenant)1 HibernateDatabaseSetup (com.manydesigns.portofino.persistence.hibernate.HibernateDatabaseSetup)1 SessionFactoryAndCodeBase (com.manydesigns.portofino.persistence.hibernate.SessionFactoryAndCodeBase)1 SessionFactoryBuilder (com.manydesigns.portofino.persistence.hibernate.SessionFactoryBuilder)1 MultiTenancyStrategy (org.hibernate.MultiTenancyStrategy)1