Skip to content

Commit 650d830

Browse files
committed
context-loader rackup script resolution should work also when rackup.path configured
closes #191 as merged
1 parent f7569c7 commit 650d830

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

src/main/java/org/jruby/rack/DefaultRackApplicationFactory.java

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.lang.reflect.Method;
1616
import java.net.URISyntaxException;
1717
import java.net.URL;
18-
import java.util.Iterator;
1918
import java.util.Map;
2019
import java.util.Set;
2120

@@ -501,23 +500,34 @@ private String findConfigRuPathInSubDirectories(final String path, int level) {
501500

502501
if (level > 0) {
503502
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) == '/' ) {
507506
subpath = findConfigRuPathInSubDirectories(subpath, level);
508-
if (subpath != null) {
509-
return subpath;
510-
}
507+
if ( subpath != null ) return subpath;
511508
}
512509
}
513510
}
514511
}
515512
return null;
516513
}
517514

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+
}
520529

530+
private String resolveRackupScript() throws RackInitializationException {
521531
String rackup = rackContext.getConfig().getRackup();
522532
if (rackup == null) {
523533
rackup = rackContext.getConfig().getRackupPath();
@@ -533,23 +543,40 @@ private String resolveRackupScript() throws RackInitializationException {
533543
}
534544
}
535545

536-
InputStream is = null;
537-
try {
538-
if (rackup != null) {
546+
if (rackup != null) {
547+
InputStream is;
548+
try {
539549
is = rackContext.getResourceAsStream(rackup);
540550
rackupLocation = rackContext.getRealPath(rackup);
551+
return this.rackupScript = IOHelpers.inputStreamToString(is);
541552
}
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);
545563
}
546-
rackup = IOHelpers.inputStreamToString(is);
547564
}
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+
}
551575
}
552576
}
577+
else {
578+
rackupLocation = "<web.xml>";
579+
}
553580

554581
return this.rackupScript = rackup;
555582
}

0 commit comments

Comments
 (0)