[opencms-dev] Velocity-good news

Alexander Kandzior alex at opencms.com
Mon Apr 1 22:25:54 CEST 2002


Werner, 

I think I get what you mean. Let me try to say it in my words with some
more details added:


You first would create an Oject "CmsVelocity" or something. This would
be extended from

org.apache.velocity.app.Velocity

You then would add a parameter "CmsObject" to all or most of the
"evaluate()" and perhaps "mergeTemplate()" and/or "invokeVelocimacro()"
methods. You also would perhaps rewrite all the methods that call the
"RuntimeSingleton" to work with an "CmsRuntimeSingleton". 

Next, you would have to create a class "CmsRuntimeSingleton" with
something like 

private static CmsRuntimeInstance ri = new CmsRuntimeInstance(); 

Around line 150. This CmsRuntimeInstance would be an extension of class
org.apache.velocity.runtime.RuntimeInstance, and you need to add an
CmsObject probably to this methods:

public Template getTemplate(String name)
public Template getTemplate(String name, String  encoding)
public ContentResource getContent(String name)
public ContentResource getContent( String name, String encoding )

(well actually you probably should just implement additional methods
having the CmsObject as additional parameter).

In class org.apache.velocity.runtime.resource.ResourceManagerImpl, you
need to add a CmsObject probably to this methods:

public Resource getResource(String resourceName, int resourceType,
String encoding )
protected Resource loadResource(String resourceName, int resourceType,
String encoding )

In "loadResource()", you could check if a resourceLoader is an
instanceOf(OpenCmsResourceLoader) around line 460, and if true use some
special calls in the loader not part of the standard velocity
ResourceLoader interface passing the CmsObject to this loader. 

And that's that. You would then probably end up with these classes:

CmsVelocity
CmsRuntimeSingleton
CmsRuntimeInstance
CmsResourceLoader


That _definitly_ looks promising. It would solve all the issues with the
synchronization and basically it seems like the "right" way of doing it.


Some additional thoughts: 

1) This is directly from
http://jakarta.apache.org/velocity/developer-guide.html:
"At no time should an application use the internal Runtime,
RuntimeConstants, RuntimeSingleton or RuntimeInstance classes in the
org.apache.velocity.runtime package, as these are intended for internal
use only and may change over time. As mentioned above, the classes you
should use are located in the org.apache.velocity.app package, and are
the Velocity and VelocityEngine classes. If anything is missing or
needed from those classes, do not hesitate to suggest changes - these
classes are intended for the application developer. "

So we should definitly try to commit theses changes back to the velocity
team, or the code might not longer work in the next release.

2) Velocity has some internal caching for the ResourceLoaders to prevent
to many file (or database) requests. This might cause problems in online
/ offline versions of a template. This must be taken care of as well. It
also would be benificial to be able to flush the cache of a Cms
ResouceLoader in case there is something published in OpenCms.

Regards,
Alex.





> -----Original Message-----
> From: owner-opencms-dev at www.opencms.com 
> [mailto:owner-opencms-dev at www.opencms.com] On Behalf Of Werner Punz
> Sent: Monday, April 01, 2002 4:44 PM
> To: opencms-dev at www.opencms.com
> Subject: [opencms-dev] Velocity-good news
> 
> 
> I've rechecked the latest velocity code and I think I've 
> found a better way to hook Velocity and OpenCMS together.
> 
> I analyzed the code from top to bottom from the 
> VelocityEngine class down to the ResourceLoaders and it seems 
> to me that the job is doable in a better but longer (lines of 
> code) way
> 
> The central resource loader caller is the Resource Manager 
> and this one is a factory patterned class as well. This code 
> generates the templates which are later merged, calls the 
> cache and calls the resource loaders indirectly (aka has 
> access to them). A new ResourceManager can be hooked into the 
> system similarily to the loaders.
> 
> All we have to do is to derive a new VelocityEngine based 
> class as well as a runtime instance class and a new 
> ResourceManager class and in all three classes the ability to 
> pass down CmsObject should be added and the whole thing can 
> be done without synchronization, as well as the Cms element 
> cache can be added to bypass the Velocity cache.This should 
> be a better base towards the integration of both systems, 
> since it is integrated in a cleaner way (aka usage of the Cms 
> cache is possible, cleaner way to access OpenCms files etc...)
> 
> 
> Ok this is a rewrite of parts of the Velocity engine (not 
> really since those classes can exist outside of the velocity 
> packages and no velocity packages are altered), but I think 
> it might be the cleanest way to integrate both parts and to 
> avoid the synchronization problems which are the main gripes 
> towards integration. I will start to work on a revised 
> version tomorrow in my spare time (since I have to finish 
> another project) and hope to have somehthing working soon.
> 
> Happy easter to you all.
> 
> (and no the posting was no april fools posting, I meant it seriously)
> 
> Werner Punz Labor_C
> http://www.labor-c.net
> 
> 
> 
> 




More information about the opencms-dev mailing list