Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up jsr223 scripting with scala as scripting language

So far I have tried the sling implementation for jsr223 scripting for scala, but was not able to get it set up correctly. when I do this:

public static void main(String[] args) {
    try {
        new ScriptEngineManager().getEngineByName("scala").
          eval("object HelloWorld {def main(args: Array[String]) { 
                  println(\"Hello, world!\") }}");
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

I got nothing but:

javax.script.ScriptException: ERROR 
org.apache.sling.scripting.scala.Script line 13 : not found: type 
Script at org.apache.sling.scripting.scala.ScalaScriptEngine.eval(ScalaScriptEngine.scala:117)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)

similar Problems are discussed here: http://scala-programming-language.1934581.n4.nabble.com/How-to-compile-Scala-code-from-java-using-the-current-ClassLoader-instead-of-a-string-based-classpat-td1955873.html#a1955873

and

http://dev.day.com/discussion-groups/content/lists/sling-dev/2009-12/2009-12-01_Scala_scripting_support_was_Re_And_another_one____Michael_D_rig.html

maybe there is another Implementation that I'm not aware of.

Any help appreciated

like image 413
VivaceVivo Avatar asked Apr 13 '11 19:04

VivaceVivo


1 Answers

Have a look at the test cases in the scala/script module of Apache Sling for a working example. The script and its entry point (that is the object) need to follow certain conventions. I'll provide more information on these if required later.

For a general overview of the scripting engine see my session slides from Scala Days 2010.

Update: Scripts must be of the following form:

package my.cool.script {
  class foo(args: fooArgs) {
    import args._ // import the bindings
    println("bar:" + bar)
  }
}

The type of args is generated by the script engine and is named after the simple class name of the script appended with 'Args'. Further the example assumes, that the Bindings passed for script evaluation contains a value for the name 'bar'. For further details see the class comment on ScalaScriptEngine.

You need to pass the name of your script class to the script engine. You do this by putting the fully qualified script name (i.e. my.cool.script.foo) into the ScriptContext by the name 'scala.script.class'.

like image 164
michid Avatar answered Sep 27 '22 19:09

michid