@@ -142,48 +142,64 @@ public PropertiesLauncher() {
142
142
}
143
143
144
144
protected File getHomeDirectory () {
145
- return new File (SystemPropertyUtils
146
- .resolvePlaceholders (System .getProperty (HOME , "${user.dir}" )));
145
+ try {
146
+ return new File (getPropertyWithDefault (HOME , "${user.dir}" ));
147
+ }
148
+ catch (Exception ex ) {
149
+ throw new IllegalStateException (ex );
150
+ }
147
151
}
148
152
149
153
private void initializeProperties () throws Exception , IOException {
150
- String config = "classpath:BOOT-INF/classes/"
151
- + SystemPropertyUtils .resolvePlaceholders (
152
- SystemPropertyUtils .getProperty (CONFIG_NAME , "application" ))
153
- + ".properties" ;
154
- config = SystemPropertyUtils .resolvePlaceholders (
155
- SystemPropertyUtils .getProperty (CONFIG_LOCATION , config ));
156
- InputStream resource = getResource (config );
157
- if (resource != null ) {
158
- log ("Found: " + config );
159
- try {
160
- this .properties .load (resource );
161
- }
162
- finally {
163
- resource .close ();
154
+ List <String > configs = new ArrayList <String >();
155
+ if (getProperty (CONFIG_LOCATION ) != null ) {
156
+ configs .add (getProperty (CONFIG_LOCATION ));
157
+ }
158
+ else {
159
+ String [] names = getPropertyWithDefault (CONFIG_NAME , "loader,application" )
160
+ .split ("," );
161
+ for (String name : names ) {
162
+ configs .add ("file:" + getHomeDirectory () + "/" + name + ".properties" );
163
+ configs .add ("classpath:" + name + ".properties" );
164
+ configs .add ("classpath:BOOT-INF/classes/" + name + ".properties" );
164
165
}
165
- for (Object key : Collections .list (this .properties .propertyNames ())) {
166
- String text = this .properties .getProperty ((String ) key );
167
- String value = SystemPropertyUtils .resolvePlaceholders (this .properties ,
168
- text );
169
- if (value != null ) {
170
- this .properties .put (key , value );
166
+ }
167
+ for (String config : configs ) {
168
+ InputStream resource = getResource (config );
169
+ if (resource != null ) {
170
+ log ("Found: " + config );
171
+ try {
172
+ this .properties .load (resource );
173
+ }
174
+ finally {
175
+ resource .close ();
171
176
}
172
- }
173
- if (SystemPropertyUtils
174
- .resolvePlaceholders ("${" + SET_SYSTEM_PROPERTIES + ":false}" )
175
- .equals ("true" )) {
176
- log ("Adding resolved properties to System properties" );
177
177
for (Object key : Collections .list (this .properties .propertyNames ())) {
178
- String value = this .properties .getProperty ((String ) key );
179
- System .setProperty ((String ) key , value );
178
+ if (config .endsWith ("application.properties" )
179
+ && ((String ) key ).startsWith ("loader." )) {
180
+ warn ("WARNING: use of application.properties for PropertiesLauncher is deprecated" );
181
+ }
182
+ String text = this .properties .getProperty ((String ) key );
183
+ String value = SystemPropertyUtils
184
+ .resolvePlaceholders (this .properties , text );
185
+ if (value != null ) {
186
+ this .properties .put (key , value );
187
+ }
188
+ }
189
+ if ("true" .equals (getProperty (SET_SYSTEM_PROPERTIES ))) {
190
+ log ("Adding resolved properties to System properties" );
191
+ for (Object key : Collections .list (this .properties .propertyNames ())) {
192
+ String value = this .properties .getProperty ((String ) key );
193
+ System .setProperty ((String ) key , value );
194
+ }
180
195
}
196
+ // Load the first one we find
197
+ return ;
198
+ }
199
+ else {
200
+ log ("Not found: " + config );
181
201
}
182
202
}
183
- else {
184
- log ("Not found: " + config );
185
- }
186
-
187
203
}
188
204
189
205
private InputStream getResource (String config ) throws Exception {
@@ -354,34 +370,50 @@ private ClassLoader wrapWithCustomClassLoader(ClassLoader parent,
354
370
}
355
371
356
372
private String getProperty (String propertyKey ) throws Exception {
357
- return getProperty (propertyKey , null );
373
+ return getProperty (propertyKey , null , null );
358
374
}
359
375
360
376
private String getProperty (String propertyKey , String manifestKey ) throws Exception {
377
+ return getProperty (propertyKey , manifestKey , null );
378
+ }
379
+
380
+ private String getPropertyWithDefault (String propertyKey , String defaultValue )
381
+ throws Exception {
382
+ return getProperty (propertyKey , null , defaultValue );
383
+ }
384
+
385
+ private String getProperty (String propertyKey , String manifestKey ,
386
+ String defaultValue ) throws Exception {
361
387
if (manifestKey == null ) {
362
388
manifestKey = propertyKey .replace ('.' , '-' );
363
389
manifestKey = toCamelCase (manifestKey );
364
390
}
365
391
String property = SystemPropertyUtils .getProperty (propertyKey );
366
392
if (property != null ) {
367
- String value = SystemPropertyUtils .resolvePlaceholders (property );
393
+ String value = SystemPropertyUtils .resolvePlaceholders (this .properties ,
394
+ property );
368
395
log ("Property '" + propertyKey + "' from environment: " + value );
369
396
return value ;
370
397
}
371
398
if (this .properties .containsKey (propertyKey )) {
372
- String value = SystemPropertyUtils
373
- . resolvePlaceholders ( this .properties .getProperty (propertyKey ));
399
+ String value = SystemPropertyUtils . resolvePlaceholders ( this . properties ,
400
+ this .properties .getProperty (propertyKey ));
374
401
log ("Property '" + propertyKey + "' from properties: " + value );
375
402
return value ;
376
403
}
377
404
try {
378
- // Prefer home dir for MANIFEST if there is one
379
- Manifest manifest = new ExplodedArchive (this .home , false ).getManifest ();
380
- if (manifest != null ) {
381
- String value = manifest .getMainAttributes ().getValue (manifestKey );
382
- log ("Property '" + manifestKey + "' from home directory manifest: "
383
- + value );
384
- return value ;
405
+ if (this .home != null ) {
406
+ // Prefer home dir for MANIFEST if there is one
407
+ Manifest manifest = new ExplodedArchive (this .home , false ).getManifest ();
408
+ if (manifest != null ) {
409
+ String value = manifest .getMainAttributes ().getValue (manifestKey );
410
+ if (value != null ) {
411
+ log ("Property '" + manifestKey
412
+ + "' from home directory manifest: " + value );
413
+ return SystemPropertyUtils .resolvePlaceholders (this .properties ,
414
+ value );
415
+ }
416
+ }
385
417
}
386
418
}
387
419
catch (IllegalStateException ex ) {
@@ -393,10 +425,11 @@ private String getProperty(String propertyKey, String manifestKey) throws Except
393
425
String value = manifest .getMainAttributes ().getValue (manifestKey );
394
426
if (value != null ) {
395
427
log ("Property '" + manifestKey + "' from archive manifest: " + value );
396
- return value ;
428
+ return SystemPropertyUtils . resolvePlaceholders ( this . properties , value ) ;
397
429
}
398
430
}
399
- return null ;
431
+ return defaultValue == null ? defaultValue
432
+ : SystemPropertyUtils .resolvePlaceholders (this .properties , defaultValue );
400
433
}
401
434
402
435
@ Override
@@ -436,10 +469,10 @@ private List<Archive> getClassPathArchives(String path) throws Exception {
436
469
log ("Adding classpath entries from archive " + archive .getUrl () + root );
437
470
lib .add (archive );
438
471
}
439
- Archive nested = getNestedArchive (root );
472
+ List < Archive > nested = getNestedArchive (root );
440
473
if (nested != null ) {
441
- log ("Adding classpath entries from nested " + nested . getUrl () + root );
442
- lib .add (nested );
474
+ log ("Adding classpath entries from nested " + root );
475
+ lib .addAll (nested );
443
476
}
444
477
return lib ;
445
478
}
@@ -457,19 +490,21 @@ private Archive getArchive(File file) throws IOException {
457
490
return null ;
458
491
}
459
492
460
- private Archive getNestedArchive (String root ) throws Exception {
493
+ private List <Archive > getNestedArchive (String root ) throws Exception {
494
+ List <Archive > list = new ArrayList <Archive >();
461
495
if (root .startsWith ("/" )
462
496
|| this .parent .getUrl ().equals (this .home .toURI ().toURL ())) {
463
497
// If home dir is same as parent archive, no need to add it twice.
464
- return null ;
498
+ return list ;
465
499
}
466
500
EntryFilter filter = new PrefixMatchingArchiveFilter (root );
467
501
if (this .parent .getNestedArchives (filter ).isEmpty ()) {
468
- return null ;
502
+ return list ;
469
503
}
470
504
// If there are more archives nested in this subdirectory (root) then create a new
471
505
// virtual archive for them, and have it added to the classpath
472
- return new FilteredArchive (this .parent , filter );
506
+ list .add (new FilteredArchive (this .parent , filter ));
507
+ return list ;
473
508
}
474
509
475
510
private void addNestedEntries (List <Archive > lib ) {
@@ -548,6 +583,11 @@ private void log(String message) {
548
583
}
549
584
}
550
585
586
+ private void warn (String message ) {
587
+ // We shouldn't use java.util.logging because of classpath issues
588
+ System .out .println (message );
589
+ }
590
+
551
591
/**
552
592
* Convenience class for finding nested archives that have a prefix in their file path
553
593
* (e.g. "lib/").
0 commit comments