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.commons.svl.vars; 018 019import java.util.function.*; 020 021import org.apache.juneau.commons.runtime.*; 022import org.apache.juneau.commons.settings.*; 023import org.apache.juneau.commons.svl.*; 024 025/** 026 * Manifest file entries variable resolver. 027 * 028 * <p> 029 * The format for this var is <js>"$MF{key[,default]}"</js>. 030 * 031 * <p> 032 * This variable resolver requires that a {@link ManifestFile} object be made available by either: 033 * <ul class='spaced-list'> 034 * <li>Classpath/default manifest discovery when using the no-arg constructor. 035 * <li>Constructing the var via {@link #create(Supplier)} (per-instance). 036 * </ul> 037 * 038 * <h5 class='section'>Example:</h5> 039 * <p class='bjava'> 040 * <jc>// Create a ManifestFile object that contains the manifest of the jar file containing this class.</jc> 041 * ManifestFile <jv>manifestFile</jv> = <jk>new</jk> ManifestFile(<jk>this</jk>.getClass()); 042 * 043 * <jc>// Create a variable resolver that resolves manifest file entries (e.g. "$MF{Main-Class}")</jc> 044 * VarResolver <jv>varResolver</jv> = VarResolver.<jsm>create</jsm>().vars(ManifestFileVar.<jsm>create</jsm>(() -> <jv>manifestFile</jv>)).build(); 045 * 046 * <jc>// Use it!</jc> 047 * System.<jsf>out</jsf>.println(<jv>varResolver</jv>.resolve(<js>"The main class is $MF{Main-Class}"</js>)); 048 * </p> 049 * 050 * <p> 051 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved. 052 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var. 053 * 054 * <h5 class='section'>See Also:</h5><ul> 055 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/MarshallSimpleVariableLanguage">Marshall Simple Variable Language</a> 056 * </ul> 057 */ 058public class ManifestFileVar extends DefaultingVar { 059 060 /** The name of this variable. */ 061 public static final String NAME = "MF"; 062 063 /** 064 * Creates a {@link ManifestFileVar} bound to a per-instance {@link Supplier} of {@link ManifestFile}. 065 * 066 * <p> 067 * The supplier is invoked on every resolve. 068 * 069 * @param supplier The supplier of {@link ManifestFile} for this var instance. Must not be <jk>null</jk>. 070 * @return A new {@link ManifestFileVar} instance backed by the supplier. 071 */ 072 public static ManifestFileVar create(Supplier<ManifestFile> supplier) { 073 return new ManifestFileVar(supplier); 074 } 075 076 private final ManifestFilePropertySource source; 077 078 /** 079 * Constructor. 080 */ 081 public ManifestFileVar() { 082 super(NAME); 083 this.source = ManifestFilePropertySource.createDefault(); 084 } 085 086 private ManifestFileVar(Supplier<ManifestFile> supplier) { 087 super(NAME); 088 this.source = new ManifestFilePropertySource(supplier); 089 } 090 091 @Override /* Overridden from Var */ 092 public String resolve(VarResolverSession session, String key) { 093 var v = source.get(key); 094 return v.isPresent() ? v.value().orElse(null) : ""; 095 } 096 097 /** 098 * Manifest entries are immutable for the lifetime of the classpath. This var opts in to 099 * compile-time stable-value folding. 100 * 101 * @return Always {@code true}. 102 */ 103 @Override /* Overridden from Var */ 104 protected boolean isStable() { 105 return true; 106 } 107}