KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > DatabaseDependentClassManager


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseDependentClassManager.java,v 1.4 2007/01/07 06:14:18 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.persist.db;
23
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import org.opensubsystems.core.error.OSSException;
29 import org.opensubsystems.core.util.ClassFactory;
30 import org.opensubsystems.core.util.GlobalConstants;
31 import org.opensubsystems.core.util.Log;
32
33 /**
34  * Class responsible for instantiation of classes whose implementation depends
35  * on currently used database. It creates new instance if it wasn't created yet
36  * and caches created instances. This of course assumes that the data dependent
37  * classes are implemented to be stateless and reentrant.
38  *
39  * @version $Id: DatabaseDependentClassManager.java,v 1.4 2007/01/07 06:14:18 bastafidli Exp $
40  * @author Miro Halas
41  * @code.reviewer Miro Halas
42  * @code.reviewed Initial revision
43  */

44 public class DatabaseDependentClassManager
45 {
46    // Constants ////////////////////////////////////////////////////////////////
47

48    /**
49     * Lock used in synchronized sections.
50     */

51    private static final String JavaDoc IMPL_LOCK = "IMPL_LOCK";
52
53    // Attributes ///////////////////////////////////////////////////////////////
54

55    /**
56     * Class factory used to instantiate database dependent classes.
57     */

58    protected ClassFactory m_classFactory;
59    
60    /**
61     * Cache where already instantiated database dependent classes will be cached.
62     * We can cache them since they should be reentrant.
63     */

64    private Map JavaDoc m_mpClassCache;
65    
66    // Cached values ////////////////////////////////////////////////////////////
67

68    /**
69     * Logger for this class
70     */

71    private static Logger JavaDoc s_logger = Log.getInstance(DatabaseDependentClassManager.class);
72    
73    /**
74     * Reference to the instance actually in use.
75     */

76    private static DatabaseDependentClassManager s_defaultInstance;
77
78    // Constructors /////////////////////////////////////////////////////////////
79

80    /**
81     * Default constructor.
82     */

83    public DatabaseDependentClassManager(
84    )
85    {
86       m_classFactory = new DatabaseDependentClassFactory();
87       m_mpClassCache = new HashMap JavaDoc();
88    }
89    
90    /**
91     * Create database dependent instance for specified class.
92     *
93     * @param dependentClass - the database dependent class for which we want
94     * applicable instance. This is usually database
95     * independent class and we will try to create
96     * database dependent class.
97     * @return Object - the database dependent instance to use for given class
98     * @throws OSSException - an error has occured
99     */

100    public static Object JavaDoc getInstance(
101       Class JavaDoc dependentClass
102    ) throws OSSException
103    {
104       return getManagerInstance().getDependentInstance(dependentClass);
105    }
106    
107    /**
108     * Get the default instance. This method is here to make the class manager
109     * configurable. Once can specify in configuration file derived class to used
110     * instead of this one [DatabaseDependentClassManager.class]=new class to use.
111     *
112     * @return DatabaseDependentClassManager
113     * @throws OSSException - cannot get current database
114     */

115    public static DatabaseDependentClassManager getManagerInstance(
116    ) throws OSSException
117    {
118       if (s_defaultInstance == null)
119       {
120          // Only if the default instance wasn't set by other means create a new one
121
// Synchronize just for the creation
122
synchronized (IMPL_LOCK)
123          {
124             setManagerInstance((DatabaseDependentClassManager)
125                ClassFactory.getInstance().createInstance(
126                   DatabaseDependentClassManager.class,
127                   DatabaseDependentClassManager.class));
128          }
129       }
130       
131       return s_defaultInstance;
132    }
133    
134    /**
135     * Set default instance. This instance will be returned by getInstance
136     * method until it is changed.
137     *
138     * @param defaultInstance - new default instance
139     * @see #getInstance
140     */

141    public static void setManagerInstance(
142       DatabaseDependentClassManager defaultInstance
143    )
144    {
145       if (GlobalConstants.ERROR_CHECKING)
146       {
147          assert defaultInstance != null : "Default instance cannot be null";
148       }
149       
150       synchronized (IMPL_LOCK)
151       {
152          s_defaultInstance = defaultInstance;
153          s_logger.info("Default database dependent class manager is "
154                        + s_defaultInstance.getClass().getName());
155       }
156    }
157
158    /**
159     * Method to create actual database dependent instance based on specified class.
160     * This method can be overriden and new manager can be setup either through
161     * setManagerInstance or through configuration file if different strategy is
162     * desired.
163     *
164     * @param dependentClass - the database dependent class for which we want
165     * applicable instance. This is usually database
166     * independent class and we will try to create
167     * database dependent class.
168     * @return Object - the database dependent instance to use for given class
169     * @throws OSSException - an error has occured
170     */

171    public Object JavaDoc getDependentInstance(
172       Class JavaDoc dependentClass
173    ) throws OSSException
174    {
175       Object JavaDoc classInstance;
176       
177       classInstance = m_mpClassCache.get(dependentClass.getName());
178       if (classInstance == null)
179       {
180          synchronized (m_mpClassCache)
181          {
182             classInstance = m_classFactory.createInstance(dependentClass);
183             m_mpClassCache.put(dependentClass.getName(), classInstance);
184          }
185       }
186       
187       return classInstance;
188    }
189 }
190
Popular Tags