Skip to content

Commit b36c8a7

Browse files
committed
Polish "Tighten up PropertiesLauncher's contract"
See gh-8346 Closes gh-7221
1 parent e4c807b commit b36c8a7

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

spring-boot-docs/src/main/asciidoc/appendix-executable-jar-format.adoc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ NOTE: `PropertiesLauncher` supports loading properties from
214214
just like a regular `-classpath` on the `javac` command line.
215215

216216
|`loader.home`
217-
|Used to resolve relative paths in `loader.path`. E.g. `loader.path=lib` then `${loader.home}/lib` is a classpath location (along with all jar files in that directory). Also used to locate a `loader.properties file`. Example `file:///opt/app` (defaults to `${user.dir}`).
217+
|Used to resolve relative paths in `loader.path`. E.g. `loader.path=lib` then
218+
`${loader.home}/lib` is a classpath location (along with all jar files in that
219+
directory). Also used to locate a `loader.properties file`. Example `file:///opt/app`
220+
(defaults to `${user.dir}`).
218221

219222
|`loader.args`
220223
|Default arguments for the main method (space separated)
@@ -271,21 +274,24 @@ TIP: Build plugins automatically move the `Main-Class` attribute to `Start-Class
271274
the fat jar is built. If you are using that, specify the name of the class to launch using
272275
the `Main-Class` attribute and leave out `Start-Class`.
273276

274-
* `loader.properties` are searched for in `loader.home` then in the root of the classpath,
275-
then in `classpath:/BOOT-INF/classes`. The first location that exists is used.
276-
* `loader.home` is only the directory location of an additional properties file (overriding
277-
the default) as long as `loader.config.location` is not specified.
277+
* `loader.properties` are searched for in `loader.home` then in the root of the
278+
classpath, then in `classpath:/BOOT-INF/classes`. The first location that exists is
279+
used.
280+
* `loader.home` is only the directory location of an additional properties file
281+
(overriding the default) as long as `loader.config.location` is not specified.
278282
* `loader.path` can contain directories (scanned recursively for jar and zip files),
279283
archive paths, or wildcard patterns (for the default JVM behavior).
280284
* `loader.path` (if empty) defaults to `BOOT-INF/lib` (meaning a local directory or a
281285
nested one if running from an archive). Because of this `PropertiesLauncher` behaves the
282286
same as `JarLauncher` when no additional configuration is provided.
283-
* `loader.path` can not be used to configure the location of `loader.properties` (the classpath
284-
used to search for the latter is the JVM classpath when `PropertiesLauncher` is launched).
287+
* `loader.path` can not be used to configure the location of `loader.properties` (the
288+
classpath used to search for the latter is the JVM classpath when `PropertiesLauncher`
289+
is launched).
285290
* Placeholder replacement is done from System and environment variables plus the
286291
properties file itself on all values before use.
287292
* The search order for properties (where it makes sense to look in more than one place)
288-
is env vars, system properties, `loader.properties`, exploded archive manifest, archive manifest.
293+
is env vars, system properties, `loader.properties`, exploded archive manifest, archive
294+
manifest.
289295

290296

291297

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private void initializeProperties() throws Exception, IOException {
167167
for (String config : configs) {
168168
InputStream resource = getResource(config);
169169
if (resource != null) {
170-
log("Found: " + config);
170+
debug("Found: " + config);
171171
try {
172172
this.properties.load(resource);
173173
}
@@ -177,7 +177,7 @@ private void initializeProperties() throws Exception, IOException {
177177
for (Object key : Collections.list(this.properties.propertyNames())) {
178178
if (config.endsWith("application.properties")
179179
&& ((String) key).startsWith("loader.")) {
180-
warn("WARNING: use of application.properties for PropertiesLauncher is deprecated");
180+
warn("Use of application.properties for PropertiesLauncher is deprecated");
181181
}
182182
String text = this.properties.getProperty((String) key);
183183
String value = SystemPropertyUtils
@@ -187,7 +187,7 @@ private void initializeProperties() throws Exception, IOException {
187187
}
188188
}
189189
if ("true".equals(getProperty(SET_SYSTEM_PROPERTIES))) {
190-
log("Adding resolved properties to System properties");
190+
debug("Adding resolved properties to System properties");
191191
for (Object key : Collections.list(this.properties.propertyNames())) {
192192
String value = this.properties.getProperty((String) key);
193193
System.setProperty((String) key, value);
@@ -197,7 +197,7 @@ private void initializeProperties() throws Exception, IOException {
197197
return;
198198
}
199199
else {
200-
log("Not found: " + config);
200+
debug("Not found: " + config);
201201
}
202202
}
203203
}
@@ -232,13 +232,13 @@ private InputStream getClasspathResource(String config) {
232232
config = config.substring(1);
233233
}
234234
config = "/" + config;
235-
log("Trying classpath: " + config);
235+
debug("Trying classpath: " + config);
236236
return getClass().getResourceAsStream(config);
237237
}
238238

239239
private InputStream getFileResource(String config) throws Exception {
240240
File file = new File(config);
241-
log("Trying file: " + config);
241+
debug("Trying file: " + config);
242242
if (file.canRead()) {
243243
return new FileInputStream(file);
244244
}
@@ -294,7 +294,7 @@ private void initializePaths() throws Exception {
294294
if (path != null) {
295295
this.paths = parsePathsProperty(path);
296296
}
297-
log("Nested archive paths: " + this.paths);
297+
debug("Nested archive paths: " + this.paths);
298298
}
299299

300300
private List<String> parsePathsProperty(String commaSeparatedPaths) {
@@ -342,7 +342,7 @@ protected ClassLoader createClassLoader(List<Archive> archives) throws Exception
342342
String customLoaderClassName = getProperty("loader.classLoader");
343343
if (customLoaderClassName != null) {
344344
loader = wrapWithCustomClassLoader(loader, customLoaderClassName);
345-
log("Using custom class loader: " + customLoaderClassName);
345+
debug("Using custom class loader: " + customLoaderClassName);
346346
}
347347
return loader;
348348
}
@@ -392,13 +392,13 @@ private String getProperty(String propertyKey, String manifestKey,
392392
if (property != null) {
393393
String value = SystemPropertyUtils.resolvePlaceholders(this.properties,
394394
property);
395-
log("Property '" + propertyKey + "' from environment: " + value);
395+
debug("Property '" + propertyKey + "' from environment: " + value);
396396
return value;
397397
}
398398
if (this.properties.containsKey(propertyKey)) {
399399
String value = SystemPropertyUtils.resolvePlaceholders(this.properties,
400400
this.properties.getProperty(propertyKey));
401-
log("Property '" + propertyKey + "' from properties: " + value);
401+
debug("Property '" + propertyKey + "' from properties: " + value);
402402
return value;
403403
}
404404
try {
@@ -408,7 +408,7 @@ private String getProperty(String propertyKey, String manifestKey,
408408
if (manifest != null) {
409409
String value = manifest.getMainAttributes().getValue(manifestKey);
410410
if (value != null) {
411-
log("Property '" + manifestKey
411+
debug("Property '" + manifestKey
412412
+ "' from home directory manifest: " + value);
413413
return SystemPropertyUtils.resolvePlaceholders(this.properties,
414414
value);
@@ -424,7 +424,7 @@ private String getProperty(String propertyKey, String manifestKey,
424424
if (manifest != null) {
425425
String value = manifest.getMainAttributes().getValue(manifestKey);
426426
if (value != null) {
427-
log("Property '" + manifestKey + "' from archive manifest: " + value);
427+
debug("Property '" + manifestKey + "' from archive manifest: " + value);
428428
return SystemPropertyUtils.resolvePlaceholders(this.properties, value);
429429
}
430430
}
@@ -460,19 +460,19 @@ private List<Archive> getClassPathArchives(String path) throws Exception {
460460
file = new File(this.home, root);
461461
}
462462
if (file.isDirectory()) {
463-
log("Adding classpath entries from " + file);
463+
debug("Adding classpath entries from " + file);
464464
Archive archive = new ExplodedArchive(file, false);
465465
lib.add(archive);
466466
}
467467
Archive archive = getArchive(file);
468468
if (archive != null) {
469-
log("Adding classpath entries from archive " + archive.getUrl() + root);
469+
debug("Adding classpath entries from archive " + archive.getUrl() + root);
470470
lib.add(archive);
471471
}
472-
List<Archive> nested = getNestedArchive(root);
472+
Archive nested = getNestedArchive(root);
473473
if (nested != null) {
474-
log("Adding classpath entries from nested " + root);
475-
lib.addAll(nested);
474+
debug("Adding classpath entries from nested " + root);
475+
lib.add(nested);
476476
}
477477
return lib;
478478
}
@@ -490,21 +490,19 @@ private Archive getArchive(File file) throws IOException {
490490
return null;
491491
}
492492

493-
private List<Archive> getNestedArchive(String root) throws Exception {
494-
List<Archive> list = new ArrayList<Archive>();
493+
private Archive getNestedArchive(String root) throws Exception {
495494
if (root.startsWith("/")
496495
|| this.parent.getUrl().equals(this.home.toURI().toURL())) {
497496
// If home dir is same as parent archive, no need to add it twice.
498-
return list;
497+
return null;
499498
}
500499
EntryFilter filter = new PrefixMatchingArchiveFilter(root);
501500
if (this.parent.getNestedArchives(filter).isEmpty()) {
502-
return list;
501+
return null;
503502
}
504503
// If there are more archives nested in this subdirectory (root) then create a new
505504
// virtual archive for them, and have it added to the classpath
506-
list.add(new FilteredArchive(this.parent, filter));
507-
return list;
505+
return new FilteredArchive(this.parent, filter);
508506
}
509507

510508
private void addNestedEntries(List<Archive> lib) {
@@ -575,15 +573,17 @@ private static String capitalize(String str) {
575573
return Character.toUpperCase(str.charAt(0)) + str.substring(1);
576574
}
577575

578-
private void log(String message) {
576+
private void debug(String message) {
579577
if (Boolean.getBoolean(DEBUG)) {
580-
// We shouldn't use java.util.logging because of classpath issues so we
581-
// just sysout log messages when "loader.debug" is true
582-
System.out.println(message);
578+
log(message);
583579
}
584580
}
585581

586582
private void warn(String message) {
583+
log("WARNING: " + message);
584+
}
585+
586+
private void log(String message) {
587587
// We shouldn't use java.util.logging because of classpath issues
588588
System.out.println(message);
589589
}

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
loader.main: demo.NoSuchApplication
1+
loader.main: demo.Application
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Manifest-Version: 1.0
2+
Start-Class: ${foo.main}

0 commit comments

Comments
 (0)