Skip to content

Commit 115670b

Browse files
authored
Merge pull request #288 from kares/more-cleanup-1.3
small behavioral changes and cleanup
2 parents 1d9767b + f3f06da commit 115670b

35 files changed

+343
-348
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import java.io.IOException;
1111

12+
import static org.jruby.rack.RackLogger.Level.*;
13+
1214
/**
1315
*
1416
* @author nicksieger
@@ -46,10 +48,10 @@ protected void handleException(
4648
final RackResponseEnvironment response) throws IOException {
4749

4850
if ( response.isCommitted() ) {
49-
context.log(RackLogger.ERROR, "couldn't handle exception (response is committed)", e);
51+
context.log(ERROR, "couldn't handle exception (response is committed)", e);
5052
return;
5153
}
52-
context.log(RackLogger.INFO, "resetting rack response due exception: " + e);
54+
context.log(INFO, "resetting rack response due exception: " + e);
5355
response.reset();
5456

5557
afterException(request, e, response);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
import org.jruby.Ruby;
3535

36+
import static org.jruby.rack.RackLogger.Level.*;
37+
3638
/**
3739
* Default error application if the Rack error application can not be setup or
3840
* "jruby.rack.error" handling is turned off (set to false).
@@ -117,7 +119,7 @@ public String getBody() {
117119
body = buildErrorBody();
118120
}
119121
catch (Exception e) {
120-
log(RackLogger.INFO, "failed building error body", e);
122+
log(INFO, "failed building error body", e);
121123
body = getError() == null ? "" : getError().toString();
122124
}
123125
}
@@ -149,11 +151,11 @@ public void respond(RackResponseEnvironment response) {
149151
defaultRespond(this, response);
150152
}
151153
catch (IOException e) {
152-
log(RackLogger.WARN, "could not write response body", e);
154+
log(WARN, "could not write response body", e);
153155
}
154156
}
155157

156-
private void log(String level, String message, Throwable e) {
158+
private void log(RackLogger.Level level, String message, Throwable e) {
157159
if ( context != null ) context.log(level, message, e);
158160
}
159161

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

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import java.io.File;
1212
import java.io.IOException;
1313
import java.io.InputStream;
14-
import java.lang.reflect.InvocationTargetException;
15-
import java.lang.reflect.Method;
1614
import java.net.URISyntaxException;
1715
import java.net.URL;
1816
import java.util.Map;
@@ -99,11 +97,7 @@ public void init(final RackContext rackContext) {
9997
*/
10098
@Override
10199
public RackApplication newApplication() {
102-
return createApplication(new ApplicationObjectFactory() {
103-
public IRubyObject create(Ruby runtime) {
104-
return createApplicationObject(runtime);
105-
}
106-
});
100+
return createApplication(this::createApplicationObject);
107101
}
108102

