You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Kevin Baynes <kb...@seagullsw.com> on 2002/08/20 18:57:56 UTC

Singleton vs Separate Instance

I have a web application with a servlet that uses the Singleton Velocity
pattern.

I have a second web app that uses velocity from a servlet, using Singleton
Velociy. When I have both web apps in the same servlet container, it appears
that the first web app (using Singleton) is "winning" and setting
properties, which makes the second web app unable to find templates in it's
/WEB-INF/classes directory, so it gets ResourceNotFoundException. If I
remove the first app then restart container, second app runs fine. I'm using
Tomcat 4.0.4-b2.

I have some questions about Singleton vs Separate Instance.

The Velocity documentation states:

"This is the legacy pattern, where there is only one instance of the
Velocity engine in the JVM (or web application, depending) that is shared by
all. This is very convenient as it allows localized configuration and
sharing of resources. For example, this is a very appropriate model for use
in a Servlet 2.2+ compliant web application as each web application can have
it's own instance of Velocity, allowing that web application's servlet to
share resources like templates, a logger, etc. The singleton is accessable
via the org.apache.velocity.app.Velocity class, and and example of use : "

The above statement is unclear to me because it says that "there is only one
instance of the Velocity engine in the JVM", then it adds "(or web
application, depending)". Depending on what?

The third sentence states that each web application can have it's own
instance of Velocity. How is this accomplished?

The Velocity doc then explains about creating a "Separate Instance" of the
VelocityEngine. This seems to allow each instantiating class to have
separate instances of Velocity. Would using "Separate Instance" protect me
from the above case where another web app in the servlet container is using
Singleton Velocity?

Thanks!

~Kevin Baynes


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: how do I class cast in the template?

Posted by Peter Romianowski <me...@gmx.de>.
Hi,

AFAIK you don't have to! Velocity calls the functions using the
Reflection-API. If
the object provides the function it is called.

Simply try it, it's cool!

Peter

-----Original Message-----
From: peter neubauer [mailto:peter_neubauer2001@yahoo.se] 
Sent: Tuesday, August 20, 2002 9:00 PM
To: Velocity Users List
Subject: how do I class cast in the template?


Hi,
I want to cast $data.user to my special user implementation. How do I do
that in the template in order to access special functions?

Thanks

/peter

_____________________________________________________
Följ VM på nära håll på Yahoo!s officielle VM-sajt www.yahoo.se/vm2002
Håll dig ajour med nyheter och resultat, med vinnare och förlorare...

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: how do I class cast in the template?

Posted by "Geir Magnusson Jr." <ge...@adeptra.com>.
On 8/20/02 2:59 PM, "peter neubauer" <pe...@yahoo.se> wrote:

> Hi,
> I want to cast $data.user to my special user implementation. How do I do
> that in the template in order to access special functions?

You can't 'cast' in velocity.

What you can do is just access any public method....  Velocity doesn't care
about the class type other than that it is public.

-- 
Geir Magnusson Jr. 
Research & Development, Adeptra Inc.
geirm@adeptra.com
+1-203-247-1713



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


how do I class cast in the template?

Posted by peter neubauer <pe...@yahoo.se>.
Hi,
I want to cast $data.user to my special user implementation. How do I do
that in the template in order to access special functions?

Thanks

/peter

_____________________________________________________
F�lj VM p� n�ra h�ll p� Yahoo!s officielle VM-sajt www.yahoo.se/vm2002
H�ll dig ajour med nyheter och resultat, med vinnare och f�rlorare...

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Singleton vs Separate Instance

Posted by Kevin Baynes <kb...@seagullsw.com>.

> If they are separate webapps (rather than separate servlets...), then they
> should be independent.
>
> All I can think of is that there is a vel jar in the classpath or
> something...

They are separate webapps each expanded from its own .war file.

I checked this before sending the first email. I searched the Tomcat folder
again and did not find any velocity*.jar files outside of their respective
webapps.

Should I suspect some modification of the classpath by the other webapp?

~kevin


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Singleton vs Separate Instance

Posted by "Geir Magnusson Jr." <ge...@adeptra.com>.
On 8/20/02 2:54 PM, "Kevin Baynes" <kb...@seagullsw.com> wrote:

> Thanks for the response! :-)
> 
>> I assume that they are using a shared velocity jar?  Is there a
>> velocity.jar
>> in each WEB-INF/lib?  If not, if in the classpath or in the common lib of
>> tomcat, then you will certainly get the behavior you are seeing.
> 
> They are to distinct webapps, each with its own Velocity jar. One is
> velocity-dep-1.2.jar and the other is velocity-1.3-rc1.jar.

Is there a vel jar anywhere else?  In the classpath?

> 
>> There is a subtly not in the docs : It works when in each has it's own jar
>> as tomcat uses separate classloaders for each webapp, resulting in a
>> separate singleton instance for each.
> 
> This statement leads me to believe that I should not be seeing the above
> behavior where I have two distinct webapps, each with it's own velocity*.jar
> and one webapp is stomping the other's configuration, right?

If they are separate webapps (rather than separate servlets...), then they
should be independent.

All I can think of is that there is a vel jar in the classpath or
something...

-- 
Geir Magnusson Jr. 
Research & Development, Adeptra Inc.
geirm@adeptra.com
+1-203-247-1713



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Singleton vs Separate Instance

Posted by Kevin Baynes <kb...@seagullsw.com>.
Thanks for the response! :-)

> I assume that they are using a shared velocity jar?  Is there a
> velocity.jar
> in each WEB-INF/lib?  If not, if in the classpath or in the common lib of
> tomcat, then you will certainly get the behavior you are seeing.

