001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.config.vars;
014
015import org.apache.juneau.config.*;
016import org.apache.juneau.svl.*;
017
018/**
019 * Config file variable resolver.
020 *
021 * <p>
022 * The format for this var is <js>"$C{key[,defaultValue]}"</js>.
023 * See {@link Config#getString(String)} for the format of the key.
024 *
025 * <p>
026 * This variable resolver requires that a {@link Config} object be set as a context object on the resolver or a
027 * session object on the resolver session.
028 *
029 * <h5 class='section'>Example:</h5>
030 * <p class='bcode w800'>
031 *    <jc>// Create a config object.</jc>
032 *    Config config = Config.<jsm>create</jsm>().name(<js>"MyConfig.cfg"</js>).build();
033 *
034 *    <jc>// Create a variable resolver that resolves config file entries (e.g. "$C{MySection/myKey}")</jc>
035 *    VarResolver r = <jk>new</jk> VarResolver().addVars(ConfigVar.<jk>class</jk>)
036 *       .addContextObject(<jsf>SESSION_config</jsf>, configFile);
037 *
038 *    <jc>// Use it!</jc>
039 *    System.<jsf>out</jsf>.println(r.resolve(<js>"Value for myKey in section MySection is $C{MySection/myKey}"</js>));
040 * </p>
041 *
042 * <p>
043 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved.
044 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var.
045 *
046 * <ul class='seealso'>
047 *    <li class='link'>{@doc VarResolvers}
048 *    <li class='link'>{@doc ConfigVariables}
049 * </ul>
050 */
051public class ConfigVar extends DefaultingVar {
052
053   /**
054    * The name of the session or context object that identifies the {@link Config} object.
055    */
056   public static final String SESSION_config = "config";
057
058   /** The name of this variable. */
059   public static final String NAME = "C";
060
061   /**
062    * Constructor.
063    */
064   public ConfigVar() {
065      super(NAME);
066   }
067
068   @Override /* Var */
069   public String resolve(VarResolverSession session, String key) {
070      return session.getSessionObject(Config.class, SESSION_config, true).getString(key);
071   }
072}