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='bcode w800'>
030 *    <jc>// Create a ManifestFile object that contains the manifest of the jar file containing this class.</jc>
031 *    ManifestFile mf = <jk>new</jk> ManifestFile(<jk>this</jk>.getClass());
032 *
033 *    ManifestFileVar.<jsm>init</jsm>(mf);
034 *
035 *    <jc>// Create a variable resolver that resolves manifest file entries (e.g. "$MF{Main-Class}")</jc>
036 *    VarResolver r = <jk>new</jk> VarResolver().addVars(ManifestFile.<jk>class</jk>);
037 *
038 *    <jc>// Use it!</jc>
039 *    System.<jsf>out</jsf>.println(r.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 * @see org.apache.juneau.utils.ManifestFile
047 * @see org.apache.juneau.svl
048 */
049public class ManifestFileVar extends DefaultingVar {
050
051   /** The name of this variable. */
052   public static final String NAME = "MF";
053
054   private static volatile ManifestFile MANIFEST_FILE;
055
056   /**
057    * Initialize the manifest file for this variable.
058    *
059    * @param manifestFile The parsed manifest file.
060    */
061   public static void init(ManifestFile manifestFile) {
062      MANIFEST_FILE = manifestFile;
063   }
064
065   private final ManifestFile manifestFile;
066
067   /**
068    * Constructor.
069    */
070   public ManifestFileVar() {
071      super(NAME);
072      this.manifestFile = MANIFEST_FILE;
073   }
074
075   @Override /* Var */
076   public String resolve(VarResolverSession session, String key) {
077      return manifestFile == null ? "" : manifestFile.getString(key);
078   }
079}