They are to distinct webapps, each with its own Velocity jar. One is
velocity-dep-1.2.jar and the other is velocity-1.3-rc1.jar.

> There is a subtly not in the docs : It works when in each has it's own jar
> as tomcat uses separate classloaders for each webapp, resulting in a
> separate singleton instance for each.

This statement leads me to believe that I should not be seeing the above
behavior where I have two distinct webapps, each with it's own velocity*.jar
and one webapp is stomping the other's configuration, right?

~kevin


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Singleton vs Separate Instance

Posted by "Geir Magnusson Jr." <ge...@adeptra.com>.
On 8/20/02 12:57 PM, "Kevin Baynes" <kb...@seagullsw.com> wrote:

> I have a web application with a servlet that uses the Singleton Velocity
> pattern.
> 
> I have a second web app that uses velocity from a servlet, using Singleton
> Velociy. When I have both web apps in the same servlet container, it appears
> that the first web app (using Singleton) is "winning" and setting
> properties, which makes the second web app unable to find templates in it's
> /WEB-INF/classes directory, so it gets ResourceNotFoundException. If I
> remove the first app then restart container, second app runs fine. I'm using
> Tomcat 4.0.4-b2.

I assume that they are using a shared velocity jar?  Is there a velocity.jar
in each WEB-INF/lib?  If not, if in the classpath or in the common lib of
tomcat, then you will certainly get the behavior you are seeing.

> 
> I have some questions about Singleton vs Separate Instance.
> 
> The Velocity documentation states:
> 
> "This is the legacy pattern, where there is only one instance of the
> Velocity engine in the JVM (or web application, depending) that is shared by
> all. This is very convenient as it allows localized configuration and
> sharing of resources. For example, this is a very appropriate model for use
> in a Servlet 2.2+ compliant web application as each web application can have
> it's own instance of Velocity, allowing that web application's servlet to
> share resources like templates, a logger, etc. The singleton is accessable
> via the org.apache.velocity.app.Velocity class, and and example of use : "
> 
> The above statement is unclear to me because it says that "there is only one
> instance of the Velocity engine in the JVM", then it adds "(or web
> application, depending)". Depending on what?

Depending on if you are using it in a webapp environment or not, and if so,
if you have a jar in each WEB-INF/lib or shared.

There is a subtly not in the docs : It works when in each has it's own jar
as tomcat uses separate classloaders for each webapp, resulting in a
separate singleton instance for each.

> 
> The third sentence states that each web application can have it's own
> instance of Velocity. How is this accomplished?

WEB-INF/lib

> 
> The Velocity doc then explains about creating a "Separate Instance" of the
> VelocityEngine. This seems to allow each instantiating class to have
> separate instances of Velocity. Would using "Separate Instance" protect me
> from the above case where another web app in the servlet container is using
> Singleton Velocity?

Ys
 
> Thanks!
> 
> ~Kevin Baynes
> 
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 

-- 
Geir Magnusson Jr. 
Research & Development, Adeptra Inc.
geirm@adeptra.com
+1-203-247-1713



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Singleton vs Separate Instance

Posted by Kevin Baynes <kb...@seagullsw.com>.
Answering my own email. :-)

Last question was: "Would using 'Separate Instance' protect me from the
above case where another web app in the servlet container is using
'Singleton' Velocity?"

I changed my app to use "Separate Instance" method. I then tested by adding
the other app (using Singleton) into the container: the new "Separate
Instance" application worked fine. I also had an older version of my app
that used singleton, which broke when I added other singleton web app into
container. It appears that "Separate Instance" will protect a web app from
other apps using "Singleton" in same container.

I would still like answers to the other questions, if anyone has them. :-)

~kevin

> -----Original Message-----
> From: Kevin Baynes [mailto:kbaynes@seagullsw.com]
> Sent: Tuesday, August 20, 2002 12:58 PM
> To: Velocity Users List
> Subject: Singleton vs Separate Instance
>
>
> I have a web application with a servlet that uses the Singleton Velocity
> pattern.
>
> I have a second web app that uses velocity from a servlet, using Singleton
> Velociy. When I have both web apps in the same servlet container,
> it appears
> that the first web app (using Singleton) is "winning" and setting
> properties, which makes the second web app unable to find
> templates in it's
> /WEB-INF/classes directory, so it gets ResourceNotFoundException. If I
> remove the first app then restart container, second app runs
> fine. I'm using
> Tomcat 4.0.4-b2.
>
> I have some questions about Singleton vs Separate Instance.
>
> The Velocity documentation states:
>
> "This is the legacy pattern, where there is only one instance of the
> Velocity engine in the JVM (or web application, depending) that
> is shared by
> all. This is very convenient as it allows localized configuration and
> sharing of resources. For example, this is a very appropriate
> model for use
> in a Servlet 2.2+ compliant web application as each web
> application can have
> it's own instance of Velocity, allowing that web application's servlet to
> share resources like templates, a logger, etc. The singleton is accessable
> via the org.apache.velocity.app.Velocity class, and and example of use : "
>
> The above statement is unclear to me because it says that "there
> is only one
> instance of the Velocity engine in the JVM", then it adds "(or web
> application, depending)". Depending on what?
>
> The third sentence states that each web application can have it's own
> instance of Velocity. How is this accomplished?
>
> The Velocity doc then explains about creating a "Separate Instance" of the
> VelocityEngine. This seems to allow each instantiating class to have
> separate instances of Velocity. Would using "Separate Instance" protect me
> from the above case where another web app in the servlet
> container is using
> Singleton Velocity?
>
> Thanks!
>
> ~Kevin Baynes
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>