You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Teemu Kanstrén <te...@mail.student.oulu.fi> on 2004/07/22 10:46:27 UTC
classpathresourceloader caching
I am using a ThreadContextResourceLoader that works much like the
ClassPathResourceLoader and I had problems getting my velocity
templates to reload under tomcat when they were modified.
Reloading the webapp or restarting tomcat made it work but was
a bit difficult for development.
There was a similar issue asked earlier here which didn't seem to produce
a good answer
http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg12795.html
So I thought I'd post this in case anyone else comes across this
Googleing around I found a solution on the tomcat-user list
http://archives.real-time.com/pipermail/tomcat-users/2003-June/113209.html
As I use my own ThreadContextResourceLoader that I based on some message
from this list, maybe this
http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg10497.html
This allowed me to modify the ThreadContextResourceLoader to this:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource(name);
result = new FileInputStream(new File(url.getFile()));
Maybe this helps someone. Please post any comments if you see some
problems with this etc.
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: classpathresourceloader caching
Posted by Will Glass-Husain <wg...@forio.com>.
Hi,
Apparently another user came to this same conclusion last year and submitted
a patch (which has not been commited).
I updated Bugzilla with a note about this discussion.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=22419
To me, the problem with the caching is really the key reason to get this
fixed.
Anyway, thanks, Teemu, for tracking this down.
WILL
----- Original Message -----
From: "Teemu Kanstrén" <te...@mail.student.oulu.fi>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Thursday, July 22, 2004 10:56 PM
Subject: Re: classpathresourceloader caching
>
> I think your problem was exactly the same as mine. I was also loading
> templates from the classpath and caching was working exactly as it did for
> you. I had to restart tomcat for changes in templates to be visible. The
> only difference with the ThreadContextResourceLoader I was using was that
> I did
>
> ClassLoader loader = Thread.currentThread().getContextClassLoader();
>
> whereas ClasspathResourceLoader does
>
> ClassLoader classLoader = this.getClass().getClassLoader();
>
> other than that they are pretty much the same. This only allowed me to
> load the templates also from jars in WEB-INF/lib, not only
> WEB-INF/classes.
>
> I am not an expert on this issue so I can only guess/speculate why the
> caching didn't work. I got it working now though so that is enough for me.
>
> I tried looking up information on classloaders and caching and pretty much
> only found that you can't unload a class loaded by a classloader. No
> mention about data loaded with getResourceAsStream(). So my guess is that
> it also caches that data and you can't unload it. But if you do i like
> this
>
> URL url = loader.getResource(name);
> result = url.openStream();
>
> I guess the URL given by the classloader only points to the resource. When
> you open the stream it is then loaded by the standard URL class which
doesn't
> cache it. But that is only guessing, you shouldn't trust me on that.. :)
>
> Here is the resourceloader I use now if you want to test it.
>
> public class ThreadContextResourceLoader extends ClasspathResourceLoader {
> /**
> * Get an InputStream so that the Runtime can build a
> * template with it.
> *
> * @param name name of template to get
> * @return InputStream containing the template
> * @throws org.apache.velocity.exception.ResourceNotFoundException
> * if template not found
> * in classpath.
> */
> public synchronized InputStream getResourceStream(String name) throws
> ResourceNotFoundException {
> InputStream result = null;
>
> if (name == null || name.length() == 0) {
> throw new ResourceNotFoundException("No template name provided");
> }
>
> try {
> if (name.startsWith("/")) {
> name = name.substring(1);
> }
> ClassLoader loader = Thread.currentThread().getContextClassLoader();
>
> URL url = loader.getResource(name);
> result = url.openStream();
>
> } catch (Exception e) {
> /*
> * log and convert to a general Velocity ResourceNotFoundException
> */
> throw new ResourceNotFoundException(e.getMessage());
> }
> return result;
> }
> }
>
> Feel free to use and modify all you want..
>
>
> On Thu, 22 Jul 2004, Will Glass-Husain wrote:
>
> > Hmmm...
> >
> > That's interesting. Do you think that could be the reason I found
caching
> > not to work when I used the classpathresourceloader? It loaded the
files
> > correctly, but didn't display any new versions (without restarting
Tomcat).
> >
> > I don't have much time to play with this now (I ended up
FileResourceLoader,
> > which was quick to implement) but I'll look again at this before filing
a
> > bug report (which is on my to-do list).
> >
> > Best, WILL
> >
> >
> > ----- Original Message -----
> > From: "Teemu Kanstrén" <te...@mail.student.oulu.fi>
> > To: "Velocity Users List" <ve...@jakarta.apache.org>
> > Sent: Thursday, July 22, 2004 3:02 AM
> > Subject: Re: classpathresourceloader caching
> >
> >
> > >
> > > OK and here is already my first correction
> > >
> > > had to replace
> > >
> > > result = new FileInputStream(new File(url.getFile()));
> > >
> > > with
> > >
> > > result = url.openStream();
> > >
> > > because it was loading stuff from the classpath (classes dir)
> > > but not from a jar file (vm.jar). Anyway, let's see how long
> > > until something else comes up.. :)
> > >
> > > On Thu, 22 Jul 2004, Teemu Kanstrén wrote:
> > >
> > > >
> > > > I am using a ThreadContextResourceLoader that works much like the
> > > > ClassPathResourceLoader and I had problems getting my velocity
> > > > templates to reload under tomcat when they were modified.
> > > > Reloading the webapp or restarting tomcat made it work but was
> > > > a bit difficult for development.
> > > >
> > > > There was a similar issue asked earlier here which didn't seem to
> > produce
> > > > a good answer
> > > >
> > > >
> >
http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg12795.html
> > > >
> > > > So I thought I'd post this in case anyone else comes across this
> > > >
> > > > Googleing around I found a solution on the tomcat-user list
> > > >
> > > >
> >
http://archives.real-time.com/pipermail/tomcat-users/2003-June/113209.html
> > > >
> > > > As I use my own ThreadContextResourceLoader that I based on some
message
> > > > from this list, maybe this
> > > >
> > > >
> >
http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg10497.html
> > > >
> > > > This allowed me to modify the ThreadContextResourceLoader to this:
> > > >
> > > > ClassLoader loader =
> > Thread.currentThread().getContextClassLoader();
> > > >
> > > > URL url = loader.getResource(name);
> > > >
> > > > result = new FileInputStream(new File(url.getFile()));
> > > >
> > > > Maybe this helps someone. Please post any comments if you see some
> > > > problems with this etc.
> > > >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: classpathresourceloader caching
Posted by Teemu Kanstrén <te...@mail.student.oulu.fi>.
I think your problem was exactly the same as mine. I was also loading
templates from the classpath and caching was working exactly as it did for
you. I had to restart tomcat for changes in templates to be visible. The
only difference with the ThreadContextResourceLoader I was using was that
I did
ClassLoader loader = Thread.currentThread().getContextClassLoader();
whereas ClasspathResourceLoader does
ClassLoader classLoader = this.getClass().getClassLoader();
other than that they are pretty much the same. This only allowed me to
load the templates also from jars in WEB-INF/lib, not only
WEB-INF/classes.
I am not an expert on this issue so I can only guess/speculate why the
caching didn't work. I got it working now though so that is enough for me.
I tried looking up information on classloaders and caching and pretty much
only found that you can't unload a class loaded by a classloader. No
mention about data loaded with getResourceAsStream(). So my guess is that
it also caches that data and you can't unload it. But if you do i like
this
URL url = loader.getResource(name);
result = url.openStream();
I guess the URL given by the classloader only points to the resource. When
you open the stream it is then loaded by the standard URL class which doesn't
cache it. But that is only guessing, you shouldn't trust me on that.. :)
Here is the resourceloader I use now if you want to test it.
public class ThreadContextResourceLoader extends ClasspathResourceLoader {
/**
* Get an InputStream so that the Runtime can build a
* template with it.
*
* @param name name of template to get
* @return InputStream containing the template
* @throws org.apache.velocity.exception.ResourceNotFoundException
* if template not found
* in classpath.
*/
public synchronized InputStream getResourceStream(String name) throws
ResourceNotFoundException {
InputStream result = null;
if (name == null || name.length() == 0) {
throw new ResourceNotFoundException("No template name provided");
}
try {
if (name.startsWith("/")) {
name = name.substring(1);
}
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource(name);
result = url.openStream();
} catch (Exception e) {
/*
* log and convert to a general Velocity ResourceNotFoundException
*/
throw new ResourceNotFoundException(e.getMessage());
}
return result;
}
}
Feel free to use and modify all you want..
On Thu, 22 Jul 2004, Will Glass-Husain wrote:
> Hmmm...
>
> That's interesting. Do you think that could be the reason I found caching
> not to work when I used the classpathresourceloader? It loaded the files
> correctly, but didn't display any new versions (without restarting Tomcat).
>
> I don't have much time to play with this now (I ended up FileResourceLoader,
> which was quick to implement) but I'll look again at this before filing a
> bug report (which is on my to-do list).
>
> Best, WILL
>
>
> ----- Original Message -----
> From: "Teemu Kanstrén" <te...@mail.student.oulu.fi>
> To: "Velocity Users List" <ve...@jakarta.apache.org>
> Sent: Thursday, July 22, 2004 3:02 AM
> Subject: Re: classpathresourceloader caching
>
>
> >
> > OK and here is already my first correction
> >
> > had to replace
> >
> > result = new FileInputStream(new File(url.getFile()));
> >
> > with
> >
> > result = url.openStream();
> >
> > because it was loading stuff from the classpath (classes dir)
> > but not from a jar file (vm.jar). Anyway, let's see how long
> > until something else comes up.. :)
> >
> > On Thu, 22 Jul 2004, Teemu Kanstrén wrote:
> >
> > >
> > > I am using a ThreadContextResourceLoader that works much like the
> > > ClassPathResourceLoader and I had problems getting my velocity
> > > templates to reload under tomcat when they were modified.
> > > Reloading the webapp or restarting tomcat made it work but was
> > > a bit difficult for development.
> > >
> > > There was a similar issue asked earlier here which didn't seem to
> produce
> > > a good answer
> > >
> > >
> http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg12795.html
> > >
> > > So I thought I'd post this in case anyone else comes across this
> > >
> > > Googleing around I found a solution on the tomcat-user list
> > >
> > >
> http://archives.real-time.com/pipermail/tomcat-users/2003-June/113209.html
> > >
> > > As I use my own ThreadContextResourceLoader that I based on some message
> > > from this list, maybe this
> > >
> > >
> http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg10497.html
> > >
> > > This allowed me to modify the ThreadContextResourceLoader to this:
> > >
> > > ClassLoader loader =
> Thread.currentThread().getContextClassLoader();
> > >
> > > URL url = loader.getResource(name);
> > >
> > > result = new FileInputStream(new File(url.getFile()));
> > >
> > > Maybe this helps someone. Please post any comments if you see some
> > > problems with this etc.
> > >
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: classpathresourceloader caching
Posted by Will Glass-Husain <wg...@forio.com>.
Hmmm...
That's interesting. Do you think that could be the reason I found caching
not to work when I used the classpathresourceloader? It loaded the files
correctly, but didn't display any new versions (without restarting Tomcat).
I don't have much time to play with this now (I ended up FileResourceLoader,
which was quick to implement) but I'll look again at this before filing a
bug report (which is on my to-do list).
Best, WILL
----- Original Message -----
From: "Teemu Kanstrén" <te...@mail.student.oulu.fi>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Thursday, July 22, 2004 3:02 AM
Subject: Re: classpathresourceloader caching
>
> OK and here is already my first correction
>
> had to replace
>
> result = new FileInputStream(new File(url.getFile()));
>
> with
>
> result = url.openStream();
>
> because it was loading stuff from the classpath (classes dir)
> but not from a jar file (vm.jar). Anyway, let's see how long
> until something else comes up.. :)
>
> On Thu, 22 Jul 2004, Teemu Kanstrén wrote:
>
> >
> > I am using a ThreadContextResourceLoader that works much like the
> > ClassPathResourceLoader and I had problems getting my velocity
> > templates to reload under tomcat when they were modified.
> > Reloading the webapp or restarting tomcat made it work but was
> > a bit difficult for development.
> >
> > There was a similar issue asked earlier here which didn't seem to
produce
> > a good answer
> >
> >
http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg12795.html
> >
> > So I thought I'd post this in case anyone else comes across this
> >
> > Googleing around I found a solution on the tomcat-user list
> >
> >
http://archives.real-time.com/pipermail/tomcat-users/2003-June/113209.html
> >
> > As I use my own ThreadContextResourceLoader that I based on some message
> > from this list, maybe this
> >
> >
http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg10497.html
> >
> > This allowed me to modify the ThreadContextResourceLoader to this:
> >
> > ClassLoader loader =
Thread.currentThread().getContextClassLoader();
> >
> > URL url = loader.getResource(name);
> >
> > result = new FileInputStream(new File(url.getFile()));
> >
> > Maybe this helps someone. Please post any comments if you see some
> > problems with this etc.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: classpathresourceloader caching
Posted by Teemu Kanstrén <te...@mail.student.oulu.fi>.
OK and here is already my first correction
had to replace
result = new FileInputStream(new File(url.getFile()));
with
result = url.openStream();
because it was loading stuff from the classpath (classes dir)
but not from a jar file (vm.jar). Anyway, let's see how long
until something else comes up.. :)
On Thu, 22 Jul 2004, Teemu Kanstrén wrote:
>
> I am using a ThreadContextResourceLoader that works much like the
> ClassPathResourceLoader and I had problems getting my velocity
> templates to reload under tomcat when they were modified.
> Reloading the webapp or restarting tomcat made it work but was
> a bit difficult for development.
>
> There was a similar issue asked earlier here which didn't seem to produce
> a good answer
>
> http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg12795.html
>
> So I thought I'd post this in case anyone else comes across this
>
> Googleing around I found a solution on the tomcat-user list
>
> http://archives.real-time.com/pipermail/tomcat-users/2003-June/113209.html
>
> As I use my own ThreadContextResourceLoader that I based on some message
> from this list, maybe this
>
> http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg10497.html
>
> This allowed me to modify the ThreadContextResourceLoader to this:
>
> ClassLoader loader = Thread.currentThread().getContextClassLoader();
>
> URL url = loader.getResource(name);
>
> result = new FileInputStream(new File(url.getFile()));
>
> Maybe this helps someone. Please post any comments if you see some
> problems with this etc.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org