Pages

Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Monday, July 30, 2007

JSR 223 and Groovy 1.1 Beta 2

In Groovy version 1.1-beta-2 a new class groovy.lang.GroovySystem provides some nice features that I would like to use in my Google Summer of Code project. Namely, this is one way to implement a bundle activator for an eclipse plug-in. Not really an activator, actually, but sort of.

In a scripted bundle, the activator can be just a singleton class, and GroovySystem can be used to create a fancy singleton pattern in Groovy, like it is described at Groovy's homepage.

The problem I faced when I tried to initialize the scripting engine using javax.sript API, was that the engine implementation still thinks that groovy.lang.MetaClass is a class, but it is an interface in the latest version of Groovy.

So the stack trace of this exception was:

Exception in thread "main"
java.lang.IncompatibleClassChangeError:
at com.sun.script.groovy.GroovyScriptEngine
.(GroovyScriptEngine.java:63)
at com.sun.script.groovy.GroovyScriptEngineFactory
.getScriptEngine(GroovyScriptEngineFactory.java:87)
at javax.script.ScriptEngineManager
.getEngineByName(ScriptEngineManager.java:225)

The quickest fix I could do, is to download the scripting engine sources from the website, and comment out the line in GroovyScriptEngine's static initializer. After the recompilation I didn't get the stack trace any more, but I think I have to investigate a bit more, what could be affected by this change.

Wednesday, April 18, 2007

JSR 223: Scripting for Java

As Charles Nutter suggested, I decided to try javax.scripting API. The Java 6 Revealed book has a nice chapter on JSR 223 and also an example of Pnuts integration with JVM.
I have downloaded the other engines from java.net. It seems that for my SOC project it is reasonable to use Java 6. Virtually it will be possible to write plug-ins in any scripting language for which the engine is available. We could even use combination of many languages, just selecting the relevant engine by name or file extension:

ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine groovyEngine = m.getEngineByName("groovy");
ScriptEngine jrubyEngine = m.getEngineByName("jruby");

The full list of the scripting engines is available at java.net

Monday, December 18, 2006

Groovy from SQL and templates

Groovy is a real fun! Get some data from database and put it to XML - must be quick-n-dirty :)
Let the table be:
-- Apache Derby ij script
connect 'jdbc:derby:../../myderby;create=true;user=test;password=test';
create table test(
name varchar(20),
age integer,
gender char(1)
);
insert into test values ('John Doe', 55, 'M');
insert into test values ('Diana Smith', 34, 'F');
insert into test values ('Foo Bar', 21, 'M');
disconnect;
exit;
... and the template.txt:
<person>
<name>${name}</name>
<age>${age}</age>
<gender>${gender}</gender>
</person>
.. and now, Groovy rocks! :)
import groovy.sql.Sql
import groovy.text.Template
import groovy.text.SimpleTemplateEngine
import java.io.File

class SQL2XML{
static void main(args) {
def file = new File("template.txt")
def engine = new SimpleTemplateEngine()
def template = engine.createTemplate(file)
def sql = Sql.newInstance("jdbc:derby:myderby",
"test", "test",
"org.apache.derby.jdbc.EmbeddedDriver")
sql.eachRow("SELECT * FROM TEST") {
def binding = ["name":"${it.name}",
"age":"${it.age}",
"gender":"${it.gender}"]
def result = template.make(binding)
println result
}
}
}

It seems to me that the dynamic features might be useful for adding customizations to the end-user application.

Disqus for Code Impossible