/*
* Copyright 2013 the original author or authors.
*
* 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 com.gopivotal.cla.repository;
import java.net.URI;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.jasypt.encryption.pbe.PBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.hibernate4.encryptor.HibernatePBEEncryptorRegistry;
import org.postgresql.Driver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
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;
import com.googlecode.flyway.core.Flyway;
import com.jolbox.bonecp.BoneCPDataSource;
/**
* Configuration of repository components
*/
@Configuration
@EnableJpaRepositories("com.gopivotal.cla.repository")
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
public class RepositoryConfiguration {
@Autowired
private volatile String databaseUrl;
@Bean
DataSource dataSource() {
URI dbUri = URI.create(this.databaseUrl);
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();
BoneCPDataSource dataSource = new BoneCPDataSource();
dataSource.setDriverClass(Driver.class.getCanonicalName());
dataSource.setJdbcUrl(dbUrl);
String[] userInfoTokens = dbUri.getUserInfo().split(":");
dataSource.setUsername(userInfoTokens.length > 0 ? userInfoTokens[0] : "");
dataSource.setPassword(userInfoTokens.length > 1 ? userInfoTokens[1] : "");
dataSource.setMaxConnectionsPerPartition(2);
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
flyway.setLocations("META-INF/db/migration");
flyway.migrate();
return dataSource;
}
@Bean
EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}
@Bean
public PersistenceExceptionTranslator persistenceExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean
PBEStringEncryptor stringEncryptor(String encryptionKey) {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setAlgorithm("PBEWithMD5AndDES");
encryptor.setKeyObtentionIterations(1000);
encryptor.setPassword(encryptionKey);
HibernatePBEEncryptorRegistry.getInstance().registerPBEStringEncryptor("basicStringEncryptor", encryptor);
return encryptor;
}
}