This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scala Bean Scripting Framework Binding

No replies
richard emberson
Joined: 2010-03-22,
User offline. Last seen 42 years 45 weeks ago.

So, Friday I implemented the Bean Scripting Framework (bsf)
BSFEngine for Scala (see http://jakarta.apache.org/bsf/).
I wanted to write Scala in an Ant script element.
I have used Beanshell to script Java in such elements, so why not
Scala.

I created both a Java implementation of the BSFEngine and
a Scala implementation. They both extend BSFEngineImpl and
have very similar code . It was pretty simple to do. The
hardest part was figuring out that the "scala.home" attribute
had to be set (to $SCALA_HOME) and that the CLASSPATH environment
variable for the process that invokes Ant has to include:
CLASSPATH=$CLASSPATH:$ANT_HOME/lib/ant.jar
CLASSPATH=$CLASSPATH:$ANT_HOME/lib/ant-apache-bsf.jar
CLASSPATH=$CLASSPATH:$ANT_HOME/lib/ant-nodeps.jar
If these steps are not taken, then very obscure error messages are
generated, but Google is your friend and I was not the first to
see such messages.

What is important (to me) is that out-of-the-box Ant can use the
Scala-BSF binding.

Now, Ant has a property file called Languages.properties which
is in the Ant jar file. It contains:
javascript = org.apache.bsf.engines.javascript.JavaScriptEngine, js
jacl = org.apache.bsf.engines.jacl.JaclEngine, jacl
netrexx = org.apache.bsf.engines.netrexx.NetRexxEngine, nrx
java = org.apache.bsf.engines.java.JavaEngine, java
javaclass = org.apache.bsf.engines.javaclass.JavaClassEngine, class
bml = org.apache.bml.ext.BMLEngine, bml
vbscript = org.apache.bsf.engines.activescript.ActiveScriptEngine, vbs
jscript = org.apache.bsf.engines.activescript.ActiveScriptEngine, jss
perlscript = org.apache.bsf.engines.activescript.ActiveScriptEngine, pls
perl = org.apache.bsf.engines.perl.PerlEngine, pl
jpython = org.apache.bsf.engines.jpython.JPythonEngine, py
jython = org.apache.bsf.engines.jython.JythonEngine, py
lotusscript = org.apache.bsf.engines.lotusscript.LsEngine, lss
xslt = org.apache.bsf.engines.xslt.XSLTEngine, xslt
pnuts = pnuts.ext.PnutsBSFEngine, pnut
beanbasic = org.apache.bsf.engines.beanbasic.BeanBasicEngine, bb
beanshell = bsh.util.BeanShellBSFEngine, bsh
ruby = org.jruby.javasupport.bsf.JRubyEngine, rb
judoscript = com.judoscript.BSFJudoEngine, judo|jud
groovy = org.codehaus.groovy.bsf.GroovyEngine, groovy|gy
objectscript = oscript.bsf.ObjectScriptEngine, os
prolog = ubc.cs.JLog.Extras.BSF.JLogBSFEngine, plog|prolog
rexx = org.rexxla.bsf.engines.rexx.RexxEngine, rex | rexx | cls | rxj | rxs
In the Ant script element one sets the "language" attribute to be
one of the names in column 1 and Ant will try to construct and instance
of the classname from colunm 2 and cast that instance to a BMLEngine.
It would be great if the Ant maintainers were to add the Scala-BSF
binding class to the Languages.properties file.

One notices in the above list of bindings that either the binding
is actually part of the Ant distribution if the classname
starts with:
"org.apache.bsf.engines."
or it is in some third-party jar file, e.g., for beanshell:
"bsh.util.BeanShellBSFEngine"

[ In either case, the user has to place the language jar file in
the $ANT_HOME/lib directory. For beanshell, bsh.jar is in the
lib directory. For Scala I had to place both scala-library.jar
and scala-compiler.jar in the lib directory. This is OK since this
is how bsf works in Ant. ]

There is no external API or property that permits one to extend
the available script name/classname binds during Ant invocation.

For the Scala-BSF class what is needed is some "official" extra
package path much like Java has the javax path for additional
libraries (but I have notice that scalax has already been take).
If the Scala group dictated, say, that extra libraries blessed
and owned by them should be under "scala.extra", then I
would make the Scala-BSF classname be:
"scala.extra.bsf.ScalaBSFEngine",
I would give the code to them with any copyright they
want and the Scala group would provide access via their web site.
Now, the wrinkle is that to compile the ScalaBSFEngine requires
the bsf.jar file to be in the compilation classpath, so it has
to be build with a special build environment - not the one used
to build the rest of Scala. I imagine that it would be an
adjunct-download.

At any rate, with an "official" Scala-BSF binding, it can be
requested of the Ant maintainers that they include the following
line to the Ant Languages.properties file:
scala = scala.extra.bsf.ScalaBSFEngine, scala

This more than likely would be more acceptable to them than,
say, a line such as:
scala = richard.embersons.bsf.ScalaBSFEngine, scala

If the line is not added to the Languages.properties file
(there is no such line in the Ant jar file on my machine) one
has to register the script attribute name "scala" and
classname with the org.apache.bsf.BSFManager. I currently do
this with the following Ant script:

This register both my Scala version, "scala" and my Java version, "jscala".
If I do the registration in Ant before I call a scala script, everything
works.

But, again, I'd like a more general solution than the above Beanshell
Ant script approach to registration.

[ Another solution is to write a bash script that pulls the
Languages.properties file out of one's ant.jar file, adds the
appropriate Scala line to it and, then, stuffs it back into the
jar file. ]

Richard

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland