You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by janandith jayawardena <ja...@gmail.com> on 2008/06/15 21:10:35 UTC

Weekly Report for Sling/Scala [Need help in solving problem]

Hi,

The report is a bit different this week because my intention was to load the
scala Interpreter in sling.

I tried to fix the scala interpreter loading problem this week. This is the
most important thing in sling-scala.

In doing so I found that the Scala Interpreter is a special case of the
compiler.[1]
That means a wrapper to the compiler.
The compiler has to be notified the classpath location to the
scala-library-2.7.1.jar or other scala-library jar version.

This should be done by passing it using the scala.tools.nsc.Settings object.
If the classpath is not specified Interpreter will look in the paths it
guesses as in [2].


I tried the following and different variations of them but failed,

1. Inside the engine I tried the solution suggested by Felix using
ClassLoader.currentTheread().

2. loading "scala.tools.nsc.Interpreter" using
ClassLoader.loadClass("scala.tools.nsc.Interpreter") which returns null.

3. Reading the URL of the parent class loaders which doesn't go beyond
"felix://extensions/"   I passed this to the Settings.

4. Using the <Import-package> and <Private-Package> directives in maven.
These help compiling but doesn't include the scala-library to the current
class path.

5. The "maven-scala-plugin" is only able to compile and run scala code using
the "compile" , "testcompile". There is no "install" goal. This requires
.scala files. Even though java code can be run in scala I think scala byte
code cannot be run in the jvm. Although I tried to change the extension
there were syntax errors because the plugin expects scala syntax for
methods.

6. Tried to write a dummy class in scala which creates the interpreter and
passes it the Setting without absolute path and pass it the script string
within eval method.

As the bundle ID reads I have tried these a 184 times.


My suggested solution,

1. While compiling in maven copy the
"m2/repository/org/scala-lang/scala-library/2.7.1/scala-library-2.7.1.jar"
in to a directory in target/lib and create a plain file with the version.
Then possible to give the classpath as "/lib/scala-library"+ version +
"jar".

I don't know whether it's possible in maven or how it's done if possible.


2. Create an osgi bundle using the existing maven dependancy "scala-library"
in repository to include it in "apache felix". This might help in solving
the problem but I don't know the correct way to do it. According to what I
found in google it's possible to create osgi bundles in scala code and
compile using the "maven-scala-plugin" but what I want is to wrap the
scla-library as a bundle to load in sling while building using maven..

3. Setting an environment variable to the maven repository and get the
classpath using
"org/scala-lang/scala-library/2.7.1/scala-library-2.7.1.jar".



I'm currently confused with this problem. I can write tests for the current
code but even though all other code is completed using

"Settings.classpath().value_$eq("/home/janandith/.m2/repository/org/scala-lang/scala-library/2.7.1/scala-library-2.7.1.jar");"
temporarily it would leave with the same problem of avoiding path setting in
the completed project.

What are your suggesstions ?. How can I move forward in solving this issue.


[1]
https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/compiler/scala/tools/nsc/Interpreter.scala
[2]
https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/compiler/scala/tools/nsc/Settings.scala

Re: Weekly Report for Sling/Scala [Need help in solving problem]

Posted by Karl Pauls <ka...@gmail.com>.
On Mon, Jun 16, 2008 at 9:14 AM, Felix Meschberger <fm...@gmail.com> wrote:
> Hi,
>
> I am not 100% clear, whether I understand the issue (and my Scala
> knowledge is close to NIL).
>
> But if Scala really needs an explicit class path (with Strings not
> URLs !), I would say, this is a real problem and virtually prevents
> Scala from being used in a truly embedded way.
>
> I tend to think, this a fundamental shortcomming of this whole
> stuff ....
>
> Maybe Bertrand has more insight...
>
>
> Of course, there could be a solution to this problem, but to be honest,
> I really do NOT like it and would more like consider it a "short-term
> workaround to get this thing running" solution and would oppose to any
> release of such a thing.
>
> Having said this, here is the solution: Copy the required library to
> some filesystem location at runtime and refer to it with the filesystem
> path:

