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 static org.apache.juneau.common.utils.Utils.*; 020 021import org.apache.juneau.collections.*; 022import org.apache.juneau.svl.*; 023 024/** 025 * JVM args variable resolver. 026 * 027 * <p> 028 * The format for this var is <js>"$A{arg[,default]}"</js>. 029 * 030 * <p> 031 * This variable resolver requires that the command-line arguments be made available through any of the following: 032 * <ul class='spaced-list'> 033 * <li><js>"sun.java.command"</js> system property. 034 * <li><js>"juneau.args"</js> system property. 035 * <li>{@link #init(Args)} has been called. 036 * </ul> 037 * 038 * <h5 class='section'>Example:</h5> 039 * <p class='bjava'> 040 * <jc>// Create an args object from the main(String[]) method.</jc> 041 * Args <jv>args</jv> = <jk>new</jk> Args(<jv>argv</jv>); 042 * 043 * ArgsVar.<jsm>init</jsm>(<jv>args</jv>); 044 * 045 * <jc>// Create a variable resolver that resolves JVM arguments (e.g. "$A{1}")</jc> 046 * VarResolver <jv>varResolver</jv> = VarResolver.<jsm>create</jsm>().vars(ArgsVar.<jk>class</jk>).build(); 047 * 048 * <jc>// Use it!</jc> 049 * System.<jsf>out</jsf>.println(<jv>varResolver</jv>.resolve(<js>"Arg #1 is set to $A{1}"</js>)); 050 * </p> 051 * 052 * <p> 053 * Since this is a {@link SimpleVar}, any variables contained in the result will be recursively resolved. 054 * Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var. 055 * 056 * <h5 class='section'>See Also:</h5><ul> 057 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SimpleVariableLanguageBasics">Simple Variable Language Basics</a> 058 * </ul> 059 */ 060public class ArgsVar extends DefaultingVar { 061 062 /** The name of this variable. */ 063 public static final String NAME = "A"; 064 065 private static volatile Args ARGS; 066 067 /** 068 * Initialize the args for this variable. 069 * 070 * @param args The parsed command-line arguments. 071 */ 072 public static void init(Args args) { 073 ARGS = args; 074 } 075 076 private final Args args; 077 078 /** 079 * Constructor. 080 */ 081 public ArgsVar() { 082 super(NAME); 083 if (ARGS != null) 084 this.args = ARGS; 085 else { 086 String s = System.getProperty("sun.java.command"); 087 if (isNotEmpty(s)) { 088 int i = s.indexOf(' '); 089 args = new Args(i == -1 ? "" : s.substring(i+1)); 090 } else { 091 args = new Args(System.getProperty("juneau.args", "")); 092 } 093 } 094 } 095 096 @Override /* Var */ 097 public String resolve(VarResolverSession session, String key) { 098 return args.getArg(key); 099 } 100}