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#get(String)} for the format of the key.
024 *
025 * <p>
026 * This variable resolver requires that a {@link Config} bean be available in the resolver session bean factory.
027 *
028 * <h5 class='figure'>Example:</h5>
029 * <p class='bjava'>
030 *    <jc>// Create a config object.</jc>
031 *    Config <jv>config</jv> = Config.<jsm>create</jsm>().name(<js>"MyConfig.cfg"</js>).build();
032 *
033 *    <jc>// Create a variable resolver that resolves config file entries (e.g. "$C{MySection/myKey}")</jc>
034 *    VarResolver <jv>resolver</jv> = VarResolver
035 *       .<jsm>create</jsm>()
036 *       .vars(ConfigVar.<jk>class</jk>)
037 *       .bean(Config.<jk>class</jk>, <jv>config</jv>)
038 *       .build();
039 *
040 *    <jc>// Use it!</jc>
041 *    System.<jsf>out</jsf>.println(<jv>resolver</jv>.resolve(<js>"Value for myKey in section MySection is $C{MySection/myKey}"</js>));
042 * </p>
043 *
044 * <p>
045 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved.
046 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var.
047 *
048 * <h5 class='section'>See Also:</h5><ul>
049 *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.SimpleVariableLanguage">Overview &gt; juneau-marshall &gt; Simple Variable Language</a>
050 *    <li class='link'><a class="doclink" href="../../../../../index.html#jc.Variables">Overview &gt; juneau-config &gt; Variables</a>
051 * </ul>
052 */
053public class ConfigVar extends DefaultingVar {
054
055   /** The name of this variable. */
056   public static final String NAME = "C";
057
058   /**
059    * Constructor.
060    */
061   public ConfigVar() {
062      super(NAME);
063   }
064
065   @Override /* Var */
066   public String resolve(VarResolverSession session, String key) {
067      return session.getBean(Config.class).get().get(key).orElse(null);
068   }
069
070   @Override /* Var */
071   public boolean canResolve(VarResolverSession session) {
072      return session.getBean(Config.class).isPresent();
073   }
074}