Another (just as ugly but maybe less resource intensive) workaround
could be to register a URLHandlerService with the framework for a
magic protocol (like e.g. scala-sling://). The implementation of the
handler would then just delegate to the  bundle.getResource method.

I agree that the correct thing to do is to figure out how to make
scala use something else then a string classpath.

regards,

Karl

>        BundleContext context = BundleContext of Scala
>        ScriptEngineFactory bundle
>        URL resource = bundle.getEntry("name of scala library");
>        File file =
>        context.getDataFile("someFolder/scala.lib.name.jar");
>        // copy resource to file
>        // add file.getAbsolutePath() to class path
>
> This could could be run at bundle start time. While at bundle stop time,
> I suggest to cleanup the filesystem again and remove the library file.
> Alternatively you could copy the interpreter file to the file system on
> request instead at bundle start time.
>
> But, to repeat myself: The correct solution would be to get Scala to
> operate on a ClassLoader and NOT on some class path based on
> String-ified URLs
>
> Regards
> Felix
>
>
> Am Montag, den 16.06.2008, 00:40 +0530 schrieb janandith jayawardena:
>> Hi,
>>
>> The report is a bit different this week because my intention was to load the
>> scala Interpreter in sling.
>>
>> I tried to fix the scala interpreter loading problem this week. This is the
>> most important thing in sling-scala.
>>
>> In doing so I found that the Scala Interpreter is a special case of the
>> compiler.[1]
>> That means a wrapper to the compiler.
>> The compiler has to be notified the classpath location to the
>> scala-library-2.7.1.jar or other scala-library jar version.
>>
>> This should be done by passing it using the scala.tools.nsc.Settings object.
>> If the classpath is not specified Interpreter will look in the paths it
>> guesses as in [2].
>>
>>
>> I tried the following and different variations of them but failed,
>>
>> 1. Inside the engine I tried the solution suggested by Felix using
>> ClassLoader.currentTheread().
>>
>> 2. loading "scala.tools.nsc.Interpreter" using
>> ClassLoader.loadClass("scala.tools.nsc.Interpreter") which returns null.
>>
>> 3. Reading the URL of the parent class loaders which doesn't go beyond
>> "felix://extensions/"   I passed this to the Settings.
>>
>> 4. Using the <Import-package> and <Private-Package> directives in maven.
>> These help compiling but doesn't include the scala-library to the current
>> class path.
>>
>> 5. The "maven-scala-plugin" is only able to compile and run scala code using
>> the "compile" , "testcompile". There is no "install" goal. This requires
>> .scala files. Even though java code can be run in scala I think scala byte
>> code cannot be run in the jvm. Although I tried to change the extension
>> there were syntax errors because the plugin expects scala syntax for
>> methods.
>>
>> 6. Tried to write a dummy class in scala which creates the interpreter and
>> passes it the Setting without absolute path and pass it the script string
>> within eval method.
>>
>> As the bundle ID reads I have tried these a 184 times.
>>
>>
>> My suggested solution,
>>
>> 1. While compiling in maven copy the
>> "m2/repository/org/scala-lang/scala-library/2.7.1/scala-library-2.7.1.jar"
>> in to a directory in target/lib and create a plain file with the version.
>> Then possible to give the classpath as "/lib/scala-library"+ version +
>> "jar".
>>
>> I don't know whether it's possible in maven or how it's done if possible.
>>
>>
>> 2. Create an osgi bundle using the existing maven dependancy "scala-library"
>> in repository to include it in "apache felix". This might help in solving
>> the problem but I don't know the correct way to do it. According to what I
>> found in google it's possible to create osgi bundles in scala code and
>> compile using the "maven-scala-plugin" but what I want is to wrap the
>> scla-library as a bundle to load in sling while building using maven..
>>
>> 3. Setting an environment variable to the maven repository and get the
>> classpath using
>> "org/scala-lang/scala-library/2.7.1/scala-library-2.7.1.jar".
>>
>>
>>
>> I'm currently confused with this problem. I can write tests for the current
>> code but even though all other code is completed using
>>
>> "Settings.classpath().value_$eq("/home/janandith/.m2/repository/org/scala-lang/scala-library/2.7.1/scala-library-2.7.1.jar");"
>> temporarily it would leave with the same problem of avoiding path setting in
>> the completed project.
>>
>> What are your suggesstions ?. How can I move forward in solving this issue.
>>
>>
>> [1]
>> https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/compiler/scala/tools/nsc/Interpreter.scala
>> [2]
>> https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/compiler/scala/tools/nsc/Settings.scala
>
>



-- 
Karl Pauls
karlpauls@gmail.com

Re: Weekly Report for Sling/Scala [Need help in solving problem]

Posted by janandith jayawardena <ja...@gmail.com>.
Hi Bertrand,

      Thanks for the ideas. I will be able to get the classpath hack you've
shown.

>The current scripting engines use a "binding" mechanism to make java
>objects accessible in java scripts,

   is this done using "org.mozilla.javascript.ScriptRuntime" . This is used
quite frequently in the wrapper classes in java script code.

How is a "sling:resourceType" represented. Is it accessible via an interface
or should it be maped to an object of the type "jcr.Node" which is kept in a
HashMap like data structure.

 Janandith

Re: Weekly Report for Sling/Scala [Need help in solving problem]

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi Janandith,

I had a closer look at the scala classpath problem and didn't found a
better solution than setting the classpath as a String on the settings
object, see how the "libJar" variable is used in [1]. That "minbundle"
works for me, requesting an existing resource with ".compile.scala"
added at the end of URL spits out the message generated by the scala
code.

We'll need to fix this, but for now that's good enough to start with,
if you can get this to work in your scripting engine bundle I'd
suggest that you go on with that, and leave the more hairy classpath
problem for later.

On Mon, Jun 16, 2008 at 8:56 PM, janandith jayawardena
<ja...@gmail.com> wrote:
> ...1. The engine needs to work with nodes so should I follow the path taken by
> the java script engine ?...

The ScriptableNode and related classes in the javascript engine are
meant to give easier access to the JCR objects in server-side
javascript. I don't know how Scala would handle that, and for now if
you're able to access the "currentNode" variable in scala that'd be
good enough.

The current scripting engines use a "binding" mechanism to make java
objects accessible in java scripts, you'll need to do the same for
Scala. But keep it as simple as possible for now, for the next steps
I'd suggest

1) make the scala engine work on a "hello world" script, using the
classpath hack

