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.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='bcode w800'>
036 *    <jc>// Create an args object from the main(String[]) method.</jc>
037 *    Args args = new Args(argv);
038 *
039 *    ArgsVar.<jsm>init</jsm>(args);
040 *
041 *    <jc>// Create a variable resolver that resolves JVM arguments (e.g. "$A{1}")</jc>
042 *    VarResolver r = <jk>new</jk> VarResolver().addVars(ArgsVar.<jk>class</jk>);
043 *
044 *    <jc>// Use it!</jc>
045 *    System.<jsf>out</jsf>.println(r.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 * @see org.apache.juneau.collections.Args
053 * @see org.apache.juneau.svl
054 */
055public class ArgsVar extends DefaultingVar {
056
057   /** The name of this variable. */
058   public static final String NAME = "A";
059
060   private static volatile Args ARGS;
061
062   /**
063    * Initialize the args for this variable.
064    *
065    * @param args The parsed command-line arguments.
066    */
067   public static void init(Args args) {
068      ARGS = args;
069   }
070
071   private final Args args;
072
073   /**
074    * Constructor.
075    */
076   public ArgsVar() {
077      super(NAME);
078      if (ARGS != null)
079         this.args = ARGS;
080      else {
081         String s = System.getProperty("sun.java.command");
082         if (isNotEmpty(s)) {
083            int i = s.indexOf(' ');
084            args = new Args(i == -1 ? "" : s.substring(i+1));
085         } else {
086            args = new Args(System.getProperty("juneau.args", ""));
087         }
088      }
089   }
090
091   @Override /* Var */
092   public String resolve(VarResolverSession session, String key) {
093      return args.getArg(key);
094   }
095}