View Javadoc
1   /**
2    * This file Copyright (c) 2008-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.beans.config;
35  
36  import info.magnolia.cms.core.SystemProperty;
37  import info.magnolia.module.ModuleRegistry;
38  import info.magnolia.module.model.ModuleDefinition;
39  import info.magnolia.module.model.PropertyDefinition;
40  import info.magnolia.objectfactory.Components;
41  
42  import java.io.File;
43  import java.io.FileInputStream;
44  import java.io.FileNotFoundException;
45  import java.io.IOException;
46  import java.io.InputStream;
47  import java.nio.file.Paths;
48  import java.text.MessageFormat;
49  import java.util.HashSet;
50  import java.util.Iterator;
51  import java.util.List;
52  import java.util.Properties;
53  import java.util.Set;
54  
55  import javax.inject.Inject;
56  import javax.servlet.ServletContext;
57  
58  import org.apache.commons.io.IOUtils;
59  import org.apache.commons.lang3.ArrayUtils;
60  import org.apache.commons.lang3.StringUtils;
61  import org.slf4j.Logger;
62  import org.slf4j.LoggerFactory;
63  
64  /**
65   * This class is responsible for loading the various "magnolia.properties" files, merging them,
66   * and substituting variables in their values.
67   *
68   * @deprecated since 4.5 - replaced by classes in the {@link info.magnolia.init} package.
69   */
70  @Deprecated
71  public class PropertiesInitializer {
72      private static final Logger log = LoggerFactory.getLogger(PropertiesInitializer.class);
73  
74      /**
75       * The properties file containing the bean default implementations.
76       */
77      private static final String MGNL_BEANS_PROPERTIES = "/mgnl-beans.properties";
78  
79      /**
80       * Placeholder prefix: "${".
81       */
82      public static final String PLACEHOLDER_PREFIX = "${";
83  
84      /**
85       * Placeholder suffix: "}".
86       */
87      public static final String PLACEHOLDER_SUFFIX = "}";
88  
89      /**
90       * Context attribute prefix, to obtain a property definition like ${contextAttribute/property}, that can refer to
91       * any context attribute.
92       */
93      public static final String CONTEXT_ATTRIBUTE_PLACEHOLDER_PREFIX = "contextAttribute/";
94  
95      /**
96       * Context parameter prefix, to obtain a property definition like ${contextParam/property}, that can refer to any
97       * context parameter.
98       */
99      public static final String CONTEXT_PARAM_PLACEHOLDER_PREFIX = "contextParam/";
100 
101     /**
102      * System property prefix, to obtain a property definition like ${systemProperty/property}, that can refer to any
103      * System property.
104      */
105     public static final String SYSTEM_PROPERTY_PLACEHOLDER_PREFIX = "systemProperty/";
106 
107     /**
108      * System property prefix, to obtain a property definition like ${systemProperty/property}, that can refer to any
109      * System property.
110      */
111     public static final String ENV_PROPERTY_PLACEHOLDER_PREFIX = "env/";
112 
113     /**
114      * @deprecated since 4.5, use IoC
115      */
116     public static PropertiesInitializer getInstance() {
117         return Components.getSingleton(PropertiesInitializer.class);
118     }
119 
120     /**
121      * Default value for the MAGNOLIA_INITIALIZATION_FILE parameter.
122      */
123     public static final String DEFAULT_INITIALIZATION_PARAMETER = //
124             "WEB-INF/config/${servername}/${webapp}/magnolia.properties,"
125                     + "WEB-INF/config/${servername}/magnolia.properties,"
126                     + "WEB-INF/config/${webapp}/magnolia.properties,"
127                     + "WEB-INF/config/default/magnolia.properties,"
128                     + "WEB-INF/config/magnolia.properties";
129 
130     private final ModuleRegistry moduleRegistry;
131 
132     @Inject
133     public PropertiesInitializer(ModuleRegistry moduleRegistry) {
134         this.moduleRegistry = moduleRegistry;
135     }
136 
137     public void loadAllProperties(String propertiesFilesString, String rootPath) {
138         // load mgnl-beans.properties first
139         loadBeanProperties();
140 
141         loadAllModuleProperties();
142 
143         // complete or override with WEB-INF properties files
144         loadPropertiesFiles(propertiesFilesString, rootPath);
145 
146         // complete or override with JVM system properties
147         overloadWithSystemProperties();
148 
149         // resolve nested properties
150         resolveNestedProperties();
151     }
152 
153     private void resolveNestedProperties() {
154 
155         Properties sysProps = SystemProperty.getProperties();
156 
157         for (Iterator<Object> it = sysProps.keySet().iterator(); it.hasNext(); ) {
158             String key = (String) it.next();
159             String oldValue = (String) sysProps.get(key);
160             String value = parseStringValue(oldValue, new HashSet<String>());
161             SystemProperty.getProperties().put(key, value.trim());
162         }
163 
164     }
165 
166     public void loadAllModuleProperties() {
167         // complete or override with modules' properties
168         final List<ModuleDefinition> moduleDefinitions = moduleRegistry.getModuleDefinitions();
169         loadModuleProperties(moduleDefinitions);
170     }
171 
172     /**
173      * Load the properties defined in the module descriptors. They can get overridden later in the properties files in
174      * WEB-INF
175      */
176     protected void loadModuleProperties(List<ModuleDefinition> moduleDefinitions) {
177         for (ModuleDefinition module : moduleDefinitions) {
178             for (PropertyDefinition property : module.getProperties()) {
179                 SystemProperty.setProperty(property.getName(), property.getValue());
180             }
181         }
182     }
183 
184     public void loadPropertiesFiles(String propertiesLocationString, String rootPath) {
185 
186         String[] propertiesLocation = StringUtils.split(propertiesLocationString, ',');
187 
188         boolean found = false;
189         // attempt to load each properties file at the given locations in reverse order: first files in the list
190         // override the later ones
191         for (int j = propertiesLocation.length - 1; j >= 0; j--) {
192             String location = StringUtils.trim(propertiesLocation[j]);
193 
194             if (loadPropertiesFile(rootPath, location)) {
195                 found = true;
196             }
197         }
198 
199         if (!found) {
200             final String msg = MessageFormat.format("No configuration found using location list {0}. Base path is [{1}]", ArrayUtils.toString(propertiesLocation), rootPath);
201             log.error(msg);
202             throw new ConfigurationException(msg);
203         }
204     }
205 
206     /**
207      * @see info.magnolia.init.DefaultMagnoliaConfigurationProperties
208      * @deprecated since 4.5, replaced by a new ClasspathPropertySource("/mgnl-beans.properties").
209      */
210     @Deprecated
211     public void loadBeanProperties() {
212         InputStream mgnlbeansStream = getClass().getResourceAsStream(MGNL_BEANS_PROPERTIES);
213 
214         if (mgnlbeansStream != null) {
215             Properties mgnlbeans = new Properties();
216             try {
217                 mgnlbeans.load(mgnlbeansStream);
218             } catch (IOException e) {
219                 log.error("Unable to load {} due to an IOException: {}", MGNL_BEANS_PROPERTIES, e.getMessage());
220             } finally {
221                 IOUtils.closeQuietly(mgnlbeansStream);
222             }
223 
224             for (Iterator<Object> iter = mgnlbeans.keySet().iterator(); iter.hasNext(); ) {
225                 String key = (String) iter.next();
226                 SystemProperty.setProperty(key, mgnlbeans.getProperty(key));
227             }
228 
229         } else {
230             log.warn("{} not found in the classpath. Check that all the needed implementation classes are defined in your custom magnolia.properties file.", MGNL_BEANS_PROPERTIES);
231         }
232     }
233 
234     /**
235      * Try to load a magnolia.properties file.
236      */
237     public boolean loadPropertiesFile(String rootPath, String location) {
238         final File initFile;
239         if (Paths.get(location).isAbsolute()) {
240             initFile = new File(location);
241         } else {
242             initFile = new File(rootPath, location);
243         }
244 
245         if (!initFile.exists() || initFile.isDirectory()) {
246             log.debug("Configuration file not found with path [{}]", initFile.getAbsolutePath());
247             return false;
248         }
249 
250         InputStream fileStream = null;
251         try {
252             fileStream = new FileInputStream(initFile);
253         } catch (FileNotFoundException e1) {
254             log.debug("Configuration file not found with path [{}]", initFile.getAbsolutePath());
255             return false;
256         }
257 
258         try {
259             SystemProperty.getProperties().load(fileStream);
260             log.info("Loading configuration at {}", initFile.getAbsolutePath());
261         } catch (Exception e) {
262             log.error(e.getMessage(), e);
263             return false;
264         } finally {
265             IOUtils.closeQuietly(fileStream);
266         }
267         return true;
268     }
269 
270     /**
271      * Overload the properties with set system properties.
272      */
273     public void overloadWithSystemProperties() {
274         Iterator<Object> it = SystemProperty.getProperties().keySet().iterator();
275         while (it.hasNext()) {
276             String key = (String) it.next();
277             if (System.getProperties().containsKey(key)) {
278                 log.info("system property found: {}", key);
279                 String value = System.getProperty(key);
280                 SystemProperty.setProperty(key, value);
281             }
282         }
283     }
284 
285     /**
286      * Returns the property files configuration string passed, replacing all the needed values: ${servername} will be
287      * reaplced with the name of the server, ${webapp} will be replaced with webapp name, ${systemProperty/*} will be
288      * replaced with the corresponding system property, ${env/*} will be replaced with the corresponding environment property,
289      * and ${contextAttribute/*} and ${contextParam/*} will be replaced with the corresponding attributes or parameters
290      * taken from servlet context.
291      * This can be very useful for all those application servers that has multiple instances on the same server, like
292      * WebSphere. Typical usage in this case:
293      * <code>WEB-INF/config/${servername}/${contextAttribute/com.ibm.websphere.servlet.application.host}/magnolia.properties</code>
294      *
295      * @param context Servlet context
296      * @param servername Server name
297      * @param webapp Webapp name
298      * @param propertiesFilesString a comma separated list of paths.
299      * @return Property file configuration string with everything replaced.
300      * @deprecated since 4.5, this is done by {@link info.magnolia.init.DefaultMagnoliaPropertiesResolver#DefaultMagnoliaPropertiesResolver}.
301      *             Note: when remove this class and method, this code will need to be cleaned up and moved to info.magnolia.init.DefaultMagnoliaPropertiesResolver
302      */
303     public static String processPropertyFilesString(ServletContext context, String servername, String webapp, String propertiesFilesString, String contextPath) {
304         // Replacing basic properties.
305         propertiesFilesString = StringUtils.replace(propertiesFilesString, "${servername}", servername);
306         propertiesFilesString = StringUtils.replace(propertiesFilesString, "${webapp}", webapp);
307         propertiesFilesString = StringUtils.replace(propertiesFilesString, "${contextPath}", contextPath);
308 
309         // Replacing servlet context attributes (${contextAttribute/something})
310         String[] contextAttributeNames = getNamesBetweenPlaceholders(propertiesFilesString, CONTEXT_ATTRIBUTE_PLACEHOLDER_PREFIX);
311         if (contextAttributeNames != null) {
312             for (String ctxAttrName : contextAttributeNames) {
313                 if (ctxAttrName != null) {
314                     // Some implementation may not accept a null as attribute key, but all should accept an empty
315                     // string.
316                     final String originalPlaceHolder = PLACEHOLDER_PREFIX + CONTEXT_ATTRIBUTE_PLACEHOLDER_PREFIX + ctxAttrName + PLACEHOLDER_SUFFIX;
317                     final Object attrValue = context.getAttribute(ctxAttrName);
318                     if (attrValue != null) {
319                         propertiesFilesString = propertiesFilesString.replace(originalPlaceHolder, attrValue.toString());
320                     }
321                 }
322             }
323         }
324 
325         // Replacing servlet context parameters (${contextParam/something})
326         String[] contextParamNames = getNamesBetweenPlaceholders(propertiesFilesString, CONTEXT_PARAM_PLACEHOLDER_PREFIX);
327         if (contextParamNames != null) {
328             for (String ctxParamName : contextParamNames) {
329                 if (ctxParamName != null) {
330                     // Some implementation may not accept a null as param key, but an empty string? TODO Check.
331                     final String originalPlaceHolder = PLACEHOLDER_PREFIX + CONTEXT_PARAM_PLACEHOLDER_PREFIX + ctxParamName + PLACEHOLDER_SUFFIX;
332                     final String paramValue = context.getInitParameter(ctxParamName);
333                     if (paramValue != null) {
334                         propertiesFilesString = propertiesFilesString.replace(originalPlaceHolder, paramValue);
335                     }
336                 }
337             }
338         }
339 
340         // Replacing system property (${systemProperty/something})
341         String[] systemPropertiesNames = getNamesBetweenPlaceholders(propertiesFilesString, SYSTEM_PROPERTY_PLACEHOLDER_PREFIX);
342         if (systemPropertiesNames != null) {
343             for (String sysPropName : systemPropertiesNames) {
344                 if (StringUtils.isNotBlank(sysPropName)) {
345                     final String originalPlaceHolder = PLACEHOLDER_PREFIX + SYSTEM_PROPERTY_PLACEHOLDER_PREFIX + sysPropName + PLACEHOLDER_SUFFIX;
346                     final String paramValue = System.getProperty(sysPropName);
347                     if (paramValue != null) {
348                         propertiesFilesString = propertiesFilesString.replace(originalPlaceHolder, paramValue);
349                     }
350                 }
351             }
352         }
353 
354         // Replacing environment property (${env/something})
355         String[] envPropertiesNames = getNamesBetweenPlaceholders(propertiesFilesString, ENV_PROPERTY_PLACEHOLDER_PREFIX);
356         if (envPropertiesNames != null) {
357             for (String envPropName : envPropertiesNames) {
358                 if (StringUtils.isNotBlank(envPropName)) {
359                     final String originalPlaceHolder = PLACEHOLDER_PREFIX + ENV_PROPERTY_PLACEHOLDER_PREFIX + envPropName + PLACEHOLDER_SUFFIX;
360                     final String paramValue = System.getenv(envPropName);
361                     if (paramValue != null) {
362                         propertiesFilesString = propertiesFilesString.replace(originalPlaceHolder, paramValue);
363                     }
364                 }
365             }
366         }
367 
368         return propertiesFilesString;
369     }
370 
371     private static String[] getNamesBetweenPlaceholders(String propertiesFilesString, String contextNamePlaceHolder) {
372         final String[] names = StringUtils.substringsBetween(
373                 propertiesFilesString,
374                 PLACEHOLDER_PREFIX + contextNamePlaceHolder,
375                 PLACEHOLDER_SUFFIX);
376         return StringUtils.stripAll(names);
377     }
378 
379     /**
380      * Parse the given String value recursively, to be able to resolve nested placeholders. Partly borrowed from
381      * org.springframework.beans.factory.config.PropertyPlaceholderConfigurer (original author: Juergen Hoeller)
382      *
383      * @deprecated since 4.5 this is now done by {@link info.magnolia.init.AbstractMagnoliaConfigurationProperties#parseStringValue}.
384      */
385     protected String parseStringValue(String strVal, Set<String> visitedPlaceholders) {
386 
387         StringBuffer buf = new StringBuffer(strVal);
388 
389         int startIndex = strVal.indexOf(PLACEHOLDER_PREFIX);
390         while (startIndex != -1) {
391             int endIndex = -1;
392 
393             int index = startIndex + PLACEHOLDER_PREFIX.length();
394             int withinNestedPlaceholder = 0;
395             while (index < buf.length()) {
396                 if (PLACEHOLDER_SUFFIX.equals(buf.subSequence(index, index + PLACEHOLDER_SUFFIX.length()))) {
397                     if (withinNestedPlaceholder > 0) {
398                         withinNestedPlaceholder--;
399                         index = index + PLACEHOLDER_SUFFIX.length();
400                     } else {
401                         endIndex = index;
402                         break;
403                     }
404                 } else if (PLACEHOLDER_PREFIX.equals(buf.subSequence(index, index + PLACEHOLDER_PREFIX.length()))) {
405                     withinNestedPlaceholder++;
406                     index = index + PLACEHOLDER_PREFIX.length();
407                 } else {
408                     index++;
409                 }
410             }
411 
412             if (endIndex != -1) {
413                 String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
414                 if (!visitedPlaceholders.add(placeholder)) {
415 
416                     log.warn("Circular reference detected in properties, \"{}\" is not resolvable", strVal);
417                     return strVal;
418                 }
419                 // Recursive invocation, parsing placeholders contained in the placeholder key.
420                 placeholder = parseStringValue(placeholder, visitedPlaceholders);
421                 // Now obtain the value for the fully resolved key...
422                 String propVal = SystemProperty.getProperty(placeholder);
423                 if (propVal != null) {
424                     // Recursive invocation, parsing placeholders contained in the
425                     // previously resolved placeholder value.
426                     propVal = parseStringValue(propVal, visitedPlaceholders);
427                     buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
428                     startIndex = buf.indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length());
429                 } else {
430                     // Proceed with unprocessed value.
431                     startIndex = buf.indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length());
432                 }
433                 visitedPlaceholders.remove(placeholder);
434             } else {
435                 startIndex = -1;
436             }
437         }
438 
439         return buf.toString();
440     }
441 
442 }