You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Anthony Eden <ae...@signaturedomains.com> on 2003/02/27 17:18:55 UTC

Passing an Object to a custom ResourceLoader

Is it possible to pass an object to a ResourceLoader as part of the 
ResourceLoader's initialization process?

I have read through the docs and checked the archives to no avail.  I 
have also tried to actually implement this by putting the object in the 
Properties object, which is passed to VelocityEngine.init(), with the 
key jpublish.resource.loader.siteContext, but I am receiving the 
following error:

java.lang.NullPointerException
        at java.util.Hashtable.put(Hashtable.java:386)
        at 
org.apache.commons.collections.ExtendedProperties.addPropertyDirect(E
xtendedProperties.java:683)
        at 
org.apache.commons.collections.ExtendedProperties.addProperty(Extende
dProperties.java:657)
        at 
org.apache.commons.collections.ExtendedProperties.setProperty(Extende
dProperties.java:746)
        at 
org.apache.commons.collections.ExtendedProperties.convertProperties(E
xtendedProperties.java:1872)
        at 
org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java
:440)
        at 
org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:148)
        at 
org.jpublish.view.velocity.VelocityViewRenderer.init(VelocityViewRend
erer.java:146)
        at org.jpublish.SiteContext.load(SiteContext.java:1050)
        at 
com.anthonyeden.lib.resource.FileResourceLoader.loadResource(Unknown
Source)
        at 
com.anthonyeden.lib.resource.FileResourceLoader.loadResource(Unknown
Source)
        at org.jpublish.SiteContext.<init>(SiteContext.java:189)
        at 
org.jpublish.servlet.JPublishServlet.init(JPublishServlet.java:137)
        at 
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.
java:934)
        at 
org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:82
1)
        at 
org.apache.catalina.core.StandardContext.loadOnStartup(StandardContex
t.java:3420)
        at 
org.apache.catalina.core.StandardContext.start(StandardContext.java:3
608)
        at 
org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:821)
        at 
org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:80
7)
        at 
org.apache.catalina.core.StandardHost.addChild(StandardHost.java:579)

        at 
org.apache.catalina.core.StandardHostDeployer.install(StandardHostDep
loyer.java:257)
        at 
org.apache.catalina.core.StandardHost.install(StandardHost.java:772)
        at 
org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.j
ava:569)
        at 
org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:411
)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:879)
        at 
org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
:368)
        at 
org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:166)
        at 
org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1196)

        at 
org.apache.catalina.core.StandardHost.start(StandardHost.java:738)
        at 
org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1188)

        at 
org.apache.catalina.core.StandardEngine.start(StandardEngine.java:347
)
        at 
org.apache.catalina.core.StandardService.start(StandardService.java:4
97)
        at 
org.apache.catalina.core.StandardServer.start(StandardServer.java:218
9)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:512)
        at org.apache.catalina.startup.Catalina.execute(Catalina.java:400)
        at org.apache.catalina.startup.Catalina.process(Catalina.java:180)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:203)


Sincerely,
Anthony Eden


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Passing an Object to a custom ResourceLoader

Posted by Anthony Eden <ae...@signaturedomains.com>.
t

Bill Burton wrote:

> I poked around a bit and it looks like you could do something like 
> this so you don't have to set each property individually with 
> setProperty:
>     VelocityEngine velocityEngine = new VelocityEngine();
>     ExtendedProperties eprops = new ExtendedProperties();
>     // if you already have a Properties object do this
>     eprops.putAll(props);
>     // Now set the property with your object instance
>     eprops.setProperty("name", object);
>     ...
>     velocityEngine.setExtendedProperties(eprops);
>     velocityEngine.init();
>
> Hope this helps.  If this works, a patch to the developer docs would 
> be helpful. 

Thanks for your help, Bill, it worked like a charm.

I would be happy to try and send in a patch for the docs - should I edit 
the docs file or the xdocs file?  Once I make the patch should I just 
send it to the mailing list?

-Anthony


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Passing an Object to a custom ResourceLoader

Posted by Bill Burton <bi...@progress.com>.
Hello,

Anthony Eden wrote:
> Is it possible to pass an object to a ResourceLoader as part of the 
> ResourceLoader's initialization process?

It should be.

> I have read through the docs and checked the archives to no avail.  I 
> have also tried to actually implement this by putting the object in the 
> Properties object, which is passed to VelocityEngine.init(), with the 
> key jpublish.resource.loader.siteContext, but I am receiving the 
> following error:

