1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
61 this.homePath = findJettyHomePath().toAbsolutePath();
62 this.basePath = findJettyBasePath().toAbsolutePath();
63
64
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
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
79 String val = System.getProperty(BaseHome.JETTY_BASE);
80 if (!Utils.isBlank(val))
81 {
82 return FS.toPath(val);
83 }
84
85
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
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
101 String val = System.getProperty(BaseHome.JETTY_HOME);
102 if (!Utils.isBlank(val))
103 {
104 return FS.toPath(val);
105 }
106
107
108
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
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
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;
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 }