Last active
May 29, 2020 23:28
-
-
Save dmikurube/fa8788aece000f0546aaf57ea6c21aae to your computer and use it in GitHub Desktop.
JRuby and environment variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile 'org.jruby:jruby-complete:9.1.5.0' | |
testCompile 'junit:junit:4.+' | |
} | |
sourceSets { | |
main { | |
java { | |
srcDir '.' | |
exclude 'Test*.java' | |
} | |
} | |
test { | |
java { | |
srcDir '.' | |
include 'Test*.java' | |
} | |
} | |
} | |
test { | |
testLogging { | |
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError' | |
} | |
} | |
task runWithMain(type:JavaExec) { | |
main = project.hasProperty('main') ? project.getProperty('main') : 'JRubyEnv' | |
classpath = sourceSets.main.runtimeClasspath | |
args 'main' | |
} | |
task runSingleton(type:JavaExec) { | |
main = project.hasProperty('main') ? project.getProperty('main') : 'JRubyEnv' | |
classpath = sourceSets.main.runtimeClasspath | |
args 'singletonNonNativeEnv' | |
} | |
task runSingletonNativeEnv(type:JavaExec) { | |
main = project.hasProperty('main') ? project.getProperty('main') : 'JRubyEnv' | |
classpath = sourceSets.main.runtimeClasspath | |
args 'singletonNativeEnv' | |
} | |
task runDefault(type:JavaExec) { | |
main = project.hasProperty('main') ? project.getProperty('main') : 'JRubyEnv' | |
classpath = sourceSets.main.runtimeClasspath | |
args 'default' | |
} | |
task runThreadSafe(type:JavaExec) { | |
main = project.hasProperty('main') ? project.getProperty('main') : 'JRubyEnv' | |
classpath = sourceSets.main.runtimeClasspath | |
args 'threadsafe' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts 'Ruby : <%s>' % ENV['TESTENV_MAIN'] | |
puts "" | |
puts ">> Ruby: ENV['TESTENV_MAIN'] = 'qux'" | |
ENV['TESTENV_MAIN'] = 'qux' | |
puts "" | |
puts 'Ruby : <%s>' % ENV['TESTENV_MAIN'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.IOException; | |
import org.jruby.RubyInstanceConfig; | |
import org.jruby.embed.LocalContextScope; | |
import org.jruby.embed.LocalVariableBehavior; | |
import org.jruby.embed.ScriptingContainer; | |
public class JRubyEnv { | |
public static void main(String[] args) { | |
if (args.length == 0) { | |
System.out.println("Too few arguments."); | |
return; | |
} | |
switch (args[0]) { | |
case "main": | |
runWithMain(); | |
break; | |
case "singletonNativeEnv": | |
runSingletonWithScriptingContainer(true); | |
break; | |
case "singletonNonNativeEnv": | |
runSingletonWithScriptingContainer(false); | |
break; | |
case "default": | |
runDefaultWithScriptingContainer(); | |
break; | |
case "threadsafe": | |
runThreadSafeWithScriptingContainer(); | |
break; | |
default: | |
System.out.println("Unknown."); | |
} | |
} | |
private static void runWithMain() { | |
String[] jrubyArgs = new String[1]; | |
System.out.println("Hello, org.jruby.Main.main."); | |
jrubyArgs[0] = "env.rb"; | |
System.out.println("Java : <" + System.getenv("TESTENV_MAIN") + ">"); | |
org.jruby.Main.main(jrubyArgs); | |
System.out.println("Java : <" + System.getenv("TESTENV_MAIN") + ">"); | |
} | |
private static void runSingletonWithScriptingContainer(boolean nativeENV) { | |
System.out.println("Hello, singleton ScriptingContainer."); | |
runWithScriptingContainer(LocalContextScope.SINGLETON, LocalVariableBehavior.PERSISTENT, nativeENV, nativeENV); | |
} | |
private static void runDefaultWithScriptingContainer() { | |
System.out.println("Hello, default ScriptingContainer."); | |
runWithScriptingContainer(null, null, true, false); | |
} | |
private static void runThreadSafeWithScriptingContainer() { | |
System.out.println("Hello, LocalContextScope.THREADSAFE."); | |
runWithScriptingContainer(LocalContextScope.THREADSAFE, null, true, false); | |
} | |
private static void runWithScriptingContainer( | |
LocalContextScope scope, | |
LocalVariableBehavior behavior, | |
boolean container1NativeENV, | |
boolean container2NativeENV) { | |
final ScriptingContainer container1 = createScriptingContainer(scope, behavior); | |
final RubyInstanceConfig config1 = container1.getProvider().getRubyInstanceConfig(); | |
config1.setUpdateNativeENVEnabled(container1NativeENV); | |
final ScriptingContainer container2 = createScriptingContainer(scope, behavior); | |
final RubyInstanceConfig config2 = container2.getProvider().getRubyInstanceConfig(); | |
config2.setUpdateNativeENVEnabled(container2NativeENV); | |
System.out.println("Java : <" + System.getenv("TESTENV") + ">"); | |
printEnvByCommand(); | |
container1.runScriptlet("puts 'Ruby1 : <%s>' % ENV['TESTENV']"); | |
container2.runScriptlet("puts 'Ruby2 : <%s>' % ENV['TESTENV']"); | |
System.out.println(""); | |
System.out.println(">> Ruby1: ENV['TESTENV'] = 'foo'"); | |
container1.runScriptlet("ENV['TESTENV'] = 'foo'"); | |
System.out.println(""); | |
System.out.println("Java : <" + System.getenv("TESTENV") + ">"); | |
printEnvByCommand(); | |
container1.runScriptlet("puts 'Ruby1 : <%s>' % ENV['TESTENV']"); | |
container2.runScriptlet("puts 'Ruby2 : <%s>' % ENV['TESTENV']"); | |
System.out.println(""); | |
System.out.println(">> Ruby2: ENV['TESTENV'] = 'bar'"); | |
container2.runScriptlet("ENV['TESTENV'] = 'bar'"); | |
System.out.println(""); | |
System.out.println("Java : <" + System.getenv("TESTENV") + ">"); | |
printEnvByCommand(); | |
container1.runScriptlet("puts 'Ruby1 : <%s>' % ENV['TESTENV']"); | |
container2.runScriptlet("puts 'Ruby2 : <%s>' % ENV['TESTENV']"); | |
} | |
private static ScriptingContainer createScriptingContainer( | |
LocalContextScope scope, | |
LocalVariableBehavior behavior) { | |
if (scope != null && behavior != null) { | |
return new ScriptingContainer(scope, behavior); | |
} else if (scope == null && behavior != null) { | |
return new ScriptingContainer(behavior); | |
} else if (scope != null && behavior == null) { | |
return new ScriptingContainer(scope); | |
} | |
return new ScriptingContainer(); | |
} | |
private static int printEnvByCommand() | |
{ | |
final ProcessBuilder processBuilder = new ProcessBuilder("printenv", "TESTENV"); | |
final Process process; | |
try { | |
process = processBuilder.start(); | |
} catch (IOException ex) { | |
return -2; | |
} | |
final InputStream input = process.getInputStream(); | |
final BufferedReader reader = new BufferedReader(new InputStreamReader(input)); | |
try { | |
final String line; | |
try { | |
line = reader.readLine(); | |
} catch (IOException ex) { | |
return -5; | |
} | |
if (line == null) { | |
System.out.println("printenv : <null>"); | |
} else { | |
System.out.println("printenv : <" + line + ">"); | |
} | |
} finally { | |
try { | |
reader.close(); | |
} catch (IOException ex) { | |
return -3; | |
} | |
} | |
try { | |
return process.waitFor(); | |
} catch (InterruptedException ex) { | |
return -1; | |
} | |
} | |
} |
$ ./gradlew runDefault
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runDefault
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Hello, default ScriptingContainer.
Java : <null>
printenv : <null>
Ruby1 : <>
Ruby2 : <>
>> Ruby1: ENV['TESTENV'] = 'foo'
Java : <null>
printenv : <null>
Ruby1 : <foo>
Ruby2 : <foo>
>> Ruby2: ENV['TESTENV'] = 'bar'
Java : <null>
printenv : <null>
Ruby1 : <bar>
Ruby2 : <bar>
BUILD SUCCESSFUL
Total time: 8.596 secs
$ ./gradlew runThreadSafe
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runThreadSafe
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Hello, LocalContextScope.THREADSAFE.
Java : <null>
printenv : <null>
Ruby1 : <>
Ruby2 : <>
>> Ruby1: ENV['TESTENV'] = 'foo'
Java : <null>
printenv : <null>
Ruby1 : <foo>
Ruby2 : <>
>> Ruby2: ENV['TESTENV'] = 'bar'
Java : <null>
printenv : <bar>
Ruby1 : <foo>
Ruby2 : <bar>
BUILD SUCCESSFUL
Total time: 9.894 secs
$ ./gradlew runSingleton
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runSingleton
Hello, singleton ScriptingContainer.
Java : <null>
printenv : <null>
Ruby1 : <>
Ruby2 : <>
>> Ruby1: ENV['TESTENV'] = 'foo'
Java : <null>
printenv : <null>
Ruby1 : <foo>
Ruby2 : <foo>
>> Ruby2: ENV['TESTENV'] = 'bar'
Java : <null>
printenv : <null>
Ruby1 : <bar>
Ruby2 : <bar>
BUILD SUCCESSFUL
Total time: 3.613 secs
$ ./gradlew runSingletonNativeEnv
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runSingletonNativeEnv
Hello, singleton ScriptingContainer.
Java : <null>
printenv : <null>
Ruby1 : <>
Ruby2 : <>
>> Ruby1: ENV['TESTENV'] = 'foo'
Java : <null>
printenv : <foo>
Ruby1 : <foo>
Ruby2 : <foo>
>> Ruby2: ENV['TESTENV'] = 'bar'
Java : <null>
printenv : <bar>
Ruby1 : <bar>
Ruby2 : <bar>
BUILD SUCCESSFUL
Total time: 3.818 secs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/jruby/jruby/wiki/RedBridge