I found that when passing an object instance for a logger for a Ant 
logging adapter, using a Properties object doesn't work as I got the 
same or a similar error.  Instead, calling VelocityEngine.setProperty 
for each property worked.  Unfortunately, one can't mix passing a 
Properties object with setProperty because the Properties object 
overwrites any setProperty calls done previously.

The real problem is a Properties object can only contain Strings as 
values unless one uses the superclass method get instead of getProperty. 
  In o.a.v.runtime.RuntimeInstance.init(Properties) is the line:
    ExtendedProperties.convertProperties(p)
The commons.ExtendedProperties class could be made smart enough to deal 
with this so an NPE isn't thrown.

There should be a way to pass a Properties or HashMap object using a 
VelocityEngine.setProperties(HashMap) method.  Then you can call the 
setProperty method to override or add to those settings before calling 
init().  However, an ExtendedProperties object can be set prior to init.

I poked around a bit and it looks like you could do something like this 
so you don't have to set each property individually with setProperty:
     VelocityEngine velocityEngine = new VelocityEngine();
     ExtendedProperties eprops = new ExtendedProperties();
     // if you already have a Properties object do this
     eprops.putAll(props);
     // Now set the property with your object instance
     eprops.setProperty("name", object);
     ...
     velocityEngine.setExtendedProperties(eprops);
     velocityEngine.init();

Hope this helps.  If this works, a patch to the developer docs would be 
helpful.

-Bill

> java.lang.NullPointerException
>        at java.util.Hashtable.put(Hashtable.java:386)
>        at 
> org.apache.commons.collections.ExtendedProperties.addPropertyDirect(E
> xtendedProperties.java:683)
>        at 
> org.apache.commons.collections.ExtendedProperties.addProperty(Extende
> dProperties.java:657)
>        at 
> org.apache.commons.collections.ExtendedProperties.setProperty(Extende
> dProperties.java:746)
>        at 
> org.apache.commons.collections.ExtendedProperties.convertProperties(E
> xtendedProperties.java:1872)
>        at 
> org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java
> :440)
>        at 
> org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:148)
>        at 
> org.jpublish.view.velocity.VelocityViewRenderer.init(VelocityViewRend
> erer.java:146)
>        at org.jpublish.SiteContext.load(SiteContext.java:1050)
>        at 
> com.anthonyeden.lib.resource.FileResourceLoader.loadResource(Unknown
> Source)
>        at 
> com.anthonyeden.lib.resource.FileResourceLoader.loadResource(Unknown
> Source)
>        at org.jpublish.SiteContext.<init>(SiteContext.java:189)
>        at 
> org.jpublish.servlet.JPublishServlet.init(JPublishServlet.java:137)
>        at 
> org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.
> java:934)
>        at 
> org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:82
> 1)
>        at 
> org.apache.catalina.core.StandardContext.loadOnStartup(StandardContex
> t.java:3420)
>        at 
> org.apache.catalina.core.StandardContext.start(StandardContext.java:3
> 608)
>        at 
> org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
> .java:821)
>        at 
> org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:80
> 7)
>        at 
> org.apache.catalina.core.StandardHost.addChild(StandardHost.java:579)
> 
>        at 
> org.apache.catalina.core.StandardHostDeployer.install(StandardHostDep
> loyer.java:257)
>        at 
> org.apache.catalina.core.StandardHost.install(StandardHost.java:772)
>        at 
> org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.j
> ava:569)
>        at 
> org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:411
> )
>        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:879)
>        at 
> org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
> :368)
>        at 
> org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
> eSupport.java:166)
>        at 
> org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1196)
> 
>        at 
> org.apache.catalina.core.StandardHost.start(StandardHost.java:738)
>        at 
> org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1188)
> 
>        at 
> org.apache.catalina.core.StandardEngine.start(StandardEngine.java:347
> )
>        at 
> org.apache.catalina.core.StandardService.start(StandardService.java:4
> 97)
>        at 
> org.apache.catalina.core.StandardServer.start(StandardServer.java:218
> 9)
>        at org.apache.catalina.startup.Catalina.start(Catalina.java:512)
>        at org.apache.catalina.startup.Catalina.execute(Catalina.java:400)
>        at org.apache.catalina.startup.Catalina.process(Catalina.java:180)
>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>        at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
> java:39)
>        at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
> sorImpl.java:25)
>        at java.lang.reflect.Method.invoke(Method.java:324)
>        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:203)
> 
> 
> Sincerely,
> Anthony Eden
> 
> 
> ---------------------------------------------------------------------
> 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