109103
/**
@@ -209,13 +203,7 @@ public RackApplication newErrorApplication() {
209203
return new DefaultErrorApplication(rackContext);
210204
}
211205
try {
212-
RackApplication app = createErrorApplication(
213-
new ApplicationObjectFactory() {
214-
public IRubyObject create(Ruby runtime) {
215-
return createErrorApplicationObject(runtime);
216-
}
217-
}
218-
);
206+
RackApplication app = createErrorApplication(this::createErrorApplicationObject);
219207
app.init();
220208
return app;
221209
}
@@ -250,7 +238,7 @@ protected IRubyObject createRackServletWrapper(Ruby runtime, String rackup, Stri
250238
);
251239
}
252240

253-
static interface ApplicationObjectFactory {
241+
interface ApplicationObjectFactory {
254242
IRubyObject create(Ruby runtime) ;
255243
}
256244

@@ -267,21 +255,7 @@ protected RubyInstanceConfig initRuntimeConfig(final RubyInstanceConfig config)
267255
// Don't affect the container and sibling web apps when ENV changes are
268256
// made inside the Ruby app ...
269257
// There are quite a such things made in a typical Bundler based app.
270-
try { // config.setUpdateNativeENVEnabled(false) using reflection :
271-
final Method setUpdateNativeENVEnabled =
272-
config.getClass().getMethod("setUpdateNativeENVEnabled", Boolean.TYPE);
273-
setUpdateNativeENVEnabled.invoke(config, false);
274-
}
275-
catch (NoSuchMethodException e) { // ignore method has been added in JRuby 1.6.7
276-
rackContext.log(DEBUG, "envronment changes made inside one app " +
277-
"might affect another, consider updating JRuby if this is an issue");
278-
}
279-
catch (IllegalAccessException e) {
280-
rackContext.log(WARN, "failed to disable updating native environment", e);
281-
}
282-
catch (InvocationTargetException e) {
283-
throw new RackException(e.getTargetException());
284-
}
258+
config.setUpdateNativeENVEnabled(false);
285259

286260
final Map<String, String> newEnv = rackConfig.getRuntimeEnvironment();
287261
if ( newEnv != null ) {
@@ -297,7 +271,6 @@ protected RubyInstanceConfig initRuntimeConfig(final RubyInstanceConfig config)
297271
else {
298272
// allow to work (backwards) "compatibly" with previous `ENV.clear`
299273
// RUBYOPT was processed since it happens on config.processArguments
300-
@SuppressWarnings("unchecked")
301274
final Map<String, String> env = config.getEnvironment();
302275
if ( env != null && env.containsKey("RUBYOPT") ) {
303276
newEnv.put( "RUBYOPT", env.get("RUBYOPT") );
@@ -355,9 +328,7 @@ protected void loadJRubyRack(final Ruby runtime) {
355328
public void initRuntime(final Ruby runtime) {
356329
loadJRubyRack(runtime);
357330
// set $servlet_context :
358-
runtime.getGlobalVariables().set(
359-
"$servlet_context", JavaUtil.convertJavaToRuby(runtime, rackContext)
360-
);
331+
runtime.getGlobalVariables().set("$servlet_context", JavaUtil.convertJavaToRuby(runtime, rackContext));
361332
// load our (servlet) Rack handler :
362333
runtime.evalScriptlet("require 'rack/handler/servlet'");
363334

@@ -411,9 +382,6 @@ public String checkAndSetRackVersion(final Ruby runtime) {
411382
rackContext.log(DEBUG, "could not read 'rack.version' magic comment from rackup", e);
412383
}
413384

414-
if ( rackVersion == null ) {
415-
// NOTE: try matching a `require 'bundler/setup'` line ... maybe not ?!
416-
}
417385
if ( rackVersion != null ) {
418386
runtime.evalScriptlet("require 'rubygems'");
419387

@@ -462,10 +430,22 @@ public void destroy() {
462430
runtime.tearDown(false);
463431
}
464432

433+
private void captureMessage(final RaiseException re) {
434+
try {
435+
IRubyObject rubyException = re.getException();
436+
ThreadContext context = rubyException.getRuntime().getCurrentContext();
437+
// JRuby-Rack internals (@see jruby/rack/capture.rb) :
438+
rubyException.callMethod(context, "capture");
439+
rubyException.callMethod(context, "store");
440+
}
441+
catch (Exception e) {
442+
rackContext.log(INFO, "failed to capture exception message", e);
443+
// won't be able to capture anything
444+
}
445+
}
465446
}
466447

467448
private RackApplication createErrorApplication(final ApplicationObjectFactory appFactory) {
468-
// final Ruby runtime = newRuntime();
469449
return new ErrorApplicationImpl(appFactory);
470450
}
471451

@@ -482,29 +462,13 @@ public void init() {
482462

483463
}
484464

485-
private void captureMessage(final RaiseException re) {
486-
try {
487-
IRubyObject rubyException = re.getException();
488-
ThreadContext context = rubyException.getRuntime().getCurrentContext();
489-
// JRuby-Rack internals (@see jruby/rack/capture.rb) :
490-
rubyException.callMethod(context, "capture");
491-
rubyException.callMethod(context, "store");
492-
}
493-
catch (Exception e) {
494-
rackContext.log(INFO, "failed to capture exception message", e);
495-
// won't be able to capture anything
496-
}
497-
}
498-
499465
private String findConfigRuPathInSubDirectories(final String path, int level) {
500-
@SuppressWarnings("unchecked")
501466
final Set<String> entries = rackContext.getResourcePaths(path);
502467
if (entries != null) {
503468
String config_ru = path + "config.ru";
504469
if ( entries.contains(config_ru) ) {
505470
return config_ru;
506471
}
507-
508472
if (level > 0) {
509473
level--;
510474
for ( String subpath : entries ) {
@@ -526,11 +490,9 @@ private static String getContextLoaderScript(final String name, final boolean si
526490
InputStream is = contextLoader.getResourceAsStream(name);
527491
return IOHelpers.inputStreamToString(is);
528492
}
529-
catch (IOException e) {
530-
if ( silent ) return null; throw e;
531-
}
532-
catch (RuntimeException e) {
533-
if ( silent ) return null; throw e;
493+
catch (IOException|RuntimeException e) {
494+
if ( silent ) return null;
495+
throw e;
534496
}
535497
}
536498

@@ -565,7 +527,7 @@ private String resolveRackupScript() throws RackInitializationException {
565527
}
566528
catch (IOException ex) { /* won't happen */ }
567529

