I have been formulating a project with a couple of at odds requirements:
- It must interface with an Oracle database
- It should be easily extensible via a language others wouldn't mind
This brief introduction will:
- Create an instance of the Rhino interpreter
The source code in this article is available for download. The zip contains of the code in this article, the Rhino library, and a Bash script to build and test the code.
Let's first look at meat of the program:
So, what is this new host object,
Foo? It is a plain Java object that extends an object provided by the Rhino library:
getClassName method. Next, we define the constructor that will be called when we do
jsConstructor method then one will be derived. From defineClass:
Finally, we define getters and setters for our one property by prepending
jsSet, respectively, to method names. We could also define static methods on our
Foo object by prepending
jsFunction to method names (but I'd shy away from this one to prevent polluting the global space).
System object. Notice that we don't have to reference our
Foo object in any special way, other than instantiating an instance of it. This works just like the standard
Array object, except it doesn't have a literal syntax.
Packages. The properties of this
Packages object are all of the Java libraries available to the JVM under which the program is running. In our script, we use an alias,
java, defined by Rhino to access the
Packages.java objects. Further information about this, and other scripting additions, can be read in the Scripting Java article.