[DSRV-12] DSpace Configuration service reads in more configuration files - Commit for Kevin Van de Velde

git-svn-id: http://scm.dspace.org/svn/repo/modules/dspace-services/trunk@6188 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Mark Diggory
2011-03-24 22:39:05 +00:00
parent 1f8985fc2b
commit 540c8e9403
6 changed files with 122 additions and 52 deletions

View File

@@ -12,12 +12,11 @@
<url>http://www.dspace.org</url> <url>http://www.dspace.org</url>
</organization> </organization>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>2.0.3-SNAPSHOT</version>
<parent> <parent>
<artifactId>dspace-pom</artifactId> <artifactId>dspace-services</artifactId>
<groupId>org.dspace</groupId> <groupId>org.dspace</groupId>
<version>7</version> <version>2.0.3-SNAPSHOT</version>
</parent> </parent>
<!-- <!--

View File

@@ -11,12 +11,11 @@
<url>http://www.dspace.org</url> <url>http://www.dspace.org</url>
</organization> </organization>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>2.0.3-SNAPSHOT</version>
<parent> <parent>
<artifactId>dspace-pom</artifactId> <artifactId>dspace-services</artifactId>
<groupId>org.dspace</groupId> <groupId>org.dspace</groupId>
<version>7</version> <version>2.0.3-SNAPSHOT</version>
</parent> </parent>
<developers> <developers>

View File

