View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.start.config;
20  
21  import java.io.File;
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  import java.net.URL;
25  import java.nio.file.Path;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  import org.eclipse.jetty.start.BaseHome;
30  import org.eclipse.jetty.start.FS;
31  import org.eclipse.jetty.start.Props;
32  import org.eclipse.jetty.start.Props.Prop;
33  import org.eclipse.jetty.start.RawArgs;
34  import org.eclipse.jetty.start.UsageException;
35  import org.eclipse.jetty.start.Utils;
36  
37  /**
38   * Configuration Source representing the Command Line arguments.
39   */
40  public class CommandLineConfigSource implements ConfigSource
41  {
42      public static final String ORIGIN_INTERNAL_FALLBACK = "<internal-fallback>";
43      public static final String ORIGIN_CMD_LINE = "<command-line>";
44  
45      private final RawArgs args;
46      private final Props props;
47      private final Path homePath;
48      private final Path basePath;
49  
50      public CommandLineConfigSource(String rawargs[])
51      {
52          this.args = new RawArgs();
53          this.props = new Props();
54          for (String arg : rawargs)
55          {
56              this.args.addArg(arg,ORIGIN_CMD_LINE);
57              this.props.addPossibleProperty(arg,ORIGIN_CMD_LINE);
58          }
59  
60          // Setup ${jetty.base} and ${jetty.home}
61          this.homePath = findJettyHomePath().toAbsolutePath();
62          this.basePath = findJettyBasePath().toAbsolutePath();
63  
64          // Update System Properties
65          setSystemProperty(BaseHome.JETTY_HOME,homePath.toAbsolutePath().toString());
66          setSystemProperty(BaseHome.JETTY_BASE,basePath.toAbsolutePath().toString());
67      }
68  
69      private final Path findJettyBasePath()
70      {
71          // If a jetty property is defined, use it
72          Prop prop = this.props.getProp(BaseHome.JETTY_BASE,false);
73          if (prop != null && !Utils.isBlank(prop.value))
74          {
75              return FS.toPath(prop.value);
76          }
77  
78          // If a system property is defined, use it
79          String val = System.getProperty(BaseHome.JETTY_BASE);
80          if (!Utils.isBlank(val))
81          {
82              return FS.toPath(val);
83          }
84  
85          // Lastly, fall back to base == ${user.dir}
86          Path base = FS.toPath(this.props.getString("user.dir","."));
87          setProperty(BaseHome.JETTY_BASE,base.toString(),ORIGIN_INTERNAL_FALLBACK);
88          return base;
89      }
90  
91      private final Path findJettyHomePath()
92      {
93          // If a jetty property is defined, use it
94          Prop prop = this.props.getProp(BaseHome.JETTY_HOME,false);
95          if (prop != null && !Utils.isBlank(prop.value))
96          {
97              return FS.toPath(prop.value);
98          }
99  
100         // If a system property is defined, use it
101         String val = System.getProperty(BaseHome.JETTY_HOME);
102         if (!Utils.isBlank(val))
103         {
104             return FS.toPath(val);
105         }
106 
107         // Attempt to find path relative to content in jetty's start.jar
108         // based on lookup for the Main class (from jetty's start.jar)
109         String classRef = "org/eclipse/jetty/start/Main.class";
110         URL jarfile = this.getClass().getClassLoader().getResource(classRef);
111         if (jarfile != null)
112         {
113             Matcher m = Pattern.compile("jar:(file:.*)!/" + classRef).matcher(jarfile.toString());
114             if (m.matches())
115             {
116                 // ${jetty.home} is relative to found BaseHome class
117                 try
118                 {
119                     return new File(new URI(m.group(1))).getParentFile().toPath();
120                 }
121                 catch (URISyntaxException e)
122                 {
123                     throw new UsageException(UsageException.ERR_UNKNOWN,e);
124                 }
125             }
126         }
127 
128         // Lastly, fall back to ${user.dir} default
129         Path home = FS.toPath(System.getProperty("user.dir","."));
130         setProperty(BaseHome.JETTY_HOME,home.toString(),ORIGIN_INTERNAL_FALLBACK);
131         return home;
132     }
133 
134     @Override
135     public boolean equals(Object obj)
136     {
137         if (this == obj)
138         {
139             return true;
140         }
141         if (obj == null)
142         {
143             return false;
144         }
145         if (getClass() != obj.getClass())
146         {
147             return false;
148         }
149         CommandLineConfigSource other = (CommandLineConfigSource)obj;
150         if (args == null)
151         {
152             if (other.args != null)
153             {
154                 return false;
155             }
156         }
157         else if (!args.equals(other.args))
158         {
159             return false;
160         }
161         return true;
162     }
163 
164     @Override
165     public RawArgs getArgs()
166     {
167         return args;
168     }
169 
170     public Path getBasePath()
171     {
172         return basePath;
173     }
174 
175     public Path getHomePath()
176     {
177         return homePath;
178     }
179 
180     @Override
181     public String getId()
182     {
183         return ORIGIN_CMD_LINE;
184     }
185 
186     @Override
187     public String getProperty(String key)
188     {
189         return props.getString(key);
190     }
191 
192     @Override
193     public Props getProps()
194     {
195         return props;
196     }
197 
198     @Override
199     public int getWeight()
200     {
201         return -1; // default value for command line
202     }
203 
204     @Override
205     public int hashCode()
206     {
207         final int prime = 31;
208         int result = 1;
209         result = (prime * result) + ((args == null)?0:args.hashCode());
210         return result;
211     }
212 
213     public void setProperty(String key, String value, String origin)
214     {
215         this.props.setProperty(key,value,origin);
216     }
217 
218     public void setSystemProperty(String key, String value)
219     {
220         this.props.setSystemProperty(key,value);
221     }
222 
223     @Override
224     public String toString()
225     {
226         return String.format("%s[%s,args.length=%d]",this.getClass().getSimpleName(),getId(),getArgs().size());
227     }
228 }