View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2011 Brabenetz Harald, Austria.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * 
16   *****************************************************************************/
17  package org.settings4j.helper.spring;
18  
19  import java.util.Properties;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.settings4j.Settings4j;
23  import org.springframework.beans.factory.BeanDefinitionStoreException;
24  import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
25  import org.springframework.util.PropertyPlaceholderHelper;
26  import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
27  
28  /**
29   * Subclass of PropertyPlaceholderConfigurer uses the Settings4j API.
30   * <p>
31   * You can also configure a Prefix if
32   * 
33   * @see Settings4j#getString(String)
34   */
35  public class Settings4jPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
36  
37      private String prefix = StringUtils.EMPTY;
38  
39      public void setPrefix(final String prefix) {
40          this.prefix = prefix;
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      protected String resolvePlaceholder(final String placeholder, final Properties props) {
46  
47          String value = Settings4j.getString(prefix + placeholder);
48          if (value == null) {
49              value = props.getProperty(prefix + placeholder);
50              if (value == null) {
51                  value = props.getProperty(placeholder);
52              }
53          }
54          return value;
55      }
56  
57      /**
58       * Parse the given String with Placeholder "${...}" and returns the result.
59       * <p>
60       * Placeholders will be resolved with Settings4j.
61       * 
62       * @param strVal the String with the Paceholders
63       * @return the parsed String
64       * @throws BeanDefinitionStoreException if invalid values are encountered (Placeholders where no values where
65       *             found).
66       */
67      public static String parseStringValue(final String strVal) throws BeanDefinitionStoreException {
68          return parseStringValue(strVal, StringUtils.EMPTY);
69      }
70  
71      /**
72       * Parse the given String with Placeholder "${...}" and returns the result.
73       * <p>
74       * Placeholders will be resolved with Settings4j.
75       * 
76       * @param strVal the String with the Paceholders
77       * @param prefix for all placehodlers.
78       * @return the parsed String
79       * @throws BeanDefinitionStoreException if invalid values are encountered (Placeholders where no values where
80       *             found).
81       */
82      public static String parseStringValue(final String strVal, final String prefix) //
83              throws BeanDefinitionStoreException {
84          return parseStringValue(strVal, prefix, new Properties());
85      }
86  
87      /**
88       * Parse the given String with Placeholder "${...}" and returns the result.
89       * <p>
90       * Placeholders will be resolved with Settings4j.
91       * 
92       * @param strVal the String with the Paceholders
93       * @param prefix for all placehodlers.
94       * @param props The default Properties if no Value where found
95       * @return the parsed String
96       * @throws BeanDefinitionStoreException if invalid values are encountered (Placeholders where no values where
97       *             found).
98       */
99      public static String parseStringValue(final String strVal, final String prefix, final Properties props)
100             throws BeanDefinitionStoreException {
101         final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX,
102             DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false);
103         return helper.replacePlaceholders(strVal, new Settings4jPlaceholderConfigurerResolver(prefix, props));
104     }
105 
106     /**
107      * PlaceholderResolver implementation can be used for
108      * {@link PropertyPlaceholderHelper#replacePlaceholders(String, PlaceholderResolver)}.
109      * 
110      * @author brabenetz
111      */
112     private static final class Settings4jPlaceholderConfigurerResolver implements PlaceholderResolver {
113 
114         private final String prefix;
115         private final Properties props;
116 
117 
118         private Settings4jPlaceholderConfigurerResolver(final String prefix, final Properties props) {
119             this.prefix = prefix;
120             this.props = props;
121         }
122 
123         public String resolvePlaceholder(final String placeholderName) {
124             final Settings4jPlaceholderConfigurer placeholderConfigurer = new Settings4jPlaceholderConfigurer();
125             placeholderConfigurer.setPrefix(prefix);
126             return placeholderConfigurer.resolvePlaceholder(placeholderName, props);
127         }
128     }
129 
130 }