View Javadoc

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