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 sources = new ConfigSources();
134 sources.add(cmdLineSource);
135 this.homeDir = cmdLineSource.getHomePath();
136 this.baseDir = cmdLineSource.getBasePath();
137
138
139
140 StartLog.getInstance().initialize(this,cmdLineSource);
141
142 sources.add(new JettyBaseConfigSource(cmdLineSource.getBasePath()));
143 sources.add(new JettyHomeConfigSource(cmdLineSource.getHomePath()));
144
145 System.setProperty(JETTY_HOME,homeDir.toAbsolutePath().toString());
146 System.setProperty(JETTY_BASE,baseDir.toAbsolutePath().toString());
147 }
148
149 public BaseHome(ConfigSources sources)
150 {
151 this.sources = sources;
152 Path home = null;
153 Path base = null;
154 for (ConfigSource source : sources)
155 {
156 if (source instanceof CommandLineConfigSource)
157 {
158 CommandLineConfigSource cmdline = (CommandLineConfigSource)source;
159 home = cmdline.getHomePath();
160 base = cmdline.getBasePath();
161 }
162 else if (source instanceof JettyBaseConfigSource)
163 {
164 base = ((JettyBaseConfigSource)source).getDir();
165 }
166 else if (source instanceof JettyHomeConfigSource)
167 {
168 home = ((JettyHomeConfigSource)source).getDir();
169 }
170 }
171
172 Objects.requireNonNull(home,"jetty.home cannot be null");
173 this.homeDir = home;
174 this.baseDir = (base != null)?base:home;
175
176 System.setProperty(JETTY_HOME,homeDir.toAbsolutePath().toString());
177 System.setProperty(JETTY_BASE,baseDir.toAbsolutePath().toString());
178 }
179
180 public String getBase()
181 {
182 if (baseDir == null)
183 {
184 return null;
185 }
186 return baseDir.toString();
187 }
188
189 public Path getBasePath()
190 {
191 return baseDir;
192 }
193
194
195
196
197
198
199
200
201 public Path getBasePath(String path)
202 {
203 return baseDir.resolve(path).normalize().toAbsolutePath();
204 }
205
206 public ConfigSources getConfigSources()
207 {
208 return this.sources;
209 }
210
211 public String getHome()
212 {
213 return homeDir.toString();
214 }
215
216 public Path getHomePath()
217 {
218 return homeDir;
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 public Path getPath(final String path)
238 {
239 Path apath = FS.toPath(path);
240
241 if (apath.isAbsolute())
242 {
243 if (FS.exists(apath))
244 {
245 return apath;
246 }
247 }
248
249 for (ConfigSource source : sources)
250 {
251 if (source instanceof DirConfigSource)
252 {
253 DirConfigSource dirsource = (DirConfigSource)source;
254 Path file = dirsource.getDir().resolve(apath);
255 if (FS.exists(file))
256 {
257 return file;
258 }
259 }
260 }
261
262
263 return FS.toPath(path);
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279 public List<Path> getPaths(Path dir, int searchDepth, String pattern) throws IOException
280 {
281 if (PathMatchers.isAbsolute(pattern))
282 {
283 throw new RuntimeException("Pattern cannot be absolute: " + pattern);
284 }
285
286 List<Path> hits = new ArrayList<>();
287 if (FS.isValidDirectory(dir))
288 {
289 PathMatcher matcher = PathMatchers.getMatcher(pattern);
290 PathFinder finder = new PathFinder();
291 finder.setFileMatcher(matcher);
292 finder.setBase(dir);
293 finder.setIncludeDirsInResults(true);
294 Files.walkFileTree(dir,SEARCH_VISIT_OPTIONS,searchDepth,finder);
295 hits.addAll(finder.getHits());
296 Collections.sort(hits,new NaturalSort.Paths());
297 }
298 return hits;
299 }
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 public List<Path> getPaths(String pattern) throws IOException
352 {
353 StartLog.debug("getPaths('%s')",pattern);
354 List<Path> hits = new ArrayList<>();
355
356 if (PathMatchers.isAbsolute(pattern))
357 {
358
359
360
361 Path root = PathMatchers.getSearchRoot(pattern);
362
363 PathMatcher matcher = PathMatchers.getMatcher(pattern);
364
365 if (FS.isValidDirectory(root))
366 {
367 PathFinder finder = new PathFinder();
368 finder.setIncludeDirsInResults(true);
369 finder.setFileMatcher(matcher);
370 finder.setBase(root);
371 Files.walkFileTree(root,SEARCH_VISIT_OPTIONS,MAX_SEARCH_DEPTH,finder);
372 hits.addAll(finder.getHits());
373 }
374 }
375 else
376 {
377
378 Path relativePath = PathMatchers.getSearchRoot(pattern);
379 PathMatcher matcher = PathMatchers.getMatcher(pattern);
380 PathFinder finder = new PathFinder();
381 finder.setIncludeDirsInResults(true);
382 finder.setFileMatcher(matcher);
383
384
385 ListIterator<ConfigSource> iter = sources.reverseListIterator();
386 while (iter.hasPrevious())
387 {
388 ConfigSource source = iter.previous();
389 if (source instanceof DirConfigSource)
390 {
391 DirConfigSource dirsource = (DirConfigSource)source;
392 Path dir = dirsource.getDir();
393 Path deepDir = dir.resolve(relativePath);
394 if (FS.isValidDirectory(deepDir))
395 {
396 finder.setBase(dir);
397 Files.walkFileTree(deepDir,SEARCH_VISIT_OPTIONS,MAX_SEARCH_DEPTH,finder);
398 }
399 }
400 }
401
402 hits.addAll(finder.getHits());
403 }
404
405 Collections.sort(hits,new NaturalSort.Paths());
406 return hits;
407 }
408
409 public boolean isBaseDifferent()
410 {
411 return homeDir.compareTo(baseDir) != 0;
412 }
413
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 }