View Javadoc
1   /**
2    * This file Copyright (c) 2011-2015 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.init;
35  
36  import java.util.HashSet;
37  import java.util.List;
38  import java.util.Set;
39  
40  /**
41   * Abstract implementation, providing the basic behavior expected from a {@link MagnoliaConfigurationProperties} implementation.
42   *
43   * TODO: cache results.
44   */
45  public abstract class AbstractMagnoliaConfigurationProperties implements MagnoliaConfigurationProperties {
46      protected static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractMagnoliaConfigurationProperties.class);
47  
48      protected List<PropertySource> sources;
49  
50      protected AbstractMagnoliaConfigurationProperties(List<PropertySource> propertySources) {
51          this.sources = propertySources;
52      }
53  
54      @Override
55      public void init() throws Exception {
56      }
57  
58      @Override
59      public Set<String> getKeys() {
60          final Set<String> allKeys = new HashSet<String>();
61          for (PropertySource source : sources) {
62              allKeys.addAll(source.getKeys());
63          }
64          return allKeys;
65      }
66  
67      @Override
68      public PropertySource getPropertySource(String key) {
69          for (PropertySource source : sources) {
70              if (source.hasProperty(key)) {
71                  return source;
72              }
73          }
74          return null;
75      }
76  
77      @Override
78      public String getProperty(String key) {
79          final PropertySource propertySource = getPropertySource(key);
80          if (propertySource != null) {
81              final String value = propertySource.getProperty(key);
82              return parseStringValue(value, new HashSet<String>());
83          }
84          return null;
85      }
86  
87      @Override
88      public boolean getBooleanProperty(String property) {
89          return Boolean.parseBoolean(getProperty(property));
90      }
91  
92      @Override
93      public boolean hasProperty(String key) {
94          return getPropertySource(key) != null;
95      }
96  
97      @Override
98      public String describe() {
99          final StringBuilder s = new StringBuilder()
100         .append("[")
101         .append(getClass().getSimpleName())
102         .append(" with sources: ");
103         for (PropertySource source : sources) {
104             s.append(source.describe());
105         }
106         s.append("]");
107         return s.toString();
108     }
109 
110     @Override
111     public String toString() {
112         return describe() + " with properties: " + sources;
113     }
114 
115     /**
116      * Parse the given String value recursively, to be able to resolve nested placeholders. Partly borrowed from
117      * org.springframework.beans.factory.config.PropertyPlaceholderConfigurer (original author: Juergen Hoeller)
118      */
119     protected String parseStringValue(String strVal, Set<String> visitedPlaceholders) {
120         final StringBuffer buf = new StringBuffer(strVal.trim());
121 
122         int startIndex = strVal.indexOf(PLACEHOLDER_PREFIX);
123         while (startIndex != -1) {
124             int endIndex = -1;
125 
126             int index = startIndex + PLACEHOLDER_PREFIX.length();
127             int withinNestedPlaceholder = 0;
128             while (index < buf.length()) {
129                 if (PLACEHOLDER_SUFFIX.equals(buf.subSequence(index, index + PLACEHOLDER_SUFFIX.length()))) {
130                     if (withinNestedPlaceholder > 0) {
131                         withinNestedPlaceholder--;
132                         index = index + PLACEHOLDER_SUFFIX.length();
133                     } else {
134                         endIndex = index;
135                         break;
136                     }
137                 } else if (PLACEHOLDER_PREFIX.equals(buf.subSequence(index, index + PLACEHOLDER_PREFIX.length()))) {
138                     withinNestedPlaceholder++;
139                     index = index + PLACEHOLDER_PREFIX.length();
140                 } else {
141                     index++;
142                 }
143             }
144 
145             if (endIndex != -1) {
146                 String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
147                 if (!visitedPlaceholders.add(placeholder)) {
148                     log.warn("Circular reference detected in properties, \"{}\" is not resolvable", strVal);
149                     return strVal;
150                 }
151                 // Recursive invocation, parsing placeholders contained in the placeholder key.
152                 placeholder = parseStringValue(placeholder, visitedPlaceholders);
153                 // Now obtain the value for the fully resolved key...
154                 // Can't call getProperty() directly, as it would blow the call stack
155                 final PropertySource propertySource = getPropertySource(placeholder);
156                 String propVal = propertySource != null ? propertySource.getProperty(placeholder) : null;
157                 if (propVal != null) {
158                     // Recursive invocation, parsing placeholders contained in the
159                     // previously resolved placeholder value.
160                     propVal = parseStringValue(propVal, visitedPlaceholders);
161                     buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
162                     startIndex = buf.indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length());
163                 } else {
164                     // Proceed with unprocessed value.
165                     startIndex = buf.indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length());
166                 }
167                 visitedPlaceholders.remove(placeholder);
168             } else {
169                 startIndex = -1;
170             }
171         }
172 
173         return buf.toString();
174     }
175 
176     protected static final String PLACEHOLDER_PREFIX = "${";
177     protected static final String PLACEHOLDER_SUFFIX = "}";
178 
179 
180 }