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