velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Raymond Auge <ra...@liferay.com>
Subject Re: velocimarco.library property and StringResourceLoader
Date Thu, 08 Jan 2009 14:39:28 GMT
Hey Steve,

I just re-purposed our entire Velocity usage to use StringResourceLoader
where before we were calling "evaluate()" ALL THE TIME... it was killer
on resources as you might imagine.

So, this is the core code, it's very simple. So, we have a wrapper class
that is a Singleton that setups up a non-singleton VelocityEngine
instance (so that other apps can use their own version, all good). This
wrapper handles all the setup and configuration of Velocity. It also
provides these methods:


public boolean mergeTemplate(
		String templateId, VelocityContext velocityContext, Writer writer)
	throws SystemException, IOException {

	return mergeTemplate(templateId, null, velocityContext, writer);
}

public boolean mergeTemplate(
		String templateId, String template, VelocityContext velocityContext,
		Writer writer)
	throws SystemException, IOException {

	try {
		if (Validator.isNotNull(template) && !resourceExists(templateId)) {
			StringResourceLoader.getRepository().putStringResource(
				templateId, template);

			if (_log.isDebugEnabled()) {
				_log.debug(
					"Added " + templateId + " to the Velocity repository");
			}
		}

		VelocityContextImpl velocityContextImpl =
			(VelocityContextImpl)velocityContext;

		return _velocityEngine.mergeTemplate(
			templateId, StringPool.UTF8,
			velocityContextImpl.getWrappedVelocityContext(), writer);
	}
	catch (IOException ioe) {
		throw ioe;
	}
	catch (Exception e) {
		throw new SystemException(e);
	}
}


So, as you can see, where we would have normally passed a string to
velocity's evaluate method, now we call


VelocityEngineUtil.mergeTemplate(scriptId, script, velocityContext, output);


and if the script is not in the repo, then add it. If it already got
added to the repo, then it will be found by name (scriptId) and
retrieved as a ready Template, which means you saved all the AST
re-parsing overhead.



I do have a question of my own. How many strings in the repository is
too many? Or an overall size. Can they have a time-to-live?


Ray




On Thu, 2009-01-08 at 07:47 -0600, Steve Cohen wrote:

> I continue to find out new things (new for me) about Velocity. 
> 
> I am trying to wrap my head around the StringResourceLoader concept, 
> which sounds like something I've been looking for.  Up to now my usage 
> of Velocity (for this AOL-Instant Messager-based app I've talked about 
> before) has been to embed each bit of text I want to display in a little 
> xhtml file loaded by Velocity including merging dynamic text into the file.
> 
> It works great, but it has felt a bit like overkill sometimes, 
> especially with the smaller bits of text.  The idea of string resource 
> loading was floating around the back of mind when I saw this post.
> 
> But I find I'm not quite getting the concept of StringResourceLoader as 
> it is laid out in the javadocs.  The documentation there is all about 
> dynamically loading strings INTO the repository.  This is not at all 
> what I am after.  I am after statically loading a bunch of string 
> resources as opposed to file resources (i.e., multiple string resources 
> in a file) somewhere where Velocity can find them for later retrieval by 
> the application.
> 
> Is there some sample code or other documentation demonstrating in more 
> detail the uses of the StringResourceLoader?
> 
> 
> Nathan Bubna wrote:
> > On Tue, Jan 6, 2009 at 3:54 PM, Caleb Jones <calebjones@gmail.com> wrote:
> >   
> >> Following the Javadoc for StringResourceLoader I have my velocity.properties
> >> setup as follows:
> >>
> >> -------------------------------
> >> template.encoding = UTF8
> >> input.encoding=UTF-8
> >> output.encoding=UTF-8
> >> velocimacro.library=VM_global_library.vm
> >> resource.loader = string
> >> string.resource.loader.description = Velocity StringResource loader
> >> string.resource.loader.class =
> >> org.apache.velocity.runtime.resource.loader.StringResourceLoader
> >>     
> >
> > looks good.
> >
> >   
> >> string.resource.loader.repository.class =
> >> org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl
> >>     
> >
> > you don't need this property unless you are using your own repository impl.
> >
> >   
> >> --------------------------------
> >>
> >> The problem I have is that Velocity cannot find my VM_global_library.vm.  I
> >> can see why the StringResourceLoader wouldn't know what to do with
> >> "VM_global_library.vm", but I as per the velocimacro.library docs at
> >> http://velocity.apache.org/engine/devel/developer-guide.html:
> >>
> >> ----------------------------------
> >> velocimacro.library = VM_global_library.vm
> >> Multi-valued key. Will accept CSV for value. Filename(s) of Velocimacro
> >> library to be loaded when the Velocity Runtime engine starts. These
> >> Velocimacros are accessable to all templates. The file is assumed to be
> >> relative to the root of the file loader resource path.
> >> -----------------------------------
> >>
> >> ...it doesn't mention how or if this can be configured when using a
> >> StringResourceLoader.
> >>
> >> What is the best way to load global macro files when using a
> >> StringResourceLoader?
> >>     
> >
> > Depends on if you want to load it from the file system or as a String.
> >  I haven't actually tried the latter, but i think you simply need to
> > put it into the repository before you init() Velocity.   So, somewhere
> > before Velocity is initialized you should do:
> >
> > StringResourceLoader.getRepository().putStringResource("VM_global_library.vm",
> > theMacrosAsAString);
> >
> > for the former (which actually sounds more like what you want), you
> > simply need to have another resource loader configured.  Note that the
> > resource.loader property takes a comma separated list of names of
> > loaders that should be initialized and used.  Velocity will check the
> > resource loaders in the order listed until it finds the requested
> > file.  So, you might do:
> >
> > velocimacro.library=VM_global_library.vm
> > resource.loader = string,file
> > string.resource.loader.class =
> > org.apache.velocity.runtime.resource.loader.StringResourceLoader
> > file.resource.loader.class =
> > org.apache.velocity.runtime.resource.loader.FileResourceLoader
> >
> > and you can, of course, add a file.resource.loader.path property or
> > use a different resource loader(s) entirely.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> > For additional commands, e-mail: user-help@velocity.apache.org
> >
> >
> >
> >   
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 
> 

--
Raymond Augé
Senior Software Engineer
Liferay, Inc.
Enterprise. Open Source. For Life.


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message