Index: magnolia-core/src/main/java/info/magnolia/cms/beans/config/PropertiesInitializer.java =================================================================== --- magnolia-core/src/main/java/info/magnolia/cms/beans/config/PropertiesInitializer.java (revision 22905) +++ magnolia-core/src/main/java/info/magnolia/cms/beans/config/PropertiesInitializer.java (working copy) @@ -41,6 +41,7 @@ import info.magnolia.module.model.PropertyDefinition; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,6 +58,8 @@ import java.util.Properties; import java.util.Set; +import javax.servlet.ServletContext; + /** * @author pbracher * @author fgiust @@ -65,18 +68,28 @@ /** * The properties file containing the bean default implementations */ - private static final String MGNL_BEANS_PROPERTIES = "/mgnl-beans.properties"; + private static final String MGNL_BEANS_PROPERTIES = "/mgnl-beans.properties"; //$NON-NLS-1$ /** * Placeholder prefix: "${" */ - public static final String PLACEHOLDER_PREFIX = "${"; + public static final String PLACEHOLDER_PREFIX = "${"; //$NON-NLS-1$ /** * Placeholder suffix: "}" */ - public static final String PLACEHOLDER_SUFFIX = "}"; + public static final String PLACEHOLDER_SUFFIX = "}"; //$NON-NLS-1$ + /** + * Context attribute prefix, to obtain a property definition like ${contextAttribute/property}, that can refer to any context attribute. + */ + public static final String CONTEXT_ATTRIBUTE_PLACEHOLDER_PREFIX = "contextAttribute/"; //$NON-NLS-1$ + + /** + * Context parameter prefix, to obtain a property definition like ${contextParam/property}, that can refer to any context parameter. + */ + public static final String CONTEXT_PARAM_PLACEHOLDER_PREFIX = "contextParam/"; //$NON-NLS-1$ + public static PropertiesInitializer getInstance() { return (PropertiesInitializer) FactoryUtil.getSingleton(PropertiesInitializer.class); } @@ -247,10 +260,51 @@ } } - public static String processPropertyFilesString(String servername, String webapp, String propertiesFilesString) { - propertiesFilesString = StringUtils.replace(propertiesFilesString, "${servername}", servername); //$NON-NLS-1$ - propertiesFilesString = StringUtils.replace(propertiesFilesString, "${webapp}", webapp); //$NON-NLS-1$ + /** + * Returns the property files configuration string passed, replacing all the needed values: ${servername} will be reaplced with the name + * of the server, ${webapp} will be replaced with webapp name, and ${contextAttribute/*} and ${contextParam/*} will be replaced with the + * corresponding attributes or parameters taken from servlet context. This can be very useful for all those application servers that has + * multiple instances on the same server, like WebSphere. Typical usage in this case: + * WEB-INF/config/${servername}/${contextAttribute/com.ibm.websphere.servlet.application.host}/magnolia.properties + * @param context Servlet context + * @param servername Server name + * @param webapp Webapp name + * @param propertiesFilesString Property file configuration string. + * @return Property file configuration string with everything replaced. + */ + public static String processPropertyFilesString(ServletContext context, String servername, String webapp, String propertiesFilesString) { + //Replacing basic properties. + propertiesFilesString = StringUtils.replace(propertiesFilesString, PLACEHOLDER_PREFIX + "servername" + PLACEHOLDER_SUFFIX, servername); //$NON-NLS-1$ + propertiesFilesString = StringUtils.replace(propertiesFilesString, PLACEHOLDER_PREFIX + "webapp" + PLACEHOLDER_SUFFIX, webapp); //$NON-NLS-1$ + //TODO Allow nested placeholders and generalize the parseStringValue() below to do all the substitutions. + + //Replacing servlet context attributes. + final String contextAttributePlaceHolder = PLACEHOLDER_PREFIX + CONTEXT_ATTRIBUTE_PLACEHOLDER_PREFIX; + String[] contextAttributes = StringUtils.stripAll(StringUtils.substringsBetween(propertiesFilesString, contextAttributePlaceHolder, PLACEHOLDER_SUFFIX)); + if (!ArrayUtils.isEmpty(contextAttributes)) { // Can be null. + for (String contextAttribute : contextAttributes) { + if (contextAttribute != null) { + //Some implementation may not accept a null as attribute key, but all should accept an empty string. + String originalPlaceHolder = contextAttributePlaceHolder + contextAttribute + PLACEHOLDER_SUFFIX; + propertiesFilesString = StringUtils.replace(propertiesFilesString, originalPlaceHolder, ObjectUtils.toString(context.getAttribute(contextAttribute), originalPlaceHolder)); + } + } + } + + //Replacing servlet context parameters. + final String contextParamPlaceHolder = PLACEHOLDER_PREFIX + CONTEXT_PARAM_PLACEHOLDER_PREFIX; + String[] contextParams = StringUtils.stripAll(StringUtils.substringsBetween(propertiesFilesString, contextParamPlaceHolder, PLACEHOLDER_SUFFIX)); + if (!ArrayUtils.isEmpty(contextParams)) { // Can be null. + for (String contextParam : contextParams) { + if (contextParam != null) { + //Some implementation may not accept a null as param key, but an empty string? TODO Check. + String originalPlaceHolder = contextParamPlaceHolder + contextParam + PLACEHOLDER_SUFFIX; + propertiesFilesString = StringUtils.replace(propertiesFilesString, originalPlaceHolder, StringUtils.defaultString(context.getInitParameter(contextParam), originalPlaceHolder)); + } + } + } + return propertiesFilesString; } Index: magnolia-core/src/main/java/info/magnolia/cms/servlets/MgnlServletContextListener.java =================================================================== --- magnolia-core/src/main/java/info/magnolia/cms/servlets/MgnlServletContextListener.java (revision 22905) +++ magnolia-core/src/main/java/info/magnolia/cms/servlets/MgnlServletContextListener.java (working copy) @@ -188,7 +188,7 @@ else { log.debug("{} value in web.xml is :'{}'", MgnlServletContextListener.MAGNOLIA_INITIALIZATION_FILE, propertiesFilesString); //$NON-NLS-1$ } - return PropertiesInitializer.processPropertyFilesString(servername, webapp, propertiesFilesString); + return PropertiesInitializer.processPropertyFilesString(context, servername, webapp, propertiesFilesString); } protected String initWebappName(String rootPath) { @@ -222,6 +222,10 @@ try { servername = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName()); + + if (StringUtils.contains(servername, ".")) { + servername = StringUtils.substringBefore(servername, "."); + } SystemProperty.setProperty(SystemProperty.MAGNOLIA_SERVERNAME, servername); } catch (UnknownHostException e) {