Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] Efficient binding Java objects to Groovy scripts

Lukasz Zmudzinski

2008-08-02

Replies: Find Java Web Hosting

Author LoginPost Reply
Hello,
   I'm porting Groovy scripts to Java system, and I have a question:
I have many objects in Java environment, and many Groovy scripts that are
loaded from files.
The source of groovy scripts is unknown and can be modified during system
runtime.
The variables in scripts are binded with Java objects.

How to bind only those objects that are needed in a particular script?

Example:

    // java code, java objects, the key in hashtable is reference name
to the object
    Hashtable<String, Object> jObjects = new Hashtable<String, Object>();
    jObjects.put("a", 1);
    jObjects.put("b", 2);
    jObjects.put("c", 3);

    String script1 = "println a";
    String script2 = "println b";
    String script3 = "println c";

    GroovyClassLoader gcl = new GroovyClassLoader();

    Binding b1 = new Binding();
    Script s1 = InvokerHelper.createScript(gcl.parseClass(script1), b1);
    s1.run();

Running s1 will throw groovy.lang.MissingPropertyException: No such
property: a for class: script1217672095046,
so the smelly roundtrip to bind only needed variables is to try running
script s1 and catch MissingPropertyException in loop,
then bind property from exception by name.

Is there a better way ?

I would like to repeat that the requirement is to bind only needed objects.
The information which objects are binded to which scripts is crucial for
system to work efficient.

Thanks for all the answers,

--
Regards,
Lukasz Zmudzinski
Cracow University of Technology




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.