I am trying to integrate the Sling scala script engine with an Apache Pivot
project. Pivot allows creation of GUI's utilizing an XML markup language
called BXML and JSR233 scriptengines. I am told in the Pivot Mailing list
that if I am able to build a test app by calling getEngineByName("scala") it
should work. I have downloaded and built the Sling scriptengine and am able
to run the following code:
package main.scala.pivot_scala
import javax.script.ScriptContext
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.ScriptException
import javax.script.SimpleBindings
import org.apache.sling.scripting.scala._
//import org.apache.sling.scripting.scala.ScalaScriptEngineFactory
//import scala.tools.nsc.settings
/**
* Hello world!
*
*/
object HelloScala {
def main(args: Array[String]) {
println("Hello World!")
//This doesn't work
//val mgr = new ScriptEngineManager()
//val engine = mgr.getEngineByName("scala")
//This works
val engine = (new ScalaScriptEngineFactory).getScriptEngine
val script = """
package script {
class Demo(args: DemoArgs) {
println("Hello from scala")
}
}
"""
engine.getContext.setAttribute("scala.script.class", "script.Demo",
ScriptContext.ENGINE_SCOPE)
try {
engine.eval(script)
} catch {
case ex => ex.printStackTrace
}
}
}
If I try to run a a script by calling getEngineByName("scala") I get an npe.
//This doesn't work
//val mgr = new ScriptEngineManager()
//val engine = mgr.getEngineByName("scala")
I think the problem is that I am unable to get a ScriptEngine for a given
name but only by using the ScalaScriptEngineFactory. Can you let me know
whether this is a Pivot issue or the way the Sling engine creates a
ScriptEngine. I have also posted this question on the Sling Dev list. Thanks
for any assistance on this. Below is description of getting a ScriptEngine
from Eclipse.
Looks up and creates a ScriptEngine for a given name. The algorithm first
searches for a ScriptEngineFactory that has been registered as a handler for
the specified name using the registerEngineName method.
If one is not found, it searches the array of ScriptEngineFactory instances
stored by the constructor for one with the specified name. If a
ScriptEngineFactory is found by either method, it is used to create instance
of ScriptEngine.
Parameters:
shortName The short name of the ScriptEngine implementation. returned by the
getNames method of its ScriptEngineFactory.
Returns:
A ScriptEngine created by the factory located in the search. Returns null if
no such factory was found. The ScriptEngineManager sets its own globalScope
Bindings as the GLOBAL_SCOPE Bindings of the newly created ScriptEngine.
Throws:
NullPointerException - if shortName is null.
--
View this message in context:
http://apache-sling.73963.n3.nabble.com/Using-scala-script-engine-in-Apache-Pivot-tp2795070p2795070.html
Sent from the Sling - Dev mailing list archive at Nabble.com.