001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.config;
020
021import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
022import org.apache.commons.configuration2.interpol.ConstantLookup;
023import org.apache.commons.configuration2.interpol.EnvironmentLookup;
024import org.apache.commons.configuration2.interpol.SystemPropertiesLookup;
025
026/**
027 * Commons-Config interpolation wrapper. This implementation uses a {@link ConfigurationInterpolator} with the default
028 * lookup: <code>sys</code> (system properties), <code>env</code> (environment variables>, and <code>const</code> (constants).
029 *
030 * <table>
031 *     <tr>
032 *         <th>lookup</th>
033 *         <th>example</th>
034 *         <th>value</th>
035 *     </tr>
036 *     <tr>
037 *         <td>sys</td>
038 *         <td>${sys:os.name}</td>
039 *         <td>mac os x</td>
040 *     </tr>
041 *     <tr>
042 *         <td>env</td>
043 *         <td>${env:EDITOR}</td>
044 *         <td>vi</td>
045 *     </tr>
046 *     <tr>
047 *         <td>const</td>
048 *         <td>${const:java.awt.event.KeyEvent.VK_ENTER}</td>
049 *         <td>\n</td>
050 *     </tr>
051 * </table>
052 *
053 * @see ConfigurationInterpolator
054 * @since 1.4
055 */
056public class CommonsInterpolator implements Interpolator {
057
058    final private ConfigurationInterpolator interpolator;
059
060    public CommonsInterpolator() {
061        this.interpolator = new ConfigurationInterpolator();
062
063        interpolator.registerLookup("const", new ConstantLookup());
064        interpolator.addDefaultLookup(new SystemPropertiesLookup());
065        interpolator.addDefaultLookup(new EnvironmentLookup());
066    }
067
068    @Override
069    public String interpolate(String value) {
070        return (String) interpolator.interpolate(value);
071    }
072
073    public ConfigurationInterpolator getConfigurationInterpolator() {
074        return interpolator;
075    }
076}