KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseSchemaManager.java,v 1.8 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 database schemas. This class determines
35  * what database schema should be used based on currently used database, creates
36  * the schema instance if it wasn't created yet and caches created instances.
37  * This of course assumes that the data factories are implemented to be stateless
38  * and reentrant.
39  *
40  * @version $Id: DatabaseSchemaManager.java,v 1.8 2007/01/07 06:14:18 bastafidli Exp $
41  * @author Miro Halas
42  * @code.reviewer Miro Halas
43  * @code.reviewed 1.4 2005/03/11 14:48:58 bastafidli
44  */

45 public class DatabaseSchemaManager
46 {
47    // Constants ////////////////////////////////////////////////////////////////
48

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

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

56    /**
57     * Class factory used to instantiate database schemas.
58     */

59    protected ClassFactory m_schemaClassFactory;
60    
61    /**
62     * Cache where already instantiated database schemas will be cached. We can
63     * cache them since database schemas should be reentrant.
64     */

65    private Map JavaDoc m_mpSchemaCache;
66    
67    // Cached values ////////////////////////////////////////////////////////////
68

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

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

77    private static DatabaseSchemaManager s_defaultInstance;
78
79    // Constructors /////////////////////////////////////////////////////////////
80

81    /**
82     * Default constructor.
83     */

84    public DatabaseSchemaManager(
85    )
86    {
87       m_schemaClassFactory = new DatabaseSchemaClassFactory();
88       m_mpSchemaCache = new HashMap JavaDoc();
89    }
90    
91    /**
92     * Create database schema for specified class.
93     *
94     * @param databaseSchema - the database schema class for which we want
95     * applicable schema. This is usually database
96     * independent class and we will try to create
97     * database dependent class.
98     * @return DatabaseSchema - the database schema to use for given class
99     * @throws OSSException - an error has occured
100     */

101    public static DatabaseSchema getInstance(
102       Class JavaDoc databaseSchema
103    ) throws OSSException
104    {
105       return getManagerInstance().getSchemaInstance(databaseSchema);
106    }
107    
108    /**
109     * Create database schema for specified class.
110     *
111     * @param databaseSchema - the database schema class name for which we want
112     * applicable schema. This is usually database
113     * independent class and we will try to create
114     * database dependent class.
115     * @return DatabaseSchema - the database schema to use for given class
116     * @throws OSSException - an error has occured
117     */

118    public static DatabaseSchema getInstance(
119       String JavaDoc databaseSchema
120    ) throws OSSException
121    {
122       return getManagerInstance().getSchemaInstance(databaseSchema);
123    }
124    
125    /**
126     * Get the default instance. This method is here to make the schema manager
127     * configurable. Once can specify in configuration file derived class to used
128     * instead of this one [DatabaseSchemaManager.class]=new class to use.
129     *
130     * @return DatabaseSchemaManager
131     * @throws OSSException - cannot get current database
132     */

133    public static DatabaseSchemaManager getManagerInstance(
134    ) throws OSSException
135    {
136       if (s_defaultInstance == null)
137       {
138          // Only if the default instance wasn't set by other means create a new one
139
// Synchronize just for the creation
140
synchronized (IMPL_LOCK)
141          {
142             setManagerInstance((DatabaseSchemaManager)
143                ClassFactory.getInstance().createInstance(
144                   DatabaseSchemaManager.class, DatabaseSchemaManager.class));
145          }
146       }
147       
148       return s_defaultInstance;
149    }
150    
151    /**
152     * Set default instance. This instance will be returned by getInstance
153     * method until it is changed.
154     *
155     * @param defaultInstance - new default instance
156     * @see #getInstance
157     */

158    public static void setManagerInstance(
159       DatabaseSchemaManager defaultInstance
160    )
161    {
162       if (GlobalConstants.ERROR_CHECKING)
163       {
164          assert defaultInstance != null : "Default instance cannot be null";
165       }
166       
167       synchronized (IMPL_LOCK)
168       {
169          s_defaultInstance = defaultInstance;
170          s_logger.fine("Default database schema manager is "
171                        + s_defaultInstance.getClass().getName());
172       }
173    }
174
175    /**
176     * Method to create actual schema based on specified class. This method can
177     * be overriden and new manager can be setup either through setManagerInstance
178     * or through configuration file if different strategy is desired.
179     *
180     * @param databaseSchema - the database schema class for which we want
181     * applicable schema. This is usually database
182     * independent class and we will try to create
183     * database dependent class.
184     * @return DatabaseSchema - the database schema to use for given class
185     * @throws OSSException - an error has occured
186     */

187    public DatabaseSchema getSchemaInstance(
188       Class JavaDoc databaseSchema
189    ) throws OSSException
190    {
191       Object JavaDoc schema;
192       
193       schema = m_mpSchemaCache.get(databaseSchema.getName());
194       if (schema == null)
195       {
196          synchronized (m_mpSchemaCache)
197          {
198             schema = m_schemaClassFactory.createInstance(databaseSchema);
199             m_mpSchemaCache.put(databaseSchema.getName(), schema);
200          }
201       }
202       
203       return (DatabaseSchema)schema;
204    }
205
206    /**
207     * Method to create actual schema based on specified class. This method can
208     * be overriden and new manager can be setup either through setManagerInstance
209     * or through configuration file if different strategy is desired.
210     *
211     * @param databaseSchema - the database schema class name for which we want
212     * applicable schema. This is usually database
213     * independent class and we will try to create
214     * database dependent class.
215     * @return DatabaseSchema - the database schema to use for given class
216     * @throws OSSException - an error has occured
217     */

218    public DatabaseSchema getSchemaInstance(
219       String JavaDoc databaseSchema
220    ) throws OSSException
221    {
222       Object JavaDoc schema;
223       
224       schema = m_mpSchemaCache.get(databaseSchema);
225       if (schema == null)
226       {
227          synchronized (m_mpSchemaCache)
228          {
229             schema = m_schemaClassFactory.createInstance(databaseSchema);
230             m_mpSchemaCache.put(databaseSchema, schema);
231          }
232       }
233       
234       return (DatabaseSchema)schema;
235    }
236 }
237
Popular Tags