Load classes (not run scripts) from GroovyScriptEngine?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Load classes (not run scripts) from GroovyScriptEngine?

Marc Palmer Local

Hi,

I've got a need in my app to both execute Groovy scripts using  
GroovyScriptEngine, and also load Groovy classes. GroovyScriptEngine  
does not support this currently.

Would anybody object to me enhancing it so that you can call getClass
(String name) on GroovyScriptEngine, in addition to run()?

The idea here is that we could reuse the caching and reloading code -  
after all the only difference in functionality is that we want to  
return the class, not instantiate and run it.

Thoughts?

~ ~ ~
Marc Palmer ([hidden email])
Consultant/Analyst
AnyWare Ltd.
http://www.anyware.co.uk/

Reply | Threaded
Open this post in threaded view
|

Re: Load classes (not run scripts) from GroovyScriptEngine?

Rapheal Kaplan

>
>
> I've got a need in my app to both execute Groovy scripts using  
> GroovyScriptEngine, and also load Groovy classes. GroovyScriptEngine  
> does not support this currently.
>
> Would anybody object to me enhancing it so that you can call getClass
> (String name) on GroovyScriptEngine, in addition to run()?
>
> The idea here is that we could reuse the caching and reloading code -  
> after all the only difference in functionality is that we want to  
> return the class, not instantiate and run it.
>
> Thoughts?


  Just wondering, is the intent that the "name" parameter refers to the
script file name, just like the other GroovyScriptEngine methods?

  - Rafe

Reply | Threaded
Open this post in threaded view
|

Re: Load classes (not run scripts) from GroovyScriptEngine?

Marc Palmer Local

On 14 Dec 2005, at 20:50, Rapheal Kaplan wrote:

>
>>
>>
>> I've got a need in my app to both execute Groovy scripts using  
>> GroovyScriptEngine, and also load Groovy classes.  
>> GroovyScriptEngine  does not support this currently.
>>
>> Would anybody object to me enhancing it so that you can call  
>> getClass (String name) on GroovyScriptEngine, in addition to run()?
>>
>> The idea here is that we could reuse the caching and reloading  
>> code -  after all the only difference in functionality is that we  
>> want to  return the class, not instantiate and run it.
>>
>> Thoughts?
>
>
>  Just wondering, is the intent that the "name" parameter refers to  
> the script file name, just like the other GroovyScriptEngine methods?

I -think- so... well the usage should really be:

GroovyScriptEngine gse = new GroovyScriptEngine(...);
Class c = gse.getClass( "mycorp.scripts.SomeClass");
-or-
gse.run( "mycorp.scripts.SomeScript"); // if it is actually a script

This is an easy change to make - I've done it and am testing locally.

Cheers

~ ~ ~
Marc Palmer ([hidden email])
Consultant/Analyst
AnyWare Ltd.
http://www.anyware.co.uk/

Reply | Threaded
Open this post in threaded view
|

Re: Load classes (not run scripts) from GroovyScriptEngine?

Guillaume Laforge
Administrator
On 14/12/05, Marc Palmer <[hidden email]> wrote:
> [...]
> I -think- so... well the usage should really be:
>
> GroovyScriptEngine gse = new GroovyScriptEngine(...);
> Class c = gse.getClass( "mycorp.scripts.SomeClass");
> -or-
> gse.run( "mycorp.scripts.SomeScript"); // if it is actually a script
>
> This is an easy change to make - I've done it and am testing locally.

If it works, don't hesitate to provide a patch, attach it to a JIRA
issue, and we'll look at it soon for incorporation in the next
release.

Thanks in advance for your contributions!

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|

Re: Load classes (not run scripts) from GroovyScriptEngine?

Marc Palmer Local

On 14 Dec 2005, at 21:48, Guillaume Laforge wrote:

> On 14/12/05, Marc Palmer <[hidden email]> wrote:
>> [...]
>> I -think- so... well the usage should really be:
>>
>> GroovyScriptEngine gse = new GroovyScriptEngine(...);
>> Class c = gse.getClass( "mycorp.scripts.SomeClass");
>> -or-
>> gse.run( "mycorp.scripts.SomeScript"); // if it is actually a script
>>
>> This is an easy change to make - I've done it and am testing locally.
>
> If it works, don't hesitate to provide a patch, attach it to a JIRA
> issue, and we'll look at it soon for incorporation in the next
> release.
>
> Thanks in advance for your contributions!

No, problem - I'm very happy to do this (will call it getClassByName  
to avoid getClass clash), especially as my app needs this to work ;-)

On a related note, can anybody see any downside to me adding  
overloaded run() and getClassByName() methods that also take a  
ClassLoader parameter?

Unless I'm missing something I don't see how else we can use  
GroovyScriptEngine to execute scripts that have access to JARs and  
classes not on the classpath that the VM loaded Groovy from.  
Currently the parent class loader of the GroovyClassLoader used to  
parse the script is hardcoded to getClass().getClassLoader().  
Therefore if the groovy jar is in WEB-INF/lib of a webapp I don't  
think there is any way to have scripts use classes loaded from  
another classloader without this change.

This should also be a trivial change from where I'm standing, and I  
can add it to Jira too.

Cheers

~ ~ ~
Marc Palmer ([hidden email])
Consultant/Analyst
AnyWare Ltd.
http://www.anyware.co.uk/

Reply | Threaded
Open this post in threaded view
|

Re: Load classes (not run scripts) from GroovyScriptEngine?

Guillaume Laforge
Administrator
Hello Marc,

On 15/12/05, Marc Palmer <[hidden email]> wrote:
> No, problem - I'm very happy to do this (will call it getClassByName
> to avoid getClass clash), especially as my app needs this to work ;-)

That's a better name :-)

> On a related note, can anybody see any downside to me adding
> overloaded run() and getClassByName() methods that also take a
> ClassLoader parameter?

I can't think of a reason why not...
So I guess if you're needing that, others might do too.

> Unless I'm missing something I don't see how else we can use
> GroovyScriptEngine to execute scripts that have access to JARs and
> classes not on the classpath that the VM loaded Groovy from.
> Currently the parent class loader of the GroovyClassLoader used to
> parse the script is hardcoded to getClass().getClassLoader().
> Therefore if the groovy jar is in WEB-INF/lib of a webapp I don't
> think there is any way to have scripts use classes loaded from
> another classloader without this change.
>
> This should also be a trivial change from where I'm standing, and I
> can add it to Jira too.

In fact, there's already a JIRA issue for that, and a patch from
Azubuko Obele to fix that problem. So, if your own patch could
incorporate this patch, and your own changes, that would be great!

Thanks in advance, and welcome to our contributors community :-)
(same applies to Azubuko too of course)

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy