[grails-dev] GroovyClassLoader and Reloading Classes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[grails-dev] GroovyClassLoader and Reloading Classes

graemer
Hi Jochen,

I have a question regarding GroovyClassLoader: Does it support
reloading of classes in the same way as URLClassLoader does?

We are thinking about a solution to auto-reloading the webapp when
Grails classes are changed and this will be needed.

Cheers,
Graeme
Reply | Threaded
Open this post in threaded view
|

Re: [grails-dev] GroovyClassLoader and Reloading Classes

Jochen Theodorou
Graeme Rocher schrieb:

> Hi Jochen,
>
> I have a question regarding GroovyClassLoader: Does it support
> reloading of classes in the same way as URLClassLoader does?
>
> We are thinking about a solution to auto-reloading the webapp when
> Grails classes are changed and this will be needed.

URLClassLoader does support reloading of classes? That's kind of new to
me. Maybe you can explain what URLClassLoader does allow or what exactly
you want to do. A scenario describtion for example

bye blackdrag
Reply | Threaded
Open this post in threaded view
|

Re: [grails-dev] GroovyClassLoader and Reloading Classes

graemer
Essentially we want to be able save/reload when developing grails web
applications, to do this is going to prove complicated. There are a
number of ways.. the easiest (but unfortunately slowest) is to reload
the whole webapp.

An alternative may be to re-load certain resources that are possible
to reload.. for example Grails controllers which even this is not
without its complications. The idea is that we create a servlet filter
that detects changes and reloads the groovy class sets it within
grails and updates the spring application context

This could satisfy most cases, although it may cause problems with
cached instances (specifically if the grails controller class has
inner classes) etc. Neither steven nor I are class loading experts so
if you have any advice for us on an approach you think may work please
let us know.

As a first baby step reloading the whole webapp could be fine, but
eventually we would like to implement a much faster mechanism than
this

Graeme

On 10/11/05, Jochen Theodorou <[hidden email]> wrote:

> Graeme Rocher schrieb:
>
> > Hi Jochen,
> >
> > I have a question regarding GroovyClassLoader: Does it support
> > reloading of classes in the same way as URLClassLoader does?
> >
> > We are thinking about a solution to auto-reloading the webapp when
> > Grails classes are changed and this will be needed.
>
> URLClassLoader does support reloading of classes? That's kind of new to
> me. Maybe you can explain what URLClassLoader does allow or what exactly
> you want to do. A scenario describtion for example
>
> bye blackdrag
>
Reply | Threaded
Open this post in threaded view
|

Re: [grails-dev] GroovyClassLoader and Reloading Classes

Jochen Theodorou
Graeme Rocher schrieb:

first one gernal thing, that you may know... two classes are equal if
the name is equal and if the classloader is equal. this implies that you
can't define two classes of the same name in one classloader.

[...]
> An alternative may be to re-load certain resources that are possible
> to reload.. for example Grails controllers which even this is not
> without its complications.

you just have to use another classloader for this. I don't know how you
create the class, but if you use GroovyClassLoader#parseClass it fail,
because the current ClassLoader is used to define the class and multiple
calls of parseClass with the same source results in multiple attempts to
define a class in a classloader already containing a class of that name.
The solution is to use a new classloader for each re-load. Fro example a
very minimalistic version of GroovyClassLoader with an normal
GroovyClassLaoder as parent... it depends on the exact scenario (example
code ;)) you wish to have. Doable is much

[...]
> This could satisfy most cases, although it may cause problems with
> cached instances (specifically if the grails controller class has
> inner classes) etc. Neither steven nor I are class loading experts so
> if you have any advice for us on an approach you think may work please
> let us know.

As I said at the beginning you can't exchange a class. But you can use
multiple classloaders to load different version of the class. This may
lead to problems for exmaple when there is a line like

f.getClass()==Foo.class

this means all objects of an old class should be ready for the next
garbage collection. If you cache them it may still work, but be ready to
expererience strange problems like the one above.

> As a first baby step reloading the whole webapp could be fine, but
> eventually we would like to implement a much faster mechanism than
> this

just inform me what you need, we talk about it. For exmaple for groovysh
I wrote some classloader magic so that it is possible to define the same
class multiple times. Each definition is done in it's own classloader.
keeping an instance of an old class means, that the class is still
existing, but it does not disturb you... not as long as you don't try to
mix and use that instance in a contxt expecting the new instance...
again, f.getClass()==Foo.class is an example for something like that.
Appossible other case is method dispatching, but I'm not sure about this
one... never tried it. Or assignment to a variable may also fail because
of a type mismatch


bye  blackdrag