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'> 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.<js>class</js>) 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 * <h5 class='section'>See Also:</h5> 047 * <ul> 048 * <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-svl.VarResolvers">Overview > juneau-svl > VarResolvers and VarResolverSessions</a> 049 * <li class='link'><a class='doclink' href='../../../../../overview-summary.html#juneau-config.Variables'>Overview > juneau-config > Variables</a> 050 * </ul> 051 */ 052public class ConfigVar extends DefaultingVar { 053 054 /** 055 * The name of the session or context object that identifies the {@link Config} object. 056 */ 057 public static final String SESSION_config = "config"; 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.getSessionObject(Config.class, SESSION_config).getString(key); 072 } 073}