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.ogdl;
020
021import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
022import org.apache.commons.configuration2.interpol.ConstantLookup;
023import org.apache.commons.configuration2.interpol.DefaultLookups;
024
025/**
026 * Commons-Config interpolation wrapper. This implementation uses a {@link ConfigurationInterpolator} with the default
027 * lookup: <code>sys</code> (system properties), <code>env</code> (environment variables>, and <code>const</code> (constants).
028 *
029 * <table>
030 *     <tr>
031 *         <th>lookup</th>
032 *         <th>example</th>
033 *         <th>value</th>
034 *     </tr>
035 *     <tr>
036 *         <td>sys</td>
037 *         <td>${os.name}</td>
038 *         <td>mac os x</td>
039 *     </tr>
040 *     <tr>
041 *         <td>env</td>
042 *         <td>${EDITOR} (resolved from environment variables)</td>
043 *         <td>vi</td>
044 *     </tr>
045 *     <tr>
046 *         <td>const</td>
047 *         <td>${const:java.awt.event.KeyEvent.VK_ENTER}</td>
048 *         <td>\n</td>
049 *     </tr>
050 * </table>
051 *
052 * @see ConfigurationInterpolator
053 * @since 1.4
054 */
055public class CommonsInterpolator implements Interpolator {
056
057    private final ConfigurationInterpolator interpolator;
058
059    public CommonsInterpolator() {
060        this.interpolator = new ConfigurationInterpolator();
061
062        interpolator.registerLookup("const", new ConstantLookup());
063        interpolator.addDefaultLookup(DefaultLookups.SYSTEM_PROPERTIES.getLookup());
064        interpolator.addDefaultLookup(DefaultLookups.ENVIRONMENT.getLookup());
065    }
066
067    @Override
068    public String interpolate(String value) {
069        return (String) interpolator.interpolate(value);
070    }
071
072    public ConfigurationInterpolator getConfigurationInterpolator() {
073        return interpolator;
074    }
075}