Skip to content
Snippets Groups Projects
Commit d024bd44 authored by Rick Herrick's avatar Rick Herrick
Browse files

Made data source and Hibernate configurations populate from...

Made data source and Hibernate configurations populate from services.properties. Moved ORM configuration into DatabaseConfig and removed XML configuration file.
parent 8eef92e1
No related branches found
No related tags found
No related merge requests found
...@@ -105,6 +105,8 @@ configurations { ...@@ -105,6 +105,8 @@ configurations {
all*.exclude group: 'avalon-logkit' all*.exclude group: 'avalon-logkit'
all*.exclude group: 'com.metaparadigm' all*.exclude group: 'com.metaparadigm'
all*.exclude group: 'com.sun.mail' all*.exclude group: 'com.sun.mail'
all*.exclude group: 'commons-dbcp'
all*.exclude group: 'commons-pool'
all*.exclude group: 'excalibur-component' all*.exclude group: 'excalibur-component'
all*.exclude group: 'excalibur-instrument' all*.exclude group: 'excalibur-instrument'
all*.exclude group: 'excalibur-logger' all*.exclude group: 'excalibur-logger'
...@@ -221,6 +223,7 @@ dependencies { ...@@ -221,6 +223,7 @@ dependencies {
compile "org.json:json:20151123" compile "org.json:json:20151123"
compile "xerces:xercesImpl:2.11.0" compile "xerces:xercesImpl:2.11.0"
compile "commons-beanutils:commons-beanutils:1.9.2"
compile "commons-codec:commons-codec:1.10" compile "commons-codec:commons-codec:1.10"
compile "commons-collections:commons-collections:3.2.2" compile "commons-collections:commons-collections:3.2.2"
compile "commons-configuration:commons-configuration:1.10" compile "commons-configuration:commons-configuration:1.10"
...@@ -228,7 +231,6 @@ dependencies { ...@@ -228,7 +231,6 @@ dependencies {
compile "commons-discovery:commons-discovery:0.5" compile "commons-discovery:commons-discovery:0.5"
compile "commons-fileupload:commons-fileupload:1.3.1" compile "commons-fileupload:commons-fileupload:1.3.1"
compile "commons-net:commons-net:3.4" compile "commons-net:commons-net:3.4"
compile "commons-pool:commons-pool:1.6"
compile "org.apache.commons:commons-email:1.4" compile "org.apache.commons:commons-email:1.4"
compile "org.apache.commons:commons-math:2.2" compile "org.apache.commons:commons-math:2.2"
......
package org.nrg.xnat.configuration; package org.nrg.xnat.configuration;
import org.apache.commons.dbcp.BasicDataSource; import org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory;
import org.springframework.beans.factory.annotation.Value; import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.ImprovedNamingStrategy;
import org.nrg.framework.exceptions.NrgServiceError;
import org.nrg.framework.exceptions.NrgServiceException;
import org.nrg.framework.orm.hibernate.AggregatedAnnotationSessionFactoryBean;
import org.nrg.framework.orm.hibernate.PrefixedTableNamingStrategy;
import org.nrg.framework.utilities.Beans;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.inject.Inject;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
/** /**
* Sets up the database configuration for XNAT. * Sets up the database configuration for XNAT.
*/ */
@Configuration @Configuration
@EnableTransactionManagement(proxyTargetClass = true)
public class DatabaseConfig { public class DatabaseConfig {
@Bean @Bean
public DataSource dataSource() { public DataSource dataSource() throws NrgServiceException {
final BasicDataSource dataSource = new BasicDataSource(); final Properties properties = Beans.getNamespacedProperties(_environment, "datasource", true);
dataSource.setDriverClassName(_driver); final String dataSourceClassName = properties.getProperty("class", SimpleDriverDataSource.class.getName());
dataSource.setUrl(_url); try {
dataSource.setUsername(_username); final Class<? extends DataSource> clazz = Class.forName(dataSourceClassName).asSubclass(DataSource.class);
dataSource.setPassword(_password); if (properties.containsKey("driver")) {
return dataSource; final String driver = (String) properties.get("driver");
properties.put("driver", Class.forName(driver).newInstance());
}
return Beans.getInitializedBean(properties, clazz);
} catch (ClassNotFoundException e) {
throw new NrgServiceException(NrgServiceError.ConfigurationError, "Couldn't find the specified data-source class name: " + dataSourceClassName);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new NrgServiceException(NrgServiceError.ConfigurationError, "An error occurred trying to access a property in the specified data-source class: " + dataSourceClassName, e);
}
} }
@Bean @Bean
public JdbcTemplate jdbcTemplate() { public JdbcTemplate jdbcTemplate() throws NrgServiceException {
return new JdbcTemplate(dataSource()); return new JdbcTemplate(dataSource());
} }
@Value("${datasource.driver}") @Bean
private String _driver; public ImprovedNamingStrategy namingStrategy() {
@Value("${datasource.url}") return new PrefixedTableNamingStrategy("xhbm");
private String _url; }
@Value("${datasource.username}")
private String _username; @Bean
@Value("${datasource.password}") public PropertiesFactoryBean hibernateProperties() {
private String _password; final PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setProperties(Beans.getNamespacedProperties(_environment, "hibernate", false));
return bean;
}
@Bean
public RegionFactory regionFactory() throws NrgServiceException {
try {
return new SingletonEhCacheRegionFactory(hibernateProperties().getObject());
} catch (IOException e) {
throw new NrgServiceException(NrgServiceError.Unknown, "An error occurred trying to retrieve the Hibernate properties", e);
}
}
@Bean
public LocalSessionFactoryBean sessionFactory() throws NrgServiceException {
try {
final AggregatedAnnotationSessionFactoryBean bean = new AggregatedAnnotationSessionFactoryBean();
bean.setDataSource(dataSource());
bean.setCacheRegionFactory(regionFactory());
bean.setHibernateProperties(hibernateProperties().getObject());
bean.setNamingStrategy(namingStrategy());
return bean;
} catch (IOException e) {
throw new NrgServiceException(NrgServiceError.Unknown, "An error occurred trying to retrieve the Hibernate properties", e);
}
}
@Bean
public PlatformTransactionManager transactionManager() throws NrgServiceException {
return new HibernateTransactionManager(sessionFactory().getObject());
}
@Inject
private Environment _environment;
} }
...@@ -25,9 +25,6 @@ import java.util.List; ...@@ -25,9 +25,6 @@ import java.util.List;
*/ */
@Configuration @Configuration
@PropertySources({ @PropertySources({
@PropertySource(value = "file:${HOME}/config/services.properties", ignoreResourceNotFound = true),
@PropertySource(value = "file:${HOME}/xnat/config/services.properties", ignoreResourceNotFound = true),
@PropertySource(value = "file:${XNAT_HOME}/config/services.properties", ignoreResourceNotFound = true),
@PropertySource(value = "file:${xnat.home}/config/services.properties", ignoreResourceNotFound = true), @PropertySource(value = "file:${xnat.home}/config/services.properties", ignoreResourceNotFound = true),
@PropertySource(value = "file:${xnat.config.home}/services.properties", ignoreResourceNotFound = true), @PropertySource(value = "file:${xnat.config.home}/services.properties", ignoreResourceNotFound = true),
@PropertySource(value = "file:${xnat.config}", ignoreResourceNotFound = true)}) @PropertySource(value = "file:${xnat.config}", ignoreResourceNotFound = true)})
......
...@@ -36,11 +36,11 @@ import java.util.List; ...@@ -36,11 +36,11 @@ import java.util.List;
"org.nrg.prefs.repositories", "org.nrg.prefs.repositories",
"org.nrg.prefs.services.impl.hibernate", "org.nrg.prefs.services.impl.hibernate",
"org.nrg.dicomtools.filters"}) "org.nrg.dicomtools.filters"})
@ImportResource({"WEB-INF/conf/xnat-security.xml", "WEB-INF/conf/orm-config.xml", "WEB-INF/conf/mq-context.xml"}) @ImportResource({"WEB-INF/conf/xnat-security.xml", "WEB-INF/conf/mq-context.xml"})
public class RootConfig { public class RootConfig {
public static final List<String> DEFAULT_ENTITY_PACKAGES = Arrays.asList("org.nrg.framework.datacache", "org.nrg.xft.entities", "org.nrg.xdat.entities", private static final List<String> DEFAULT_ENTITY_PACKAGES = Arrays.asList("org.nrg.framework.datacache", "org.nrg.xft.entities", "org.nrg.xdat.entities",
"org.nrg.xnat.entities", "org.nrg.prefs.entities", "org.nrg.config.entities"); "org.nrg.xnat.entities", "org.nrg.prefs.entities", "org.nrg.config.entities");
@Bean @Bean
public String siteId() { public String siteId() {
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ D:/Development/XNAT/1.6/xnat_builder_1_6dev/plugin-resources/conf/orm-config.xml
~ XNAT http://www.xnat.org
~ Copyright (c) 2014, Washington University School of Medicine
~ All Rights Reserved
~
~ Released under the Simplified BSD.
~
~ Last modified 2/7/14 12:19 PM
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Used the prefixed table naming strategy, class FooBar gives you prefix_foo_bar instead of FooBar. -->
<bean id="namingStrategy" class="org.nrg.framework.orm.hibernate.PrefixedTableNamingStrategy" p:prefix="xhbm" />
<!-- Set up default hibernate properties. -->
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
<bean id="regionFactory" class="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory">
<constructor-arg index="0" ref="hibernateProperties" />
</bean>
<bean id="sessionFactory" class="org.nrg.framework.orm.hibernate.AggregatedAnnotationSessionFactoryBean"
p:cacheRegionFactory-ref="regionFactory" p:dataSource-ref="dataSource"
p:hibernateProperties-ref="hibernateProperties" p:namingStrategy-ref="namingStrategy" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
</beans>
...@@ -16,6 +16,12 @@ datasource.url=jdbc:postgresql://localhost/xnat ...@@ -16,6 +16,12 @@ datasource.url=jdbc:postgresql://localhost/xnat
datasource.username=xnat datasource.username=xnat
datasource.password=xnat datasource.password=xnat
hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
mailserver.host=mail.server mailserver.host=mail.server
mailserver.port=25 mailserver.port=25
mailserver.username= mailserver.username=
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment