1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.start;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.FileVisitOption;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.PathMatcher;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.EnumSet;
30 import java.util.List;
31 import java.util.ListIterator;
32 import java.util.Objects;
33
34 import org.eclipse.jetty.start.config.CommandLineConfigSource;
35 import org.eclipse.jetty.start.config.ConfigSource;
36 import org.eclipse.jetty.start.config.ConfigSources;
37 import org.eclipse.jetty.start.config.DirConfigSource;
38 import org.eclipse.jetty.start.config.JettyBaseConfigSource;
39 import org.eclipse.jetty.start.config.JettyHomeConfigSource;
40
41
42
43
44
45
46
47
48
49
50 public class BaseHome
51 {
52 public static class SearchDir
53 {
54 private Path dir;
55 private String name;
56
57 public SearchDir(String name)
58 {
59 this.name = name;
60 }
61
62 public Path getDir()
63 {
64 return dir;
65 }
66
67 public Path resolve(Path subpath)
68 {
69 return dir.resolve(subpath);
70 }
71
72 public Path resolve(String subpath)
73 {
74 return dir.resolve(FS.separators(subpath));
75 }
76
77 public SearchDir setDir(File path)
78 {
79 if (path != null)
80 {
81 return setDir(path.toPath());
82 }
83 return this;
84 }
85
86 public SearchDir setDir(Path path)
87 {
88 if (path != null)
89 {
90 this.dir = path.toAbsolutePath();
91 }
92 return this;
93 }
94
95 public SearchDir setDir(String path)
96 {
97 if (path != null)
98 {
99 return setDir(FS.toPath(path));
100 }
101 return this;
102 }
103
104 public String toShortForm(Path path)
105 {
106 Path relative = dir.relativize(path);
107 return String.format("${%s}%c%s",name,File.separatorChar,relative.toString());
108 }
109 }
110
111 public static final String JETTY_BASE = "jetty.base";
112 public static final String JETTY_HOME = "jetty.home";
113 private final static EnumSet<FileVisitOption> SEARCH_VISIT_OPTIONS = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
114
115 private final static int MAX_SEARCH_DEPTH = Integer.getInteger("org.eclipse.jetty.start.searchDepth",10);
116
117 private final ConfigSources sources;
118 private final Path homeDir;
119 private final Path baseDir;
120
121 public BaseHome() throws IOException
122 {
123 this(new String[0]);
124 }
125
126 public BaseHome(String cmdLine[]) throws IOException
127 {
128 this(new CommandLineConfigSource(cmdLine));
129 }
130
131 public BaseHome(CommandLineConfigSource cmdLineSource) throws IOException
132 {
133
134 sources = new ConfigSources();
135 sources.add(cmdLineSource);
136 this.homeDir = cmdLineSource.getHomePath();
137 this.baseDir = cmdLineSource.getBasePath();
138
139
140
141 StartLog.getInstance().initialize(this,cmdLineSource);
142
143 sources.add(new JettyBaseConfigSource(cmdLineSource.getBasePath()));
144 sources.add(new JettyHomeConfigSource(cmdLineSource.getHomePath()));
145
146 System.setProperty(JETTY_HOME,homeDir.toAbsolutePath().toString());
147 System.setProperty(JETTY_BASE,baseDir.toAbsolutePath().toString());
148 }
149
150 public BaseHome(ConfigSources sources)
151 {
152 this.sources = sources;
153 Path home = null;
154 Path base = null;
155 for (ConfigSource source : sources)
156 {
157 if (source instanceof CommandLineConfigSource)
158 {
159 CommandLineConfigSource cmdline = (CommandLineConfigSource)source;
160 home = cmdline.getHomePath();
161 base = cmdline.getBasePath();
162 }
163 else if (source instanceof JettyBaseConfigSource)
164 {
165 base = ((JettyBaseConfigSource)source).getDir();
166 }
167 else if (source instanceof JettyHomeConfigSource)
168 {
169 home = ((JettyHomeConfigSource)source).getDir();
170 }
171 }
172
173 Objects.requireNonNull(home,"jetty.home cannot be null");
174 this.homeDir = home;
175 this.baseDir = (base != null)?base:home;
176
177 System.setProperty(JETTY_HOME,homeDir.toAbsolutePath().toString());
178 System.setProperty(JETTY_BASE,baseDir.toAbsolutePath().toString());
179 }
180
181 public String getBase()
182 {
183 if (baseDir == null)
184 {
185 return null;
186 }
187 return baseDir.toString();
188 }
189
190 public Path getBasePath()
191 {
192 return baseDir;
193 }
194
195
196
197
198
199
200
201
202 public Path getBasePath(String path)
203 {
204 return baseDir.resolve(path);
205 }
206
207 public ConfigSources getConfigSources()
208 {
209 return this.sources;
210 }
211
212 public String getHome()
213 {
214 return homeDir.toString();
215 }
216
217 public Path getHomePath()
218 {
219 return homeDir;
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 public Path getPath(final String path)
239 {
240 Path apath = FS.toPath(path);
241
242 if (apath.isAbsolute())
243 {
244 if (FS.exists(apath))
245 {
246 return apath;
247 }
248 }
249
250 for (ConfigSource source : sources)
251 {
252 if (source instanceof DirConfigSource)
253 {
254 DirConfigSource dirsource = (DirConfigSource)source;
255 Path file = dirsource.getDir().resolve(apath);
256 if (FS.exists(file))
257 {
258 return file;
259 }
260 }
261 }
262
263
264 return FS.toPath(path);
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 public List<Path> getPaths(Path dir, int searchDepth, String pattern) throws IOException
281 {
282 if (PathMatchers.isAbsolute(pattern))
283 {
284 throw new RuntimeException("Pattern cannot be absolute: " + pattern);
285 }
286
287 List<Path> hits = new ArrayList<>();
288 if (FS.isValidDirectory(dir))
289 {
290 PathMatcher matcher = PathMatchers.getMatcher(pattern);
291 PathFinder finder = new PathFinder();
292 finder.setFileMatcher(matcher);
293 finder.setBase(dir);
294 finder.setIncludeDirsInResults(true);
295 Files.walkFileTree(dir,SEARCH_VISIT_OPTIONS,searchDepth,finder);
296 hits.addAll(finder.getHits());
297 Collections.sort(hits,new NaturalSort.Paths());
298 }
299 return hits;
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353 public List<Path> getPaths(String pattern) throws IOException
354 {
355 StartLog.debug("getPaths('%s')",pattern);
356 List<Path> hits = new ArrayList<>();
357
358 if (PathMatchers.isAbsolute(pattern))
359 {
360
361
362
363 Path root = PathMatchers.getSearchRoot(pattern);
364
365 PathMatcher matcher = PathMatchers.getMatcher(pattern);
366
367 if (FS.isValidDirectory(root))
368 {
369 PathFinder finder = new PathFinder();
370 finder.setIncludeDirsInResults(true);
371 finder.setFileMatcher(matcher);
372 finder.setBase(root);
373 Files.walkFileTree(root,SEARCH_VISIT_OPTIONS,MAX_SEARCH_DEPTH,finder);
374 hits.addAll(finder.getHits());
375 }
376 }
377 else
378 {
379
380 Path relativePath = PathMatchers.getSearchRoot(pattern);
381 PathMatcher matcher = PathMatchers.getMatcher(pattern);
382 PathFinder finder = new PathFinder();
383 finder.setIncludeDirsInResults(true);
384 finder.setFileMatcher(matcher);
385
386
387 ListIterator<ConfigSource> iter = sources.reverseListIterator();
388 while (iter.hasPrevious())
389 {
390 ConfigSource source = iter.previous();
391 if (source instanceof DirConfigSource)
392 {
393 DirConfigSource dirsource = (DirConfigSource)source;
394 Path dir = dirsource.getDir();
395 Path deepDir = dir.resolve(relativePath);
396 if (FS.isValidDirectory(deepDir))
397 {
398 finder.setBase(dir);
399 Files.walkFileTree(deepDir,SEARCH_VISIT_OPTIONS,MAX_SEARCH_DEPTH,finder);
400 }
401 }
402 }
403
404 hits.addAll(finder.getHits());
405 }
406
407 Collections.sort(hits,new NaturalSort.Paths());
408 return hits;
409 }
410
411 public boolean isBaseDifferent()
412 {
413 return homeDir.compareTo(baseDir) != 0;
414 }
415
416
417
418
419 public String toShortForm(final File path)
420 {
421 return toShortForm(path.toPath());
422 }
423
424
425
426
427
428
429
430
431 public String toShortForm(final Path path)
432 {
433 Path apath = path.toAbsolutePath();
434
435 for (ConfigSource source : sources)
436 {
437 if (source instanceof DirConfigSource)
438 {
439 DirConfigSource dirsource = (DirConfigSource)source;
440 Path dir = dirsource.getDir();
441 if (apath.startsWith(dir))
442 {
443 if (dirsource.isPropertyBased())
444 {
445 Path relative = dir.relativize(apath);
446 return String.format("%s%c%s",dirsource.getId(),File.separatorChar,relative.toString());
447 }
448 else
449 {
450 return apath.toString();
451 }
452 }
453 }
454 }
455
456 return apath.toString();
457 }
458
459
460
461
462
463
464
465
466 public String toShortForm(final String path)
467 {
468 if ((path == null) || (path.charAt(0) == '<'))
469 {
470 return path;
471 }
472
473 return toShortForm(FS.toPath(path));
474 }
475 }