568-
rackContext.log(RackLogger.ERROR, "failed to read rackup from '"+ rackup + "' (" + e + ")");
530+
rackContext.log(ERROR, "failed to read rackup from '"+ rackup + "' (" + e + ")");
569531
throw new RackInitializationException("failed to read rackup input", e);
570532
}
571533
}

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/DefaultRackDispatcher.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import org.jruby.rack.servlet.ServletRackContext;
1313

14+
import static org.jruby.rack.RackLogger.Level.*;
15+
1416
/**
1517
* Dispatcher suited for use in a servlet container
1618
* @author nick
@@ -50,10 +52,10 @@ protected void afterException(
5052
}
5153
catch (final RuntimeException re) {
5254
// allow the error app to re-throw Ruby/JRuby-Rack exceptions :
53-
if (re instanceof RackException) throw (RackException) re;
55+
if (re instanceof RackException) throw re;
5456
//if (e instanceof RaiseException) throw (RaiseException) e;
5557
// TODO seems redundant maybe we should let the container decide ?!
56-
context.log(RackLogger.ERROR, "error app failed to handle exception: " + e, re);
58+
context.log(ERROR, "error app failed to handle exception: " + e, re);
5759
Integer errorCode = getErrorApplicationFailureStatusCode();
5860
if ( errorCode != null && errorCode.intValue() > 0 ) {
5961
response.sendError(errorCode);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*
1414
* @author kares
1515
*/
16-
@SuppressWarnings("serial")
1716
public class RackException extends RuntimeException {
1817

1918
public RackException(String message) {
@@ -57,9 +56,7 @@ static String exceptionMessage(final RaiseException e) {
5756
st.append(b.toString());
5857
return st.toString();
5958
}
60-
else {
61-
return null;
62-
}
59+
return null;
6360
}
6461

6562
}

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

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
* @author nicksieger
1313
*/
1414
public interface RackLogger {
15-
16-
void log(String message) ;
17-
void log(String message, Throwable ex) ;
18-
1915
//void debug(String message) ;
2016
//void debug(String message, Throwable e) ;
2117

@@ -37,44 +33,27 @@ enum Level {
3733
void log(Level level, String message) ;
3834
void log(Level level, String message, Throwable ex) ;
3935

40-
@Deprecated final String DEBUG = Level.DEBUG.name();
41-
@Deprecated final String INFO = Level.INFO.name();
42-
@Deprecated final String WARN = Level.WARN.name();
43-
@Deprecated final String ERROR = Level.ERROR.name();
36+
default void log(String message) {
37+
log(Level.INFO, message);
38+
}
4439

45-
void log(String level, String message) ;
46-
void log(String level, String message, Throwable ex) ;
40+
default void log(String message, Throwable ex) {
41+
log(Level.ERROR, message, ex);
42+
}
4743

48-
abstract class Base implements RackLogger {
44+
default void log(String level, String message) {
45+
log(Level.valueOf(level), message);
46+
}
4947

50-
public abstract Level getLevel() ;
48+
default void log(String level, String message, Throwable ex) {
49+
log(Level.valueOf(level), message, ex);
50+
}
5151

52+
abstract class Base implements RackLogger {
53+
public abstract Level getLevel() ;
5254
public void setLevel(Level level) { /* noop */ }
5355

5456
public boolean isFormatting() { return false; }
55-
5657
public void setFormatting(boolean flag) { /* noop */ }
57-
58-
@Override
59-
public void log(String message) {
60-
log(Level.INFO, message);
61-
}
62-
63-
@Override
64-
public void log(String message, Throwable ex) {
65-
log(Level.ERROR, message, ex);
66-
}
67-
68-
@Override
69-
public void log(String level, String message) {
70-
log(Level.valueOf(level), message);
71-
}
72-
73-
@Override
74-
public void log(String level, String message, Throwable ex) {
75-
log(Level.valueOf(level), message, ex);
76-
}
77-
7858
}
79-
8059
}

0 commit comments

Comments
 (0)