16.7.10

Make My Java Program Supports Dynamic Language (4)

What I want?

What I want is:

  • Transparent Dynamic Language Support.
  • Search specific paths/JARs for the script.

Transparent Dynamic Language Support

For example, I can use:

    Class clazz = Class.forName(“my.hello.World”);
MyWorld world = (MyWorld)clazz.newInstance();
world.sayHello();
MyResult result = world.cal();
System.out.println("Result is : " + result.toString());


Search specific paths/JARs for the script.


I don’t want the script engine to search too many places for the sake of performance. For example:

    MyEngine myEngine = new MyEngine();
myEngine.addScriptPath("scripts");
myEngine.addScriptJAR("lib/scripts.jar");

Candidates



  • BeanShell
  • Groovy
  • Scala

BeanShell



  • Problem: Too Slow

BeanShell is terribly slow according to my test. It’s even slower than the JavaScript version. Can someboy tell me why?


BTW: I just come across an open-source project hosted in GoogleCode names BeanShell2.



  • Problem: Lack of Class support

There is no real Class support in BeanShell. I can create an object to implement an Interface, but I can not define a “static” Class.

  public void sayHello() {
System.out.println("Hello world");
}
public String retrieveResult() {
return "Hello world as result";
}
return this;

Please refer to http://www.beanshell.org/examples.html for more examples.



  • Problem: API

The BeanShell APIs are very similar to JSR 223, It provides .eval() and .source() interface for programming. Another option to use BeanShell is via JDK 6’s Scripting Supports via https://scripting.dev.java.net/


However, none of them could be transparent. Although transparent is not very essential for me, for I am still designing my Software Architecture. However, I need to keep in mind this requirement to avoid some major changes in the previous version.



  • Can be replaced

Yes, BeanShell could be replace totally by Groovy if you just use strict Java language syntax in Groovy. So why BeanShell?

No comments: