View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.config.ogdl;
20  
21  import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
22  import org.apache.commons.configuration2.interpol.ConstantLookup;
23  import org.apache.commons.configuration2.interpol.DefaultLookups;
24  
25  /**
26   * Commons-Config interpolation wrapper. This implementation uses a {@link ConfigurationInterpolator} with the default
27   * lookup: <code>sys</code> (system properties), <code>env</code> (environment variables>, and <code>const</code> (constants).
28   *
29   * <table>
30   *     <tr>
31   *         <th>lookup</th>
32   *         <th>example</th>
33   *         <th>value</th>
34   *     </tr>
35   *     <tr>
36   *         <td>sys</td>
37   *         <td>${os.name}</td>
38   *         <td>mac os x</td>
39   *     </tr>
40   *     <tr>
41   *         <td>env</td>
42   *         <td>${EDITOR} (resolved from environment variables)</td>
43   *         <td>vi</td>
44   *     </tr>
45   *     <tr>
46   *         <td>const</td>
47   *         <td>${const:java.awt.event.KeyEvent.VK_ENTER}</td>
48   *         <td>\n</td>
49   *     </tr>
50   * </table>
51   *
52   * @see ConfigurationInterpolator
53   * @since 1.4
54   */
55  public class CommonsInterpolator implements Interpolator {
56  
57      private final ConfigurationInterpolator interpolator;
58  
59      public CommonsInterpolator() {
60          this.interpolator = new ConfigurationInterpolator();
61  
62          interpolator.registerLookup("const", new ConstantLookup());
63          interpolator.addDefaultLookup(DefaultLookups.SYSTEM_PROPERTIES.getLookup());
64          interpolator.addDefaultLookup(DefaultLookups.ENVIRONMENT.getLookup());
65      }
66  
67      @Override
68      public String interpolate(String value) {
69          return (String) interpolator.interpolate(value);
70      }
71  
72      public ConfigurationInterpolator getConfigurationInterpolator() {
73          return interpolator;
74      }
75  }