Re: Re: singleton - hashmap references
"Shash Chatterjee" <[email protected]> Tue, 17 Aug 2004 07:36:41 -0600
| Newsgroups | gmane.comp.java.keel.user |
|---|---|
| Message-ID | <[email protected]> |
Vince,
For this to work, you need to acquire the Dictionary from the container. But
since you are doing a "new" to instantiate the object, it is giving you a
sepearte instance of the class, as expected. In other words, you are not
getting the same instance that the container created and initialized.
If you must create it in the JSP, which is on the keel-client side and does
not have access to the keel-server-side components, then you must write your
class as a "classic" Singleton, (i.e. make the constructor private, and
implement a static getInstance method, which always returns the same
instance). In that case, Keel/Fortress are out of the picture.
I don't know if it makes sense for your application or not, but for Keel to be
involved, you need to send the phrase you are looking up as a parameter to a
model, and then have the model do the lookup and return the value back as an
"Output" for the JSP to display.
Shash
> Shash
>
> Were almost there, but is not behaving as expected ...
>
> -------------------------------
> in the Dictionary.java file (stripped down)
>
> import org.apache.avalon.framework.context.Context;
> import org.apache.avalon.framework.activity.Initializable;
>
> /**
> * Dictionary methods to handle lookups
> *
> * @avalon.component
> * @avalon.service type=org.keel.services.model.Model
> * @x-avalon.info name=avaya.dictionary
> * @x-avalon.lifestyle type=singleton
> *
> */
>
> public class Dictionary implements Initializable {
>
> public static String hello="h";
>
> public void initialize() {
> System.err.println("Dictionary.initialize()");
> hello="hello";
> }
>
> }
>
> -------------------------------
> in the .jsp file (stripped down)
>
> <%
> com.netlabs.avaya.models.Dictionary Dict = new
> com.netlabs.avaya.models.Dictionary();
> %>
> hello=<%= Dict.hello %>
>
> -------------------------------
>
> The .jsp outputs "hello=h" instead of "hello=hello" ?
>
> It appears as if the .initialize() was not run.
> However, I do see "Dictionary.initialize()" output in the
> catalina.out logs.
>
> ----- Original Message -----
> From: "Shash Chatterjee" <shash-9rV7jguIsxHYtjvyW6yDsg-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org>
> Newsgroups: gmane.comp.java.keel.user
> Sent: Wednesday, August 11, 2004 4:14 PM
> Subject: Re: singleton - hashmap references
>
> > Vince,
> >
> >
> > > What is the preferred method to create a singleton class that is
> > > instantiated when the application is initialized but whose methods,
> > > or variables,
> > > (in my case static hashMap lookups) are available to all models
> > > within an application.
> > >
> >
> > - For the container to return the same singleton component instance every
> time
> > the component is looked up, in your (thread-safe) implementation class's
> > meta-data, you should have:
> > * @x-avalon.lifestyle type=singleton
> >
> > - For the container to instantiate this component when the container
> starts
> > up, in the component's configuration set activation as "startup":
> > <my-component id="myapp.mycomponent" activation="startup">
> > <other-config>
> > </other-config>
> > </my-component>
> >
> > - For initialization of the class when it is instantiated, unless you want
> > lazy initialization, implement the Initializable interface. The container
> > will call the initialize() method when the component is first created
> >
> > HTH,
> > Shash
>
> _______________________________________________
> User mailing list
> [email protected]
> http://lists.keelframework.org/listinfo.cgi/user-keelframework.org