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.svl.vars;
014
015import org.apache.juneau.svl.*;
016import org.apache.juneau.utils.*;
017
018/**
019 * Manifest file entries variable resolver.
020 *
021 * <p>
022 * The format for this var is <js>"$MF{key[,default]}"</js>.
023 *
024 * <p>
025 * This variable resolver requires that a {@link ManifestFile} object be made available by calling
026 * the {@link #init(ManifestFile)} method.
027 *
028 * <h5 class='section'>Example:</h5>
029 * <p class='bjava'>
030 *    <jc>// Create a ManifestFile object that contains the manifest of the jar file containing this class.</jc>
031 *    ManifestFile <jv>manifestFile</jv> = <jk>new</jk> ManifestFile(<jk>this</jk>.getClass());
032 *
033 *    ManifestFileVar.<jsm>init</jsm>(<jv>manifestFile</jv>);
034 *
035 *    <jc>// Create a variable resolver that resolves manifest file entries (e.g. "$MF{Main-Class}")</jc>
036 *    VarResolver <jv>varResolver</jv> = VarResolver.<jsm>create</jsm>().vars(ManifestFile.<jk>class</jk>).build();
037 *
038 *    <jc>// Use it!</jc>
039 *    System.<jsf>out</jsf>.println(<jv>varResolver</jv>.resolve(<js>"The main class is $MF{Main-Class}"</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><ul>
047 *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.SimpleVariableLanguage">Simple Variable Language</a>
048 * </ul>
049 */
050public class ManifestFileVar extends DefaultingVar {
051
052   /** The name of this variable. */
053   public static final String NAME = "MF";
054
055   private static volatile ManifestFile MANIFEST_FILE;
056
057   /**
058    * Initialize the manifest file for this variable.
059    *
060    * @param manifestFile The parsed manifest file.
061    */
062   public static void init(ManifestFile manifestFile) {
063      MANIFEST_FILE = manifestFile;
064   }
065
066   /**
067    * Constructor.
068    */
069   public ManifestFileVar() {
070      super(NAME);
071   }
072
073   @Override /* Var */
074   public String resolve(VarResolverSession session, String key) {
075      return MANIFEST_FILE == null ? "" : MANIFEST_FILE.getString(key);
076   }
077}