Thanks Nathan, indeed a good catch :)
I overlooked that and assumed that both singleton and runtime are the
same instance.

On 7/13/07, Will Glass-Husain <[EMAIL PROTECTED]> wrote:
Good catch Nathan.

Just to clarify this.  (it may not be obvious).  Velocity offers two
parallel API's, the singleton and the runtime.

Generally, you want to initialize VelocityEngine, and do all calls against
an *instance*  of that engine.  This allows you to run Velocity with
different set of properties simultaneously (e.g. one for email and one for
web).

You can also call static methods from the Velocity *class*. This is entirely
independent from the VelocityEngine instance, though they work the same way.

WILL


On 7/12/07, Nathan Bubna <[EMAIL PROTECTED]> wrote:
>
> On 7/12/07, Joe Kramer <[EMAIL PROTECTED]> wrote:
> > On 7/13/07, Nathan Bubna <[EMAIL PROTECTED]> wrote:
> > > On 7/12/07, Joe Kramer <[EMAIL PROTECTED]> wrote:
> > > > Hello,
> > > > I've spent good 10 hours trying to load template in webapp.
> > > > First I tried
> > > >  resource.loader= file
> > > > and putting template into any posible directory, WEB-INF, classes,
> > > > lib, web app root.
> > > > Also tried to use
> > > >  resource.loader=class
> > > > Putting template in all possible places. Including create
> template.jar
> > > > and putting it in /lib.
> > > > It's always "Unable to find resource 'template.vm'"
> > > >
> > > > So I nothing helps using out-of-the-box methods.
> > > > I wrote class ResourceLoader extends
> > > > org.apache.velocity.runtime.resource.loader.ResourceLoader
> > > >
> > > > and put resource.loader=custom
> > > >
> > > > I added Log message into getResourceStream(), the method to be
> called
> > > > when template is requested.
> > > > Java code:
> > > > VelocityEngine ve = new VelocityEngine();
> > > > ve.init();
> > > > Template template = Velocity.getTemplate("template.vm");
> > >
> > > This is all wrong.   You create a VelocityEngine, then initialize it
> > > without your custom properties, then try to get a template from the
> > > Velocity singleton!!  Before we can worry about the resource loaders,
> > > we need to make sure you are using Velocity properly period.  Change
> > > the code above to:
> > >
> > > VelocityEngine ve = new VelocityEngine();
> > > ve.init("path/to/your/properties/file");
> > > Template template = ve.getTemplate("template.vm");
> >
> > Sorry I omitted the Properties form init.
> > my code is actually:
> > VelocityEngine ve = new VelocityEngine();
> > Properties p = Servlet.loadProperties("velocity.properties");
> > ve.init(p);
> >
> > initialization using ve.init("velocity.properties");
> > doesn't work for me because it cannot be found (same as template,
> > anywhere I put it).
>
> then you're not giving it the correct, complete path. :)  of course,
> in a webapp, that is not something you should hardcode, and if the
> webapp is deployed as an unexploded WAR, then it won't ever work.  so
> you are right to load the Properties instance yourself and pass that
> to the engine.
>
> so, what about the fact that you are trying to get the template from
> the Velocity singleton?  you need to call ve.getTemplate() instead of
> Velocity.getTemplate().
>
> > I have to load properties using classLoader and pass then to init().
>
> sounds great.
>
> >
> > >
> > > oh, and in a web application, you should really only be using the
> > > ClasspathResourceLoader or the WebappLoader from VelocityTools.   And
> > > to shorten your test cycle, did you know you can use multiple resource
> > > loaders?  Try:
> > >
> > > resource.loader = webapp, classpath
> > > webapp.resource.loader.class =
> > > org.apache.velocity.tools.view.servlet.WebappLoader
> > > classpath.resource.loader.class =
> > > org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
> > >
> > > then, you can put your template.vm file in the root folder of the
> > > webapp if you want the WebappLoader to find it and/or in the
> > > /WEB-INF/classes/ directory (as Will said) if you want the
> > > ClasspathResourceLoader to find it.  Note that with the properties
> > > above, the resource manager will always first try to find a template
> > > via the webapp loader, and only fall back to checking the classpath
> > > loader if the webapp one can't find it.
> > >
> >
> > I tried to use both of them, and using resource.loader = webapp,
> classpath
> > my template is copied into every possible folder in webapp. and still
> not found.
>
> i would still love to see some actual log files for this stuff.
>
> > > > What do I get in log:
> > > >  INFO   - getResourceStream() load VM_global_library.vm
> > > > ERROR  - Unable to find resource 'template.vm'
> > > >
> > > > It looks for VM_global_library.vm! Why???
> > >
> > > Will has said this, but i'll repeat anyway: ignore the
> > > VM_global_library.vm thing.  It's a default setting that won't hurt
> > > anything.
> > >
> > > > In velocity.properties it's even commented out:
> > > > # velocimacro.library = VM_global_library.vm
> > > > And VM_global_library.vm is present in path just in case.
> > > > I don't get any messages that VM_global_library.vm not found, I gt
> > > > message that requested template not found but request to
> > > > ResourceLoader wasn't made for that template.
> > >
> > > in that case, i'm not entirely convinced that your custom properties
> > > have been loaded.  especially since your code sample shows you
> > > init()'ing the engine without your properties and then asking the
> > > singleton (which is an entirely separate engine underneath) for the
> > > template.  What do your log files say for Velocity startup?  We need
> > > to make sure that your properties are being loaded.
> > >
> >
> > Properties are loaded because my Custom ResourceLoader is picked up
> > and being called.
> > But instead of my template.vm, my ResourceLoader is called by Velocity
> > for VM_global_library.vm and Velocity returns that template.vm not
> > found.
>
> i think that's because the VelocityEngine you init() with your
> properties is looking for the VM_global_library.vm when init() is
> called, and then the Velocity singleton (which does not get init()'ed
> with your properties (at least not in any code i've seen you post),
> but from which you are trying to load your "template.vm" file is
> unable to find it because it uses the FileResourceLoader by default
> which is great in standalone apps but not useful in webapps.
>
> >
> > This looks very much like a bug.
>
> in Velocity or in your code?  ;-)
>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


--
Forio Business Simulations

Will Glass-Husain
[EMAIL PROTECTED]
www.forio.com


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to