Sunday, November 19, 2006

BSF4Rexx Available on the Apple Macintosh

The Bean Scripting Facility is an IBM tool, since donated to Apache, for embedding scripting into the Java VM. There is a version for Object Rexx, that now also is available under MacOSX.

This is because it needs one native component to take care of the interfacing from Java to the (native) ooRexx interpreter - this is sharp contrast to NetRexx, that lives exclusively in the Java universe.

There are some advantages to using Object Rexx to interact with your Java objects. Firstly, it is easier to script OS commands and get the output back into your program. In Java, you need to do a Runtime.exec(), hook the filehandles for output and spawn a tread to look into those. In Object Rexx, the command is executed by putting it into quotes and sending it into an external queue.

Secondly, in Object Rexx it is possible and even easy to create runtime classes and add fields or methods to them using messages. For NetRexx and Java, we need to do awkward runtime bytecode modifications to accomplish the same goal, and still have class loader issues to run the newly composed classes or their instances.

Thirdly, Object Rexx has mixin classes and metaclasses that are simply not there in Java.

But the most important part about this, is that we now have greater freedom and flexibility to quickly implement our architectures or just run prototypes for feasibility studies.

A quick example using jdbc to a database:

/* ooRexx program to demo jdbc capabilities */
-- specify imports
jclass = .bsf~bsf.import("java.lang.Class")
driverMgr = .bsf~bsf.import("java.sql.DriverManager")

-- instantiate jdbc driver
jclass~forname('org.postgresql.Driver')~newinstance

-- make the dbms connection and open a statement
statement = driverMgr~getConnection('jdbc:postgresql:rvjansen','rvjansen','')~createStatement
signal on syntax
statement~executeUpdate("DROP TABLE test") -- catch exception for first time
syntax:
-- specify query and execute to get result set
statement~executeUpdate("CREATE TABLE test( name char(42), place char(42))")

statement~executeUpdate("INSERT INTO test (name, place) VALUES('Rony Flatscher', 'Vienna')")
statement~executeUpdate("INSERT INTO test (name, place) VALUES('Lee Peedin', 'Wallace')")
statement~executeUpdate("INSERT INTO test (name, place) VALUES('Rene Jansen', 'Amsterdam')")

-- select database content
rs = statement~executeQuery('select name, place from test')
do while rs~next
say rs~getString("name")~strip "from" rs~getString("place")~strip
end

-- calculate total
rs = statement~executeQuery('select count(*) from test')
do while rs~next
say "BSF4Rexx has at least" rs~getString(1) "fans!"
end

::requires bsf.cls

No comments: