From: <fg...@us...> - 2007-01-27 19:44:34
|
Revision: 150 http://svn.sourceforge.net/openutils/?rev=150&view=rev Author: fgiust Date: 2007-01-27 11:44:34 -0800 (Sat, 27 Jan 2007) Log Message: ----------- make propertyConfigurer more configurable Modified Paths: -------------- trunk/openutils-deployment/pom.xml trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java Modified: trunk/openutils-deployment/pom.xml =================================================================== --- trunk/openutils-deployment/pom.xml 2007-01-05 16:55:48 UTC (rev 149) +++ trunk/openutils-deployment/pom.xml 2007-01-27 19:44:34 UTC (rev 150) @@ -1,4 +1,5 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>net.sourceforge.openutils</groupId> @@ -17,17 +18,6 @@ <version>2.2</version> </dependency> <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.0.1</version> - </dependency> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.13</version> - <optional>true</optional> - </dependency> - <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>2.0.1</version> @@ -48,6 +38,11 @@ </exclusions> </dependency> <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.2</version> + </dependency> + <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> @@ -60,11 +55,5 @@ <version>3.8.1</version> <scope>test</scope> </dependency> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.13</version> - <scope>test</scope> - </dependency> </dependencies> </project> \ No newline at end of file Modified: trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2007-01-05 16:55:48 UTC (rev 149) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2007-01-27 19:44:34 UTC (rev 150) @@ -1,6 +1,6 @@ package it.openutils.deployment.spring; -import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.net.InetAddress; import java.net.MalformedURLException; @@ -17,6 +17,7 @@ import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; +import org.springframework.util.ResourceUtils; import org.springframework.web.context.ServletContextAware; @@ -27,18 +28,32 @@ public class EnvironmentPropertyConfigurer extends PropertyPlaceholderConfigurer implements ServletContextAware { - private String fileLocation; + /** + * Application name (webapp name) variable. + */ + private static final String PROPERTY_APPL = "${appl}"; - private String defaultEnvironment; + /** + * Environment (server name) variable. + */ + private static final String PROPERTY_ENV = "${env}"; - private ServletContext servletContext; - /** * Logger. */ private static Logger log = LoggerFactory.getLogger(EnvironmentPropertyConfigurer.class); /** + * @deprecated use defaultLocation + */ + @Deprecated + private String defaultEnvironment; + + private ServletContext servletContext; + + private String fileLocation; + + /** * {@inheritDoc} */ public void setServletContext(ServletContext servletContext) @@ -58,19 +73,75 @@ /** * Setter for <code>defaultEnvironment</code>. * @param defaultEnvironment The defaultEnvironment to set. + * @deprecated use defaultLocation */ + @Deprecated public void setDefaultEnvironment(String defaultEnvironment) { this.defaultEnvironment = defaultEnvironment; } - private String getRootPath() + /** + * {@inheritDoc} + */ + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { - if (servletContext != null) + if (fileLocation != null) { - return servletContext.getRealPath("/"); + + String hostname = null; + try + { + hostname = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()); + } + catch (UnknownHostException e) + { + log.error(e.getMessage()); // should not happen + } + + System.setProperty("env", hostname); + + String applName = getApplicationName(); + System.setProperty("appl", applName); + + URL propertyUrl = null; + + String replacedLocations = StringUtils.replace(fileLocation, PROPERTY_ENV, hostname); + replacedLocations = StringUtils.replace(replacedLocations, PROPERTY_APPL, applName); + + String[] locations = StringUtils.split(replacedLocations, ","); + + for (String loc : locations) + { + propertyUrl = getResource(StringUtils.strip(loc)); + if (propertyUrl != null) + { + break; + } + log.debug("Property file not found at {}", loc); + } + + if (propertyUrl == null && defaultEnvironment != null) + { + log.warn("Usage of \"defaultEnvironment\" is deprecated, please specify the fallback location " + + "as the last comma separated value in \"fileLocation\""); + propertyUrl = getResource(StringUtils.replace(fileLocation, PROPERTY_ENV, this.defaultEnvironment)); + + } + + if (propertyUrl == null) + { + log.error("No properties found at {}", replacedLocations); + } + else + { + Resource resource = new UrlResource(propertyUrl); + super.setLocation(resource); + } } - return "src/main/webapp/"; + + super.postProcessBeanFactory(beanFactory); } private URL getResource(String resource) @@ -106,65 +177,30 @@ { try { - return new File(getRootPath(), resource).toURL(); + url = ResourceUtils.getURL(resource); } - catch (MalformedURLException e) + catch (FileNotFoundException e) { - log.error(e.getMessage(), e); + // ignore, can be normal } - // test } return url; } - /** - * {@inheritDoc} - */ - @Override - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException + private String getApplicationName() { - if (fileLocation != null) + if (servletContext != null) { - - String hostname = null; - - try + String url = servletContext.getRealPath("/"); + url = StringUtils.replace(url, "\\", "/"); + if (url.endsWith("/")) { - hostname = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()); + url = StringUtils.substringBeforeLast(url, "/"); } - catch (UnknownHostException e) - { - log.error(e.getMessage()); - } - System.setProperty("env", hostname); - - String resolvedLocation = StringUtils.replace(fileLocation, "${env}", hostname); - URL propertyUrl = null; - - propertyUrl = getResource(resolvedLocation); - - if (propertyUrl == null) - { - log.info("No environment specific properties found at {}, using default", resolvedLocation); - resolvedLocation = StringUtils.replace(fileLocation, "${env}", this.defaultEnvironment); - - propertyUrl = getResource(resolvedLocation); - - } - - if (propertyUrl == null) - { - log.error("No default properties found at {}", resolvedLocation); - } - else - { - Resource resource = new UrlResource(propertyUrl); - super.setLocation(resource); - } + return StringUtils.substringAfterLast(url, "/"); } - - super.postProcessBeanFactory(beanFactory); + return null; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |