View Javadoc

1   // $Id: SingletonManager.java,v 1.4 2004/06/24 20:54:18 powerpete Exp $
2   // Created on 27.03.2004
3   package com.pnpconsult.zeiterfassung.util;
4   
5   import java.util.HashMap;
6   import java.util.Map;
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  /***
11   * @deprecated
12   * 
13   * @author <a href="mailto:powerpete@users.sf.net">Moritz Petersen</a>
14   * @version $Id: SingletonManager.java,v 1.4 2004/06/24 20:54:18 powerpete Exp $
15   */
16  public abstract class SingletonManager
17  {
18      private static final Log LOG = LogFactory.getLog(SingletonManager.class);
19  
20      private static Map instances = new HashMap();
21  
22      protected SingletonManager()
23      {}
24  
25      protected synchronized static Object getInstance(Class type, Invokator invokator)
26      {
27          Object instance = instances.get(type);
28          if (instance == null)
29          {
30              try
31              {
32                  instance = invokator.newInstance();
33              }
34              catch (Exception e)
35              {
36                  LOG.fatal("Error while instantiating " + type, e);
37              }
38              changeInstance(type, instance);
39          }
40          return instance;
41      }
42  
43      public static void changeInstance(Class clazz, Object instance)
44      {
45          instances.put(clazz, instance);
46      }
47  
48      protected static interface Invokator
49      {
50          Object newInstance();
51      }
52  }