@@ -7,19 +7,10 @@
*/ */
package org.dspace.servicemanager.config; package org.dspace.servicemanager.config;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.dspace.constants.Constants; import org.dspace.constants.Constants;
@@ -29,12 +20,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.SimpleTypeConverter; import org.springframework.beans.SimpleTypeConverter;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
/** /**
* The central DSpace configuration service. * The central DSpace configuration service.
* This is effectively immutable once the config has loaded. * This is effectively immutable once the config has loaded.
* *
* @author Aaron Zeckoski (azeckoski @ gmail.com) * @author Aaron Zeckoski (azeckoski @ gmail.com)
* @author Kevin Van de Velde (kevin at atmire dot com)
*/ */
public final class DSpaceConfigurationService implements ConfigurationService { public final class DSpaceConfigurationService implements ConfigurationService {
@@ -42,18 +36,20 @@ public final class DSpaceConfigurationService implements ConfigurationService {
public static final String DSPACE_WEB_CONTEXT_PARAM = "dspace-config"; public static final String DSPACE_WEB_CONTEXT_PARAM = "dspace-config";
public static final String DSPACE = "dspace"; public static final String DSPACE = "dspace";
public static final String DOT_CONFIG = ".cfg"; public static final String EXT_CONFIG = "cfg";
public static final String DOT_PROPERTIES = ".properties"; public static final String DOT_CONFIG = "." + EXT_CONFIG;
public static final String DSPACE_PREFIX = "dspace."; public static final String DSPACE_PREFIX = "dspace.";
public static final String DSPACE_HOME = DSPACE + ".dir"; public static final String DSPACE_HOME = DSPACE + ".dir";
public static final String DEFAULT_CONFIGURATION_FILE_NAME = "dspace-defaults.properties"; public static final String DEFAULT_CONFIGURATION_FILE_NAME = "dspace-defaults" + DOT_CONFIG;
public static final String DEFAULT_DSPACE_CONFIG_PATH = "config/" + DEFAULT_CONFIGURATION_FILE_NAME; public static final String DEFAULT_DSPACE_CONFIG_PATH = "config/" + DEFAULT_CONFIGURATION_FILE_NAME;
public static final String DSPACE_CONFIG_PATH = "config/" + DSPACE + DOT_PROPERTIES; public static final String DSPACE_CONFIG_PATH = "config/" + DSPACE + DOT_CONFIG;
public static final String LEGACY_DSPACE_CONFIG_PATH = "config/" + DSPACE + DOT_CONFIG;
public static final String DSPACE_MODULES_CONFIG_PATH = "config" + File.separator + "modules";
protected transient Map<String, Map<String, ServiceConfig>> serviceNameConfigs; protected transient Map<String, Map<String, ServiceConfig>> serviceNameConfigs;
public static final String DSPACE_CONFIG_ADDON = "dspace/config-*";
public DSpaceConfigurationService() { public DSpaceConfigurationService() {
// init and load up current config settings // init and load up current config settings
@@ -372,16 +368,59 @@ public final class DSpaceConfigurationService implements ConfigurationService {
// Collect values from all the properties files: the later ones loaded override settings from prior. // Collect values from all the properties files: the later ones loaded override settings from prior.
// read all the known files from the home path that are properties files
pushPropsToMap(configMap, readPropertyFile(homePath + File.separatorChar + LEGACY_DSPACE_CONFIG_PATH)); //Find any addon config files found in the config dir in our jars
pushPropsToMap(configMap, readPropertyFile(homePath + File.separatorChar + DSPACE_CONFIG_PATH)); try {
pushPropsToMap(configMap, readPropertyFile(homePath + "local" + DOT_PROPERTIES)); PathMatchingResourcePatternResolver patchMatcher = new PathMatchingResourcePatternResolver();
Resource[] resources = patchMatcher.getResources("classpath*:" + DSPACE_CONFIG_ADDON + DOT_CONFIG);
for (Resource resource : resources) {
String prefix = resource.getFilename().substring(0, resource.getFilename().lastIndexOf(".")).replaceFirst("config-", "");
pushPropsToMap(configMap, prefix, readPropertyStream(resource.getInputStream()));
}
}catch (Exception e){
e.printStackTrace();
}
//Attempt to load up all the config files in the modules directory
try{
File modulesDirectory = new File(homePath + File.separator + DSPACE_MODULES_CONFIG_PATH);
if(modulesDirectory.exists()){
try{
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("file:" + modulesDirectory.getAbsolutePath() + File.separator + "*" + DOT_CONFIG);
if(resources != null){
for(Resource resource : resources){
String prefix = resource.getFilename().substring(0, resource.getFilename().lastIndexOf("."));
pushPropsToMap(configMap, prefix, readPropertyStream(resource.getInputStream()));
}
}
}catch (IOException e){
log.error("Error while loading the modules properties from:" + modulesDirectory.getAbsolutePath());
}
}else{
log.info("Failed to load the modules properties since (" + homePath + File.separator + DSPACE_MODULES_CONFIG_PATH + "): Does not exist");
}
}catch (IllegalArgumentException e){
//This happens if we don't have a modules directory
log.error("Error while loading the module properties since (" + homePath + File.separator + DSPACE_MODULES_CONFIG_PATH + "): is not a valid directory", e);
}
// attempt to load from the current classloader also (works for commandline config sitting on classpath // attempt to load from the current classloader also (works for commandline config sitting on classpath
pushPropsToMap(configMap, readPropertyResource(DSPACE + DOT_CONFIG)); pushPropsToMap(configMap, readPropertyResource(DSPACE + DOT_CONFIG));
pushPropsToMap(configMap, readPropertyResource(DSPACE + DOT_PROPERTIES));
pushPropsToMap(configMap, readPropertyResource("local" + DOT_PROPERTIES)); // read all the known files from the home path that are properties files
pushPropsToMap(configMap, readPropertyResource("webapp" + DOT_PROPERTIES)); pushPropsToMap(configMap, readPropertyFile(homePath + File.separatorChar + DSPACE_CONFIG_PATH));
// pushPropsToMap(configMap, readPropertyFile(homePath + File.separatorChar + DSPACE_CONFIG_PATH));
// TODO: still use this local file loading?
// pushPropsToMap(configMap, readPropertyFile(homePath + File.separatorChar + "local" + DOT_PROPERTIES));
// pushPropsToMap(configMap, readPropertyResource(DSPACE + DOT_PROPERTIES));
// pushPropsToMap(configMap, readPropertyResource("local" + DOT_PROPERTIES));
// pushPropsToMap(configMap, readPropertyResource("webapp" + DOT_PROPERTIES));
// now push all of these into the config service store // now push all of these into the config service store
loadConfiguration(configMap, true); loadConfiguration(configMap, true);
@@ -515,9 +554,43 @@ public final class DSpaceConfigurationService implements ConfigurationService {
return props; return props;
} }
protected Properties readPropertyFile(File propertyFile) {
Properties props = new Properties();
try{
if(propertyFile.exists()){
props.load(new FileInputStream(propertyFile));
log.info("Loaded"+props.size() + " config properties from file: " + propertyFile.getName());
System.out.println("Loaded"+props.size() + " config properties from file: " + propertyFile.getName());
}
} catch (Exception e){
log.warn("Failed to load config properties from file (" + propertyFile.getName() + ": "+ e.getMessage(), e);
}
return props;
}
protected Properties readPropertyStream(InputStream propertyStream){
Properties props = new Properties();
try{
props.load(propertyStream);
log.info("Loaded"+props.size() + " config properties from stream");
} catch (Exception e){
log.warn("Failed to load config properties from stream: " + e.getMessage(), e);
}
return props;
}
protected void pushPropsToMap(Map<String, String> map, Properties props) { protected void pushPropsToMap(Map<String, String> map, Properties props) {
pushPropsToMap(map, null, props);
}
protected void pushPropsToMap(Map<String, String> map, String prefix, Properties props) {
for (Entry<Object, Object> entry : props.entrySet()) { for (Entry<Object, Object> entry : props.entrySet()) {
map.put(entry.getKey().toString(), entry.getValue() == null ? "" : entry.getValue().toString()); String key = entry.getKey().toString();
if(prefix != null){
key = prefix + "." + key;
}
map.put(key, entry.getValue() == null ? "" : entry.getValue().toString());
} }
} }

View File

@@ -57,11 +57,11 @@ public final class DSpaceKernelServletContextListener implements ServletContextL
if (providedHome == null) if (providedHome == null)
{ {
String dspaceHome = arg0.getServletContext().getInitParameter(DSpaceConfigurationService.DSPACE_WEB_CONTEXT_PARAM); String dspaceHome = arg0.getServletContext().getInitParameter(DSpaceConfigurationService.DSPACE_HOME);
if(dspaceHome != null && !dspaceHome.equals("") && if(dspaceHome != null && !dspaceHome.equals("") &&
!dspaceHome.equals("${" + DSpaceConfigurationService.DSPACE_HOME + "}")){ !dspaceHome.equals("${" + DSpaceConfigurationService.DSPACE_HOME + "}")){
File test = new File(dspaceHome); File test = new File(dspaceHome);
if(test.exists() && new File(test,DSpaceConfigurationService.LEGACY_DSPACE_CONFIG_PATH).exists()) { if(test.exists() && new File(test,DSpaceConfigurationService.DSPACE_CONFIG_PATH).exists()) {
providedHome = dspaceHome; providedHome = dspaceHome;
} }
} }

View File

@@ -11,12 +11,11 @@
<url>http://www.dspace.org</url> <url>http://www.dspace.org</url>
</organization> </organization>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>2.0.3-SNAPSHOT</version>
<parent> <parent>
<artifactId>dspace-pom</artifactId> <artifactId>dspace-services</artifactId>
<groupId>org.dspace</groupId> <groupId>org.dspace</groupId>
<version>7</version> <version>2.0.3-SNAPSHOT</version>
</parent> </parent>