Re: singleton - hashmap references

"Vince Bono" <[email protected]> Sat, 14 Aug 2004 14:49:45 -0400
Newsgroups gmane.comp.java.keel.user
Message-ID <003901c4822f$7ab7b4b0$4345fea9@p4laptop>
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