eu.supersede.fe.multitenant.MultiTenancyJpaConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for eu.supersede.fe.multitenant.MultiTenancyJpaConfiguration.java

Source

/*
   (C) Copyright 2015-2018 The SUPERSEDE Project Consortium
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package eu.supersede.fe.multitenant;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.sql.DataSource;

import org.hibernate.MultiTenancyStrategy;
import org.hibernate.cfg.Environment;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

@Configuration
@PropertySource("classpath:wp5_application.properties")
@EnableConfigurationProperties(JpaProperties.class)
public class MultiTenancyJpaConfiguration {
    private final static String HBM2DDL_AUTO_PROPERTY = "application.multitenancy.hibernate.hbm2ddl.auto";

    @Value("${application.multitenancy.models.packages}")
    private String MODELS_PACKAGES;

    @Autowired(required = false)
    private DataSource dataSource;

    @Autowired
    private JpaProperties jpaProperties;

    @Autowired
    private MultiTenantConnectionProvider multiTenantConnectionProvider;

    @Autowired
    private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;

    @Autowired
    private org.springframework.core.env.Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        if (dataSource == null) {
            return null;
        }

        Map<String, Object> hibernateProps = new LinkedHashMap<>();
        hibernateProps.putAll(jpaProperties.getHibernateProperties(dataSource));

        hibernateProps.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
        hibernateProps.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
        hibernateProps.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolver);
        hibernateProps.put(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");

        String HBM2DDL_AUTO = env.getProperty(HBM2DDL_AUTO_PROPERTY);

        if (HBM2DDL_AUTO != null) {
            hibernateProps.put(Environment.HBM2DDL_AUTO, HBM2DDL_AUTO);
        }

        Set<String> packages = new HashSet<>();
        String[] tmp = MODELS_PACKAGES.split(",");

        for (String t : tmp) {
            packages.add(t.trim());
        }

        packages.add("eu.supersede.fe.notification.model");

        return builder.dataSource(dataSource).packages(packages.toArray(new String[packages.size()]))
                .properties(hibernateProps).jta(false).build();
    }
}