Thursday, October 7, 2010

Native Java support

A new project has let me to finally realize something I wanted to do for some time. That is direct support for native java objects as expression values.

Currently there is the "jcall" command which supports calling the main method of a java program in the same JVM. This is useful to avoid a new java process overhead, but is not that useful to get at native java objects and methods which are not already exposed to the language.

There is also extension modules which allow you to write custom java code and expose it as commands. However the types of values going into and out of the modules are still limited to the native types of xmlsh (XDM values).

There is use for being able to pass in native java Objects to some commands. For example the xsql command currently requires connecting to the database every call, but if you could create, persist, and pass in a Connection object it could be reused for multiple calls.

Now you can. Not yet released, but checked into the source repository is support for allowing any java Object to be passed as an expression or stored in a variable.

But how do you *get* these java objects created and assigned ?
One way is to pass them in through the calling application, setting them as an XValue to the shell positional parameters or environment variables.

Another way is a new command "jset" which lets you create java objects, set them to variables and then call methods on the objects and assign those to other variables.


jset -v str -c java.lang.String "Hi There"

will create a java String object and assign it to $str

You can do the same for Date, for example

jset -v date -c java.util.Date
echo $date

You can also call methods (static or instance).

jset -v len -o $str -m length

sets $len to the result of String.length()

I'm still working on a better syntax, possibly with combination to the "tie" command so you can tie method invocation to variable expansion.

I'd like to see something like ${str:length} call the length() method of the object in $str
but I need to flesh out issues like passing in arguments.

Be ready for a new release soon with this support ! Suggestions as always are welcome.

No comments:

Post a Comment

Due to comment spam, moderation is turned on. I will approve all non-spam comments.