Thursday, March 06, 2008

Tips for JRuby engine: how to get engine's instance

Writing a code by using scripting API introduced in JDK 6 might be enjoyable because it enables us to use both dynamic languages and Java at a time, smoothly. Moreover, it enables to mix multiple dynamic languages without writing any Java code. For example, Rhino can execute Ruby code over scripting API. However, documents are few, unfortunately, and users often confuse how to use the API. In light of this bad situation, I'll try to give users some tips about JRuby engine to possibly help them out from annoying exceptions and errors.

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:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");
or

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("ruby");
or

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;

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();
List names = 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();
}
}
This code outputs like this:

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: