Skip to content

Commit 86955ee

Browse files
committed
change context listener to throw by default
1 parent e98ee50 commit 86955ee

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,18 +253,11 @@ public Map<String, String> getRuntimeEnvironment() {
253253
if ( env == null ) env = getProperty("jruby.runtime.environment");
254254
final Object envFlag = toStrictBoolean(env, null);
255255
if ( envFlag != null ) {
256-
boolean keep = ((Boolean) envFlag).booleanValue();
257256
// jruby.runtime.env = true keep as is (return null)
258257
// jruby.runtime.env = false clear env (return empty)
259-
//return keep ? null : new HashMap<String, String>();
260-
if ( keep ) {
261-
return new HashMap<String, String>(System.getenv());
262-
}
263-
else {
264-
return new HashMap<String, String>();
265-
}
258+
return (Boolean) envFlag ? new HashMap<>(System.getenv()) : new HashMap<>();
266259
}
267-
if ( isIgnoreEnvironment() ) return new HashMap<String, String>();
260+
if ( isIgnoreEnvironment() ) return new HashMap<>();
268261
// TODO maybe support custom value 'servlet' to use init params ?
269262
return toStringMap(env);
270263
}
@@ -289,14 +282,14 @@ public boolean isThrowInitException() {
289282

290283
static boolean isThrowInitException(RackConfig config) {
291284
Boolean error = config.getBooleanProperty("jruby.rack.error");
292-
if ( error != null && ! error.booleanValue() ) {
293-
return true; // jruby.rack.error = false
285+
if ( error != null && error.booleanValue() ) {
286+
return false; // jruby.rack.error = true
294287
}
295-
error = config.getBooleanProperty("jruby.rack.exception");
296-
if ( error != null && ! error.booleanValue() ) {
297-
return true; // jruby.rack.exception = false
288+
error = config.getBooleanProperty(RackEnvironment.EXCEPTION);
289+
if ( error != null && error.booleanValue() ) {
290+
return false; // jruby.rack.exception = true
298291
}
299-
return false;
292+
return true;
300293
}
301294

302295
@Override
@@ -421,7 +414,7 @@ private Map<String, String> toStringMap(final String env) {
421414
}
422415

423416
private static Map<String,String> getLoggerTypes() {
424-
final Map<String,String> loggerTypes = new HashMap<String, String>(8);
417+
final Map<String,String> loggerTypes = new HashMap<>(8);
425418
loggerTypes.put("commons_logging", "org.jruby.rack.logging.CommonsLoggingLogger");
426419
loggerTypes.put("clogging", "org.jruby.rack.logging.CommonsLoggingLogger");
427420
loggerTypes.put("slf4j", "org.jruby.rack.logging.Slf4jLogger");

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void contextInitialized(final ServletContextEvent event) {
5050
try {
5151
factory.init(rackContext);
5252
}
53-
catch (RuntimeException e) {
53+
catch (Exception e) {
5454
handleInitializationException(e, factory, rackContext);
5555
}
5656
}
@@ -71,7 +71,7 @@ protected RackApplicationFactory newApplicationFactory(RackConfig config) {
7171

7272
final RackApplicationFactory factory = new DefaultRackApplicationFactory();
7373
final Integer maxRuntimes = config.getMaximumRuntimes();
74-
// for backwards compatibility when runtime mix/max values not specified
74+
// for backwards compatibility when runtime min/max values not specified
7575
// we assume a single shared (threadsafe) runtime to be used :
7676
if ( maxRuntimes == null || maxRuntimes.intValue() == 1 ) {
7777
return new SharedRackApplicationFactory(factory);
@@ -87,7 +87,6 @@ protected void handleInitializationException(
8787
final Exception e,
8888
final RackApplicationFactory factory,
8989
final ServletRackContext rackContext) {
90-
// TODO for backwards compat we do not throw (by default) but should :
9190
if ( isThrowInitException(rackContext.getConfig()) ) {
9291
if (e instanceof RuntimeException) {
9392
throw (RuntimeException) e;

src/spec/ruby/rack/servlet_context_listener_spec.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,19 @@
3333
@listener.contextInitialized servlet_context_event
3434
end
3535

36-
it "logs an error if initialization failed" do
37-
@factory.should_receive(:init).and_raise org.jruby.rack.RackInitializationException, "help"
38-
@servlet_context.should_receive(:log) do |level, message, error|
39-
level == 'ERROR' && message =~ /initialization failed/ && error.message == 'help'
40-
end
41-
@listener.contextInitialized servlet_context_event
36+
it "throws an error if initialization failed" do
37+
@servlet_context = org.springframework.mock.web.MockServletContext.new
38+
@factory.should_receive(:init).and_raise org.jruby.rack.RackInitializationException.new("help")
39+
40+
expect { @listener.contextInitialized(servlet_context_event) }.to raise_error(org.jruby.rack.RackInitializationException)
4241
end
4342

44-
it "throws an error if initialization failed (and jruby.rack.error = false)" do
43+
it "does not throw if initialization failed (and jruby.rack.error = true)" do
4544
@servlet_context = org.springframework.mock.web.MockServletContext.new
46-
@servlet_context.add_init_parameter 'jruby.rack.error', 'false'
45+
@servlet_context.addInitParameter 'jruby.rack.error', 'true'
4746
@factory.should_receive(:init).and_raise org.jruby.rack.RackInitializationException.new("help")
4847

49-
expect { @listener.contextInitialized servlet_context_event }.to raise_error(org.jruby.rack.RackInitializationException)
48+
expect { @listener.contextInitialized(servlet_context_event) }.to_not raise_error
5049
end
5150

5251
end

0 commit comments

Comments
 (0)