Skip to content

Commit e98ee50

Browse files
committed
[refactor] avoid reflection + internal cleanup
1 parent 7b0d3b1 commit e98ee50

File tree

1 file changed

+21
-59
lines changed

1 file changed

+21
-59
lines changed

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

Lines changed: 21 additions & 59 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

0 commit comments

Comments
 (0)