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