15
15
import java .lang .reflect .Method ;
16
16
import java .net .URISyntaxException ;
17
17
import java .net .URL ;
18
- import java .util .Iterator ;
19
18
import java .util .Map ;
20
19
import java .util .Set ;
21
20
@@ -501,23 +500,34 @@ private String findConfigRuPathInSubDirectories(final String path, int level) {
501
500
502
501
if (level > 0 ) {
503
502
level --;
504
- for ( Iterator < String > i = entries . iterator (); i . hasNext (); ) {
505
- String subpath = i . next ();
506
- if (subpath .endsWith ( "/" ) ) {
503
+ for ( String subpath : entries ) {
504
+ final int len = subpath . length ();
505
+ if ( len > 0 && subpath .charAt ( len - 1 ) == '/' ) {
507
506
subpath = findConfigRuPathInSubDirectories (subpath , level );
508
- if (subpath != null ) {
509
- return subpath ;
510
- }
507
+ if ( subpath != null ) return subpath ;
511
508
}
512
509
}
513
510
}
514
511
}
515
512
return null ;
516
513
}
517
514
518
- private String resolveRackupScript () throws RackInitializationException {
519
- rackupLocation = "<web.xml>" ;
515
+ private static String getContextLoaderScript (final String name , final boolean silent )
516
+ throws IOException {
517
+ try { // still try context-loader for resolving rackup :
518
+ final ClassLoader contextLoader = Thread .currentThread ().getContextClassLoader ();
519
+ InputStream is = contextLoader .getResourceAsStream (name );
520
+ return IOHelpers .inputStreamToString (is );
521
+ }
522
+ catch (IOException e ) {
523
+ if ( silent ) return null ; throw e ;
524
+ }
525
+ catch (RuntimeException e ) {
526
+ if ( silent ) return null ; throw e ;
527
+ }
528
+ }
520
529
530
+ private String resolveRackupScript () throws RackInitializationException {
521
531
String rackup = rackContext .getConfig ().getRackup ();
522
532
if (rackup == null ) {
523
533
rackup = rackContext .getConfig ().getRackupPath ();
@@ -533,23 +543,40 @@ private String resolveRackupScript() throws RackInitializationException {
533
543
}
534
544
}
535
545
536
- InputStream is = null ;
537
- try {
538
- if ( rackup != null ) {
546
+ if ( rackup ! = null ) {
547
+ InputStream is ;
548
+ try {
539
549
is = rackContext .getResourceAsStream (rackup );
540
550
rackupLocation = rackContext .getRealPath (rackup );
551
+ return this .rackupScript = IOHelpers .inputStreamToString (is );
541
552
}
542
- else {
543
- is = Thread .currentThread ().getContextClassLoader ().getResourceAsStream ("config.ru" );
544
- rackupLocation = "uri:classloader://config.ru" ;
553
+ catch (IOException e ) {
554
+ try { // last - try context-loader for resolving rackup :
555
+ if ( (rackup = getContextLoaderScript (rackup , true )) != null ) {
556
+ return this .rackupScript = rackup ;
557
+ }
558
+ }
559
+ catch (IOException ex ) { /* won't happen */ }
560
+
561
+ rackContext .log (RackLogger .ERROR , "failed to read rackup from '" + rackup + "' (" + e + ")" );
562
+ throw new RackInitializationException ("failed to read rackup input" , e );
545
563
}
546
- rackup = IOHelpers .inputStreamToString (is );
547
564
}
548
- catch (IOException e ) {
549
- rackContext .log (RackLogger .ERROR , "failed to read rackup from '" + rackup + "' (" + e + ")" );
550
- throw new RackInitializationException ("failed to read rackup input" , e );
565
+ else {
566
+ rackup = "config.ru" ;
567
+ try {
568
+ rackup = getContextLoaderScript (rackup , false );
569
+ rackupLocation = "uri:classloader://config.ru" ;
570
+ }
571
+ catch (IOException e ) {
572
+ rackContext .log (RackLogger .ERROR , "failed to read rackup from '" + rackup + "' (" + e + ")" );
573
+ throw new RackInitializationException ("failed to read rackup input" , e );
574
+ }
551
575
}
552
576
}
577
+ else {
578
+ rackupLocation = "<web.xml>" ;
579
+ }
553
580
554
581
return this .rackupScript = rackup ;
555
582
}
0 commit comments