2) find out how to make Sling variables (currentNode etc.) available
to Scala scripts

And we can then take it from there.

> ....2. To parse scala code within html can I use the filtering shown in
> EspReader.java in javascript engine ?...

Eventually yes, but right now I'd suggest sticking to scala scripts
that use an "out" variable to print stuff to the response. Keep it as
simple as possible until you're able to get some data coming from a
JCR node in output generated by scala code.

-Bertrand

[1] http://code.google.com/p/sling-scala/source/browse/trunk/scala-min-bundle/src/main/java/org/apache/sling/scripting/scala/minbundle/ScalaCompileServlet.java

Re: Weekly Report for Sling/Scala [Need help in solving problem]

Posted by janandith jayawardena <ja...@gmail.com>.
Hi,

Thanks for throwing me a lifeline Bertrand.I'll write tests and proceed with
the rest until a solution is found.
I think Bertrand it would be best to ask from scala folks if possible. I
posted about this in the scala mailing list some time ago but there was no
positive reply. You can check this post by using the following link,

http://www.nabble.com/Help-solve-exception-thrown-when-calling-interpreter-in-scala.-td17753046.html#a17753046

Also I found the following URL's which directed me to my conclusion.

[1]
https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/build.examples.xml

This shows that the path to the library is given in the xml.


[2] http://www.nabble.com/Scala-Interpreter-inside-OSGi-td15733281.html

This document shows the exception thrown when calling the Interpreter and it
shows an example of setting the library path using a string.

[3] http://www.nabble.com/Compiler-API-td12050645.html#a12051708

The "error" passed to Settings in the code given uses a guessed path
according to whats defined in scala.tools.nsc.Settings.

I realised this by looking at
https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/compiler/scala/tools/nsc/Main.scala



A couple of questions more.

1. The engine needs to work with nodes so should I follow the path taken by
the java script engine ?

2. To parse scala code within html can I use the filtering shown in
EspReader.java in javascript engine ?


Thanks Felix and Karl for sharing your views .

regards,
janandith.

Re: Weekly Report for Sling/Scala [Need help in solving problem]

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Mon, Jun 16, 2008 at 9:14 AM, Felix Meschberger <fm...@gmail.com> wrote:
> ...if Scala really needs an explicit class path (with Strings not
> URLs !), I would say, this is a real problem and virtually prevents
> Scala from being used in a truly embedded way...

