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

Added fail-safe defaults to DatabaseConfig. Removed SpawnerConfig but added...

Added fail-safe defaults to DatabaseConfig. Removed SpawnerConfig but added reference to spawner library SpawnerConfig to WebConfig. Added /xapi path to Swagger configuration. Added mavenLocal() to publishing repositories in build file.
parent ad221254
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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;
}
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();
}
}
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() {
......
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