From: <fg...@us...> - 2010-03-19 23:09:50
|
Revision: 2160 http://openutils.svn.sourceforge.net/openutils/?rev=2160&view=rev Author: fgiust Date: 2010-03-19 22:21:21 +0000 (Fri, 19 Mar 2010) Log Message: ----------- DEPLOY-3, DEPLOY-4, DEPLOY-5 EnvironmentPropertyConfigurer enhancements Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 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 2010-03-19 09:34:10 UTC (rev 2159) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2010-03-19 22:21:21 UTC (rev 2160) @@ -1,21 +1,18 @@ /** + * Copyright Openmind http://www.openmindonline.it * - * openutils deployment tools (http://www.openmindlab.com/lab/products/deployment.html) - * Copyright(C) null-2010, Openmind S.r.l. http://www.openmindonline.it + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ - package it.openutils.deployment.spring; import java.beans.PropertyDescriptor; @@ -26,8 +23,11 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; +import java.util.Enumeration; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.Map; import java.util.Properties; import javax.servlet.ServletContext; @@ -51,8 +51,31 @@ /** + * <p> + * A propertyconfigurer that can be used to dynamically select a list of property files based on the current + * environment. + * </p> + * <p> + * You can configure the <code>fileLocation</code> parameter with a list of file location, using variables for: + * </p> + * <ul> + * <li>the server name: ${env}</li> + * <li>the web application root folder name (only for web contexts): ${appl}</li> + * <li>any web context init parameter (only for web contexts): ${contextParam/paramname}</li> + * </ul> + * <p> + * <p> + * A sample value for the <code>fileLocation</code> parameter would be: <code>WEB-INF/config/${env}/my.properties, + * WEB-INF/config/${appl}/my.properties, + * WEB-INF/config/${contextParam/instance}/my.properties, + * classpath:my-${env}.properties,classpath:default.properties</code>. + * </p> + * <p> + * After replacing all the known variables the resulting list is parsed and any existing file is merged in a single + * property list, used to configure the remaining spring context + * </p> * @author fgiust - * @version $Id: $ + * @version $Id: EnvironmentPropertyConfigurer.java 592 2010-03-19 13:24:12Z christian.strappazzon $ */ public class EnvironmentPropertyConfigurer extends PropertyPlaceholderConfigurer implements @@ -60,27 +83,15 @@ SmartInstantiationAwareBeanPostProcessor { - /** - * Application name (webapp name) variable. - */ - private static final String PROPERTY_APPL = "${appl}"; + private String serverPropertyName = "env"; - /** - * Environment (server name) variable. - */ - private static final String PROPERTY_ENV = "${env}"; + private String applicationPropertyName = "appl"; /** * Logger. */ private static Logger log = LoggerFactory.getLogger(EnvironmentPropertyConfigurer.class); - /** - * @deprecated use defaultLocation - */ - @Deprecated - private String defaultEnvironment; - private ServletContext servletContext; private String fileLocation; @@ -121,17 +132,24 @@ } /** - * Setter for <code>defaultEnvironment</code>. - * @param defaultEnvironment The defaultEnvironment to set. - * @deprecated use defaultLocation + * Sets the serverPropertyName. + * @param serverPropertyName the serverPropertyName to set */ - @Deprecated - public void setDefaultEnvironment(String defaultEnvironment) + public void setServerPropertyName(String serverPropertyName) { - this.defaultEnvironment = defaultEnvironment; + this.serverPropertyName = serverPropertyName; } /** + * Sets the applicationPropertyName. + * @param applicationPropertyName the applicationPropertyName to set + */ + public void setApplicationPropertyName(String applicationPropertyName) + { + this.applicationPropertyName = applicationPropertyName; + } + + /** * Set all the properties configured as system properties. * @param exposeSystemProperties <code>true</code> if you want to set configured properties as system properties. */ @@ -150,18 +168,35 @@ /** * {@inheritDoc} */ + @SuppressWarnings("unchecked") @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + + Map<String, String> initParametersMap = new HashMap<String, String>(); + if (fileLocation != null) { + if (this.servletContext != null) + { + + Enumeration<String> initParameters = servletContext.getInitParameterNames(); + while (initParameters.hasMoreElements()) + { + String paramName = initParameters.nextElement(); + initParametersMap.put("${contextParam/" + paramName + "}", servletContext + .getInitParameter(paramName)); + } + } + String hostname = null; try { hostname = StringUtils.substringBefore( StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()), "."); + initParametersMap.put("${" + serverPropertyName + "}", hostname); } catch (UnknownHostException e) { @@ -177,24 +212,15 @@ if (applName != null) { System.setProperty("appl", applName); + initParametersMap.put("${" + applicationPropertyName + "}", applName); } URL propertyUrl = null; String fileLocationFull = fileLocation; - if (defaultEnvironment != null) - { - log.warn("Usage of \"defaultEnvironment\" is deprecated, please specify the fallback location " - + "as the last comma separated value in \"fileLocation\""); - fileLocationFull = fileLocationFull - + "," - + StringUtils.replace(fileLocationFull, PROPERTY_ENV, this.defaultEnvironment); - } + String replacedLocations = replaceAll(initParametersMap, fileLocationFull); - String replacedLocations = StringUtils.replace(fileLocationFull, PROPERTY_ENV, hostname); - replacedLocations = StringUtils.replace(replacedLocations, PROPERTY_APPL, applName); - String[] locations = StringUtils.split(replacedLocations, ","); if (inherit) @@ -274,6 +300,24 @@ super.postProcessBeanFactory(beanFactory); } + /** + * @param propertyenv + * @param hostname + * @param fileLocationFull + * @return + */ + private String replaceAll(Map<String, String> params, String fileLocationFull) + { + String replacedLocations = fileLocationFull; + + for (Map.Entry<String, String> param : params.entrySet()) + { + replacedLocations = StringUtils.replace(replacedLocations, param.getKey(), param.getValue()); + } + + return replacedLocations; + } + private URL getResource(String resource) { URL url = null; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <df...@us...> - 2011-01-11 15:18:27
|
Revision: 3214 http://openutils.svn.sourceforge.net/openutils/?rev=3214&view=rev Author: dfghi Date: 2011-01-11 15:18:21 +0000 (Tue, 11 Jan 2011) Log Message: ----------- DEPLOY-7 Using property names when setting the system properties for server and application name. Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 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 2011-01-04 15:45:22 UTC (rev 3213) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2011-01-11 15:18:21 UTC (rev 3214) @@ -208,13 +208,13 @@ if (hostname != null) { - System.setProperty("env", hostname); + System.setProperty(serverPropertyName, hostname); } String applName = getApplicationName(); if (applName != null) { - System.setProperty("appl", applName); + System.setProperty(applicationPropertyName, applName); initParametersMap.put("${" + applicationPropertyName + "}", applName); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2011-08-11 09:06:09
|
Revision: 3605 http://openutils.svn.sourceforge.net/openutils/?rev=3605&view=rev Author: fgiust Date: 2011-08-11 09:06:03 +0000 (Thu, 11 Aug 2011) Log Message: ----------- DEPLOY-6 Misleading log in EnvironmentPropertyConfigurer.java Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 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 2011-08-08 13:01:07 UTC (rev 3604) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2011-08-11 09:06:03 UTC (rev 3605) @@ -188,8 +188,9 @@ while (initParameters.hasMoreElements()) { String paramName = initParameters.nextElement(); - initParametersMap.put("${contextParam/" + paramName + "}", servletContext - .getInitParameter(paramName)); + initParametersMap.put( + "${contextParam/" + paramName + "}", + servletContext.getInitParameter(paramName)); } } @@ -240,7 +241,7 @@ if (propertyUrl != null) { found = true; - log.debug("Loading property file at {}", loc); + log.debug("Loading property file at {} from {}", loc, propertyUrl); Resource resource = new UrlResource(propertyUrl); InputStream is = null; @@ -274,7 +275,10 @@ break; } } - log.debug("Property file not found at {}", loc); + else + { + log.debug("Property file not found at {}", loc); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2011-08-12 08:49:38
|
Revision: 3608 http://openutils.svn.sourceforge.net/openutils/?rev=3608&view=rev Author: fgiust Date: 2011-08-12 08:49:32 +0000 (Fri, 12 Aug 2011) Log Message: ----------- DEPLOY-12 Possible nullpointer while retrieving application name. Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 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 2011-08-11 15:55:03 UTC (rev 3607) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2011-08-12 08:49:32 UTC (rev 3608) @@ -381,7 +381,7 @@ { if (servletContext != null) { - String url = servletContext.getRealPath("/"); + String url = StringUtils.defaultString(servletContext.getRealPath("/")); url = StringUtils.replace(url, "\\", "/"); if (url.endsWith("/")) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2011-08-12 08:58:31
|
Revision: 3609 http://openutils.svn.sourceforge.net/openutils/?rev=3609&view=rev Author: fgiust Date: 2011-08-12 08:58:23 +0000 (Fri, 12 Aug 2011) Log Message: ----------- DEPLOY-10 warning when overwriting system properties Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 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 2011-08-12 08:49:32 UTC (rev 3608) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2011-08-12 08:58:23 UTC (rev 3609) @@ -297,6 +297,11 @@ { String key = (String) i.next(); String value = (String) props.get(key); + + if (System.getProperty(key) != null) + { + log.warn("Overwriting system property {}", key); + } System.setProperty(key, value); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2011-08-12 09:04:11
|
Revision: 3610 http://openutils.svn.sourceforge.net/openutils/?rev=3610&view=rev Author: fgiust Date: 2011-08-12 09:04:05 +0000 (Fri, 12 Aug 2011) Log Message: ----------- DEPLOY-9 Accept also system properties in the file location. Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 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 2011-08-12 08:58:23 UTC (rev 3609) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2011-08-12 09:04:05 UTC (rev 3610) @@ -31,7 +31,9 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; +import java.util.Set; import javax.servlet.ServletContext; @@ -48,6 +50,7 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; +import org.springframework.util.CollectionUtils; import org.springframework.util.ResourceUtils; import org.springframework.util.StringValueResolver; import org.springframework.web.context.WebApplicationContext; @@ -65,6 +68,7 @@ * <li>the server name: ${env}</li> * <li>the web application root folder name (only for web contexts): ${appl}</li> * <li>any web context init parameter (only for web contexts): ${contextParam/paramname}</li> + * <li>any system property: ${systemProperty/paramname}</li> * </ul> * <p> * <p> @@ -194,6 +198,18 @@ } } + Set<Entry<Object, Object>> entrySet = System.getProperties().entrySet(); + if (!CollectionUtils.isEmpty(entrySet)) + { + Iterator<Entry<Object, Object>> properties = entrySet.iterator(); + while (properties.hasNext()) + { + Entry<Object, Object> entry = properties.next(); + String propName = (String) entry.getKey(); + initParametersMap.put("${systemProperty/" + propName + "}", (String) entry.getValue()); + } + } + String hostname = null; try { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2011-08-12 09:09:05
|
Revision: 3611 http://openutils.svn.sourceforge.net/openutils/?rev=3611&view=rev Author: fgiust Date: 2011-08-12 09:08:59 +0000 (Fri, 12 Aug 2011) Log Message: ----------- DEPLOY-8 system properties are now set when "exposeSystemProperties" is true and a warning is logged when an existing property is overwritten Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 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 2011-08-12 09:04:05 UTC (rev 3610) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2011-08-12 09:08:59 UTC (rev 3611) @@ -216,22 +216,38 @@ hostname = StringUtils.substringBefore( StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()), "."); - initParametersMap.put("${" + serverPropertyName + "}", hostname); } catch (UnknownHostException e) { log.error(e.getMessage()); // should not happen } + String applName = getApplicationName(); if (hostname != null) { - System.setProperty(serverPropertyName, hostname); + if (exposeSystemProperties) + { + if (System.getProperty(serverPropertyName) != null) + { + log.warn("Overwriting system property {}", serverPropertyName); + } + System.setProperty(serverPropertyName, hostname); + } + initParametersMap.put("${" + serverPropertyName + "}", hostname); } - String applName = getApplicationName(); if (applName != null) { - System.setProperty(applicationPropertyName, applName); + + if (exposeSystemProperties) + { + if (System.getProperty(applicationPropertyName) != null) + { + log.warn("Overwriting system property {}", applicationPropertyName); + } + System.setProperty(applicationPropertyName, applName); + } + initParametersMap.put("${" + applicationPropertyName + "}", applName); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <df...@us...> - 2012-06-06 09:03:36
|
Revision: 4056 http://openutils.svn.sourceforge.net/openutils/?rev=4056&view=rev Author: dfghi Date: 2012-06-06 09:03:25 +0000 (Wed, 06 Jun 2012) Log Message: ----------- DEPLOY-13 Add the possibility to expose the server name as system property. Committed just because I've already used this in a couple of projects... Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 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 2012-05-28 13:52:12 UTC (rev 4055) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2012-06-06 09:03:25 UTC (rev 4056) @@ -117,6 +117,11 @@ * Set all the properties configured as system properties. */ private boolean exposeSystemProperties; + + /** + * Expose the server name as system property. + */ + private boolean exposeServerName; private String nullValue; @@ -164,6 +169,14 @@ { this.exposeSystemProperties = exposeSystemProperties; } + + /** + * Expose the server name as system property. + * @param exposeServerName <code>true</code> if you want to set the server name as system property (with the key stated in <code>serverPropertyName</code>). + */ + public void setExposeServerName(boolean exposeServerName) { + this.exposeServerName = exposeServerName; + } @Override public void setNullValue(String nullValue) @@ -225,7 +238,7 @@ if (hostname != null) { - if (exposeSystemProperties) + if (exposeSystemProperties || exposeServerName) { if (System.getProperty(serverPropertyName) != null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |