The first tips are how to get an engine's instance, which is a prerequisite to get scripts run. Scripting API has a couple of methods available. As for JRuby engine, following three ways work well for getting javax.script.ScriptEngine typed instance:
or
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");
or
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("ruby");
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("rb");
Users can get engine's instance by one of these. Although scripting API defines ScriptEngineManager#getEngineByMimeType() method, no coder can use this method. Since JRuby engine doesn't define its mime types obtainable, users don't have any parameter to pass to.
Here's an entire example code for the first step:
package canna;This code outputs like this:
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class GettingEngineExample {
GettingEngineExample() throws ScriptException {
caseOne();
caseTwo();
caseThree();
caseFour();
}
void caseOne() throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");
ScriptEngineFactory factory = engine.getFactory();
System.out.println("engine name: " + factory.getEngineName());
System.out.println("version: " + factory.getEngineVersion());
engine.eval("puts 'Case one: getEngineByName(\"jruby\").'");
}
void caseTwo() throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("ruby");
ScriptEngineFactory factory = engine.getFactory();
System.out.println("engine name: " + factory.getEngineName());
System.out.println("version: " + factory.getEngineVersion());
engine.eval("puts 'Case two: getEngineByName(\"ruby\").'");
}
void caseThree() throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("rb");
ScriptEngineFactory factory = engine.getFactory();
System.out.println("engine name: " + factory.getEngineName());
System.out.println("version: " + factory.getEngineVersion());
engine.eval("puts 'Case three: getEngineByExtension(\"rb\").'");
}
void caseFour() {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("rb");
ScriptEngineFactory factory = engine.getFactory();
Listnames = factory.getMimeTypes();
System.out.println("mime types are ...");
for (String name : names) {
System.out.print(name);
}
}
public static void main(String[] args) throws ScriptException {
new GettingEngineExample();
}
}
engine name: JRuby Engine
version: 1.1.1
Case one: getEngineByName("jruby").
engine name: JRuby Engine
version: 1.1.1
Case two: getEngineByName("ruby").
engine name: JRuby Engine
version: 1.1.1
Case three: getEngineByExtension("rb").
mime types are ...
No comments:
Post a Comment