Java tutorial
/** * Copyright 2013 Stockholm County Council * * This file is part of APIGW * * APIGW is free software; you can redistribute it and/or modify * it under the terms of version 2.1 of the GNU Lesser General Public * License as published by the Free Software Foundation. * * APIGW is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with APIGW; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ package org.apigw.monitoring.config; import java.util.Properties; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.flywaydb.core.Flyway; import org.flywaydb.core.api.MigrationVersion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver; import org.springframework.orm.hibernate4.HibernateExceptionTranslator; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableTransactionManagement @EnableJpaRepositories("org.apigw.monitoring.svc.repository") public class PersistenceConfig { private static final Logger log = LoggerFactory.getLogger(PersistenceConfig.class); @Value("${monitoring.db.username}") private String username; @Value("${monitoring.db.password}") private String password; @Value("${monitoring.db.url}") private String url; @Value("${monitoring.db.driver}") private String driver; @Value("${monitoring.db.validationQuery}") private String validationQuery; @Value("${monitoring.db.dialect}") private String hibernateDialect; @Value("${monitoring.db.showSql:false}") private String hibernateShowSql; @Bean public PlatformTransactionManager transactionManager() { log.debug("Setting up transactionManager"); EntityManagerFactory factory = entityManagerFactory().getObject(); return new JpaTransactionManager(factory); } @Bean @DependsOn("flyway") public LocalContainerEntityManagerFactoryBean entityManagerFactory() { log.debug("Setting up entityManagerFactory"); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setShowSql(Boolean.getBoolean(hibernateShowSql)); vendorAdapter.setDatabasePlatform("org.hibernate.dialect." + hibernateDialect); factory.setDataSource(dataSource()); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("org.apigw.monitoring.types.domain"); Properties jpaProperties = new Properties(); factory.setJpaProperties(jpaProperties); factory.afterPropertiesSet(); factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver()); return factory; } @Bean public HibernateExceptionTranslator hibernateExceptionTranslator() { log.debug("Setting up HibernateExceptionTranslator"); return new HibernateExceptionTranslator(); } @Bean public DataSource dataSource() { log.debug("Setting up datasource for with drive: {}, url: {}, username: {}", driver, url, username); BasicDataSource bdc = new BasicDataSource(); bdc.setUsername(username); bdc.setPassword(password); bdc.setUrl(url); bdc.setDriverClassName(driver); bdc.setValidationQuery(validationQuery); return bdc; } @Bean(initMethod = "migrate") public Flyway flyway() { log.debug("Setting up Flyway"); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource()); flyway.setTable("monitoring_schema_version"); flyway.setInitOnMigrate(true); flyway.setLocations("db.migration." + hibernateDialect); flyway.setInitVersion(MigrationVersion.fromVersion("0.0.0")); return flyway; } }