Agreed. Janandith, could you indicate precisely where you got that
info, that the Scala compiler (we don't really care about the
interpreter as that's only able to process single-line scripts IIUC)
requires the path to some jars as a String, as opposed to finding the
corresponding classes in its classpath?

Once you provide this info, we can maybe check with the Scala folks if
there's really no other way, or work with them to fix that.

In the meantime, I suggest for Janandith to work around the problem in
any one of the suggested ways (find the easiest path, no worries about
being clean at this point).

And if you're stuck, don't waste too much time on this, but, as
suggested before, work on automated tests outside of the OSGi
environment, using the scripting/javascript module tests as examples,
so that you can do useful stuff while we research that more complex
problem. And make sure to commit your work to the google code SVN so
that we see some progress.

-Bertrand

Re: Weekly Report for Sling/Scala [Need help in solving problem]

Posted by Felix Meschberger <fm...@gmail.com>.
Hi,

I am not 100% clear, whether I understand the issue (and my Scala
knowledge is close to NIL).

But if Scala really needs an explicit class path (with Strings not
URLs !), I would say, this is a real problem and virtually prevents
Scala from being used in a truly embedded way.

I tend to think, this a fundamental shortcomming of this whole
stuff ....

Maybe Bertrand has more insight...


Of course, there could be a solution to this problem, but to be honest,
I really do NOT like it and would more like consider it a "short-term
workaround to get this thing running" solution and would oppose to any
release of such a thing.

Having said this, here is the solution: Copy the required library to
some filesystem location at runtime and refer to it with the filesystem
path:

        BundleContext context = BundleContext of Scala
        ScriptEngineFactory bundle
        URL resource = bundle.getEntry("name of scala library");
        File file =
        context.getDataFile("someFolder/scala.lib.name.jar");
        // copy resource to file
        // add file.getAbsolutePath() to class path

This could could be run at bundle start time. While at bundle stop time,
I suggest to cleanup the filesystem again and remove the library file.
Alternatively you could copy the interpreter file to the file system on
request instead at bundle start time.

But, to repeat myself: The correct solution would be to get Scala to
operate on a ClassLoader and NOT on some class path based on
String-ified URLs

Regards
Felix


Am Montag, den 16.06.2008, 00:40 +0530 schrieb janandith jayawardena:
> Hi,
> 
> The report is a bit different this week because my intention was to load the
> scala Interpreter in sling.
> 
> I tried to fix the scala interpreter loading problem this week. This is the
> most important thing in sling-scala.
> 
> In doing so I found that the Scala Interpreter is a special case of the
> compiler.[1]
> That means a wrapper to the compiler.
> The compiler has to be notified the classpath location to the
> scala-library-2.7.1.jar or other scala-library jar version.
> 
> This should be done by passing it using the scala.tools.nsc.Settings object.
> If the classpath is not specified Interpreter will look in the paths it
> guesses as in [2].
> 
> 
> I tried the following and different variations of them but failed,
> 
> 1. Inside the engine I tried the solution suggested by Felix using
> ClassLoader.currentTheread().
> 
> 2. loading "scala.tools.nsc.Interpreter" using
> ClassLoader.loadClass("scala.tools.nsc.Interpreter") which returns null.
> 
> 3. Reading the URL of the parent class loaders which doesn't go beyond
> "felix://extensions/"   I passed this to the Settings.
> 
> 4. Using the <Import-package> and <Private-Package> directives in maven.
> These help compiling but doesn't include the scala-library to the current
> class path.
> 
> 5. The "maven-scala-plugin" is only able to compile and run scala code using
> the "compile" , "testcompile". There is no "install" goal. This requires
> .scala files. Even though java code can be run in scala I think scala byte
> code cannot be run in the jvm. Although I tried to change the extension
> there were syntax errors because the plugin expects scala syntax for
> methods.
> 
> 6. Tried to write a dummy class in scala which creates the interpreter and
> passes it the Setting without absolute path and pass it the script string
> within eval method.
> 
> As the bundle ID reads I have tried these a 184 times.
> 
> 
> My suggested solution,
> 
> 1. While compiling in maven copy the
> "m2/repository/org/scala-lang/scala-library/2.7.1/scala-library-2.7.1.jar"
> in to a directory in target/lib and create a plain file with the version.
> Then possible to give the classpath as "/lib/scala-library"+ version +
> "jar".
> 
> I don't know whether it's possible in maven or how it's done if possible.
> 
> 
> 2. Create an osgi bundle using the existing maven dependancy "scala-library"
> in repository to include it in "apache felix". This might help in solving
> the problem but I don't know the correct way to do it. According to what I
> found in google it's possible to create osgi bundles in scala code and
> compile using the "maven-scala-plugin" but what I want is to wrap the
> scla-library as a bundle to load in sling while building using maven..
> 
> 3. Setting an environment variable to the maven repository and get the
> classpath using
> "org/scala-lang/scala-library/2.7.1/scala-library-2.7.1.jar".
> 
> 
> 
> I'm currently confused with this problem. I can write tests for the current
> code but even though all other code is completed using
> 
> "Settings.classpath().value_$eq("/home/janandith/.m2/repository/org/scala-lang/scala-library/2.7.1/scala-library-2.7.1.jar");"
> temporarily it would leave with the same problem of avoiding path setting in
> the completed project.
> 
> What are your suggesstions ?. How can I move forward in solving this issue.
> 
> 
> [1]
> https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/compiler/scala/tools/nsc/Interpreter.scala
> [2]
> https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/compiler/scala/tools/nsc/Settings.scala