diff --git a/build.gradle b/build.gradle index 63ac93f199ff13d7dc16e4fc00fa1279e9cf75b0..0d0a783917c38a4eed3eb0921b2142de2f4fd8ac 100644 --- a/build.gradle +++ b/build.gradle @@ -105,6 +105,8 @@ configurations { all*.exclude group: 'avalon-logkit' all*.exclude group: 'com.metaparadigm' 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-instrument' all*.exclude group: 'excalibur-logger' @@ -221,6 +223,7 @@ dependencies { compile "org.json:json:20151123" compile "xerces:xercesImpl:2.11.0" + compile "commons-beanutils:commons-beanutils:1.9.2" compile "commons-codec:commons-codec:1.10" compile "commons-collections:commons-collections:3.2.2" compile "commons-configuration:commons-configuration:1.10" @@ -228,7 +231,6 @@ dependencies { compile "commons-discovery:commons-discovery:0.5" compile "commons-fileupload:commons-fileupload:1.3.1" 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-math:2.2" diff --git a/src/main/java/org/nrg/xnat/configuration/DatabaseConfig.java b/src/main/java/org/nrg/xnat/configuration/DatabaseConfig.java index 6fe2217cba7ecb1777d277aadc1cc97c5a252d05..8fb7d5a1b0c11ff86d4be07dd694abba036c544b 100644 --- a/src/main/java/org/nrg/xnat/configuration/DatabaseConfig.java +++ b/src/main/java/org/nrg/xnat/configuration/DatabaseConfig.java @@ -1,40 +1,99 @@ package org.nrg.xnat.configuration; -import org.apache.commons.dbcp.BasicDataSource; -import org.springframework.beans.factory.annotation.Value; +import org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory; +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.Configuration; +import org.springframework.core.env.Environment; 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 java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.Properties; /** * Sets up the database configuration for XNAT. */ @Configuration +@EnableTransactionManagement(proxyTargetClass = true) public class DatabaseConfig { - @Bean - public DataSource dataSource() { - final BasicDataSource dataSource = new BasicDataSource(); - dataSource.setDriverClassName(_driver); - dataSource.setUrl(_url); - dataSource.setUsername(_username); - dataSource.setPassword(_password); - return dataSource; + public DataSource dataSource() throws NrgServiceException { + final Properties properties = Beans.getNamespacedProperties(_environment, "datasource", true); + final String dataSourceClassName = properties.getProperty("class", SimpleDriverDataSource.class.getName()); + try { + final Class<? extends DataSource> clazz = Class.forName(dataSourceClassName).asSubclass(DataSource.class); + if (properties.containsKey("driver")) { + 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 - public JdbcTemplate jdbcTemplate() { + public JdbcTemplate jdbcTemplate() throws NrgServiceException { return new JdbcTemplate(dataSource()); } - @Value("${datasource.driver}") - private String _driver; - @Value("${datasource.url}") - private String _url; - @Value("${datasource.username}") - private String _username; - @Value("${datasource.password}") - private String _password; + @Bean + public ImprovedNamingStrategy namingStrategy() { + return new PrefixedTableNamingStrategy("xhbm"); + } + + @Bean + public PropertiesFactoryBean hibernateProperties() { + 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; } diff --git a/src/main/java/org/nrg/xnat/configuration/PropertiesConfig.java b/src/main/java/org/nrg/xnat/configuration/PropertiesConfig.java index 4e54061a2e675b1d62ac93541484d8a70b486c45..b2e133d440606f1ab92e082f5ad726d9841a9f9d 100644 --- a/src/main/java/org/nrg/xnat/configuration/PropertiesConfig.java +++ b/src/main/java/org/nrg/xnat/configuration/PropertiesConfig.java @@ -25,9 +25,6 @@ import java.util.List; */ @Configuration @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.config.home}/services.properties", ignoreResourceNotFound = true), @PropertySource(value = "file:${xnat.config}", ignoreResourceNotFound = true)}) diff --git a/src/main/java/org/nrg/xnat/initialization/RootConfig.java b/src/main/java/org/nrg/xnat/initialization/RootConfig.java index e00af60cadb2d0b2c1a06a006ed0eeae76ede0ec..6ae818e422bad777c60226fdf8bef428a53179b9 100644 --- a/src/main/java/org/nrg/xnat/initialization/RootConfig.java +++ b/src/main/java/org/nrg/xnat/initialization/RootConfig.java @@ -36,11 +36,11 @@ import java.util.List; "org.nrg.prefs.repositories", "org.nrg.prefs.services.impl.hibernate", "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 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"); + 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"); @Bean public String siteId() { diff --git a/src/main/webapp/WEB-INF/conf/orm-config.xml b/src/main/webapp/WEB-INF/conf/orm-config.xml deleted file mode 100644 index 4bea2ff4e64029bfaef813aca208f481a8ff63af..0000000000000000000000000000000000000000 --- a/src/main/webapp/WEB-INF/conf/orm-config.xml +++ /dev/null @@ -1,47 +0,0 @@ -<?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> diff --git a/src/main/webapp/WEB-INF/conf/services.properties b/src/main/webapp/WEB-INF/conf/services.properties index 17081f1e259c05cd75b9755771a3583cc46d0e9d..de1d5c7fac4e77dbee428cf9994d40c80eea32e3 100644 --- a/src/main/webapp/WEB-INF/conf/services.properties +++ b/src/main/webapp/WEB-INF/conf/services.properties @@ -16,6 +16,12 @@ datasource.url=jdbc:postgresql://localhost/xnat datasource.username=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.port=25 mailserver.username=