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