diff --git a/build.gradle b/build.gradle index 2e3efa7c6772f8a3c2d074fd0bdc1ab303023f74..74c34eaf6d5920a65ce0de8bd5fb0c1d29f0d065 100644 --- a/build.gradle +++ b/build.gradle @@ -170,6 +170,7 @@ publishing { } } repositories { + mavenLocal() maven { credentials { // These properties must be set in the ~/.gradle/gradle.properties file or passed on the Gradle command diff --git a/src/main/java/org/nrg/xnat/configuration/DatabaseConfig.java b/src/main/java/org/nrg/xnat/configuration/DatabaseConfig.java index 8fb7d5a1b0c11ff86d4be07dd694abba036c544b..ea308773aca7781a0fa2bf0a3de440bc86dfa2ca 100644 --- a/src/main/java/org/nrg/xnat/configuration/DatabaseConfig.java +++ b/src/main/java/org/nrg/xnat/configuration/DatabaseConfig.java @@ -8,6 +8,9 @@ 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.postgresql.Driver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -31,17 +34,29 @@ import java.util.Properties; @Configuration @EnableTransactionManagement(proxyTargetClass = true) public class DatabaseConfig { + + public static final String DEFAULT_DATASOURCE_URL = "jdbc:postgresql://localhost/xnat"; + public static final String DEFAULT_DATASOURCE_USERNAME = "xnat"; + public static final String DEFAULT_DATASOURCE_PASSWORD = "xnat"; + public static final String DEFAULT_DATASOURCE_CLASS = SimpleDriverDataSource.class.getName(); + public static final String DEFAULT_DATASOURCE_DRIVER = Driver.class.getName(); + @Bean public DataSource dataSource() throws NrgServiceException { final Properties properties = Beans.getNamespacedProperties(_environment, "datasource", true); - final String dataSourceClassName = properties.getProperty("class", SimpleDriverDataSource.class.getName()); + setDefaultDatasourceProperties(properties); + final String dataSourceClassName = properties.getProperty("class"); try { - final Class<? extends DataSource> clazz = Class.forName(dataSourceClassName).asSubclass(DataSource.class); + final Class<? extends DataSource> dataSourceClazz = Class.forName(dataSourceClassName).asSubclass(DataSource.class); if (properties.containsKey("driver")) { - final String driver = (String) properties.get("driver"); - properties.put("driver", Class.forName(driver).newInstance()); + final String driver = properties.getProperty("driver"); + try { + properties.put("driver", Class.forName(driver).newInstance()); + } catch (ClassNotFoundException e) { + throw new NrgServiceException(NrgServiceError.ConfigurationError, "Couldn't find the specified JDBC driver class name: " + driver); + } } - return Beans.getInitializedBean(properties, clazz); + return Beans.getInitializedBean(properties, dataSourceClazz); } catch (ClassNotFoundException e) { throw new NrgServiceException(NrgServiceError.ConfigurationError, "Couldn't find the specified data-source class name: " + dataSourceClassName); } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { @@ -62,7 +77,11 @@ public class DatabaseConfig { @Bean public PropertiesFactoryBean hibernateProperties() { final PropertiesFactoryBean bean = new PropertiesFactoryBean(); - bean.setProperties(Beans.getNamespacedProperties(_environment, "hibernate", false)); + final Properties properties = Beans.getNamespacedProperties(_environment, "hibernate", false); + if (properties.size() == 0) { + properties.putAll(DEFAULT_HIBERNATE_PROPERTIES); + } + bean.setProperties(properties); return bean; } @@ -94,6 +113,51 @@ public class DatabaseConfig { return new HibernateTransactionManager(sessionFactory().getObject()); } + private static Properties setDefaultDatasourceProperties(final Properties properties) { + // Configure some defaults if they're not already set. + if (!properties.containsKey("class")) { + if (_log.isWarnEnabled()) { + _log.warn("No value set for the XNAT datasource class, using the default value of " + DEFAULT_DATASOURCE_CLASS); + } + properties.setProperty("class", DEFAULT_DATASOURCE_CLASS); + } + if (!properties.containsKey("driver")) { + if (_log.isWarnEnabled()) { + _log.warn("No value set for the XNAT datasource driver, using the default value of " + DEFAULT_DATASOURCE_DRIVER); + } + properties.setProperty("driver", DEFAULT_DATASOURCE_DRIVER); + } + if (!properties.containsKey("url")) { + if (_log.isWarnEnabled()) { + _log.warn("No value set for the XNAT datasource URL, using the default value of " + DEFAULT_DATASOURCE_URL); + } + properties.setProperty("url", DEFAULT_DATASOURCE_URL); + } + if (!properties.containsKey("username")) { + if (_log.isWarnEnabled()) { + _log.warn("No value set for the XNAT datasource username, using the default value of " + DEFAULT_DATASOURCE_USERNAME + ". Note that you can set the username to an empty value if you really need an empty string."); + } + properties.setProperty("username", DEFAULT_DATASOURCE_USERNAME); + } + if (!properties.containsKey("password")) { + if (_log.isWarnEnabled()) { + _log.warn("No value set for the XNAT datasource password, using the default value of " + DEFAULT_DATASOURCE_PASSWORD + ". Note that you can set the password to an empty value if you really need an empty string."); + } + properties.setProperty("password", DEFAULT_DATASOURCE_PASSWORD); + } + return properties; + } + + private static final Logger _log = LoggerFactory.getLogger(DatabaseConfig.class); + + private static final Properties DEFAULT_HIBERNATE_PROPERTIES = new Properties() {{ + setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect"); + setProperty("hibernate.hbm2ddl.auto", "update"); + setProperty("hibernate.show_sql", "false"); + setProperty("hibernate.cache.use_second_level_cache", "true"); + setProperty("hibernate.cache.use_query_cache", "true"); + }}; + @Inject private Environment _environment; } diff --git a/src/main/java/org/nrg/xnat/configuration/SpawnerConfig.java b/src/main/java/org/nrg/xnat/configuration/SpawnerConfig.java deleted file mode 100644 index 93f2d7afcef20a7354af68cf67573b9e31e5d66c..0000000000000000000000000000000000000000 --- a/src/main/java/org/nrg/xnat/configuration/SpawnerConfig.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.nrg.xnat.configuration; - -import org.nrg.framework.orm.hibernate.HibernateEntityPackageList; -import org.nrg.xnat.spawner.services.SpawnerResourceLocator; -import org.nrg.xnat.spawner.services.impl.SpawnerWorker; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -import java.util.Collections; - -@Configuration -@ComponentScan({"org.nrg.xnat.spawner.controllers", "org.nrg.xnat.spawner.services.impl.hibernate", "org.nrg.xnat.spawner.repositories"}) -public class SpawnerConfig { - @Bean - public SpawnerWorker spawnerWorker() { - return new SpawnerWorker(); - } - - @Bean - public HibernateEntityPackageList spawnerEntityPackages() { - return new HibernateEntityPackageList(Collections.singletonList("org.nrg.xnat.spawner.entities")); - } - - @Bean - public SpawnerResourceLocator spawnerResourceLocator() { - // TODO: This uses the default spawner element pattern. It would be nice to set this as a site configuration property. - return new SpawnerResourceLocator(); - } -} diff --git a/src/main/java/org/nrg/xnat/configuration/WebConfig.java b/src/main/java/org/nrg/xnat/configuration/WebConfig.java index 297f294b6788fef581d7ed4614a821864a1a298c..b6bcb890a39819f8492dade1c4dc5573154bde16 100644 --- a/src/main/java/org/nrg/xnat/configuration/WebConfig.java +++ b/src/main/java/org/nrg/xnat/configuration/WebConfig.java @@ -1,12 +1,14 @@ package org.nrg.xnat.configuration; import org.nrg.framework.annotations.XapiRestController; +import org.nrg.xnat.spawner.configuration.SpawnerConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.support.StandardServletMultipartResolver; @@ -26,7 +28,8 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableWebMvc @EnableSwagger2 -@ComponentScan("org.nrg.xapi.rest") +@Import(SpawnerConfig.class) +@ComponentScan({"org.nrg.xapi.rest", "org.nrg.xnat.spawner.controllers"}) public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { @@ -58,7 +61,7 @@ public class WebConfig extends WebMvcConfigurerAdapter { @Bean public Docket api() { _log.debug("Initializing the Swagger Docket object"); - return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withClassAnnotation(XapiRestController.class)).paths(PathSelectors.any()).build().apiInfo(apiInfo()); + return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withClassAnnotation(XapiRestController.class)).paths(PathSelectors.any()).build().apiInfo(apiInfo()).pathMapping("/xapi"); } private ApiInfo apiInfo() {