KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > dsi > DatabaseInfo


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.dsi;
20
21 import java.util.*;
22
23 import org.openharmonise.commons.dsi.*;
24
25
26 /**
27  * This class provides utility operations for finding out information
28  * regarding how objects within Harmonise are mapped to the database.
29  *
30  * @author Michael Bell
31  * @version $Revision: 1.1 $
32  *
33  */

34 public class DatabaseInfo {
35
36     /**
37      * <code>Map</code> of mappings between classnames and database table names
38      */

39     private Map m_mappings = null;
40     
41     /**
42      * Singleton instance of this class
43      */

44     private static DatabaseInfo m_instance = null;
45
46     /**
47      * Constructs an instance of <code>DatabaseInfo</code>
48      */

49     private DatabaseInfo() {
50         m_mappings = Collections.synchronizedMap(new HashMap());
51     }
52
53     /**
54      * Returns the singleton instance of <code>DatabaseInfo</code>
55      *
56      * @return the singleton instance of <code>DatabaseInfo</code>
57      */

58     public static DatabaseInfo getInstance() {
59         if (m_instance == null) {
60             m_instance = new DatabaseInfo();
61         }
62
63         return m_instance;
64     }
65
66     /**
67      * Register a database table name against the specified class name
68      *
69      * @param sClassname the class name
70      * @param sTableName the database table name
71      */

72     public void registerTableName(String JavaDoc sClassname, String JavaDoc sTableName) {
73         m_mappings.put(sClassname, sTableName);
74
75     }
76
77     /**
78      * Returns the database table name associated to the specified class name
79      *
80      * @param sClassname the class name
81      * @return the database table name associated to the specified class name
82      * @throws DataStoreException if the specified class name can not be found
83      * by the class loader or an error occurs trying to find the associated
84      * table name
85      */

86     public String JavaDoc getTableName(String JavaDoc sClassname) throws DataStoreException {
87         String JavaDoc sTableName = null;
88
89         sTableName = (String JavaDoc) m_mappings.get(sClassname);
90
91         if (sTableName == null) {
92             //Need to make sure class is loaded and try again
93
try {
94                 Class JavaDoc clss = Class.forName(sClassname);
95
96                 sTableName = (String JavaDoc) m_mappings.get(sClassname);
97
98                 if (sTableName == null) {
99                     if(DataStoreObject.class.isAssignableFrom(clss)) {
100                         DataStoreObject dsObj =
101                             (DataStoreObject) clss.newInstance();
102
103                         sTableName = dsObj.getDBTableName();
104
105                         registerTableName(sClassname, sTableName);
106                     }
107                 }
108             } catch (ClassNotFoundException JavaDoc e) {
109                 throw new DataStoreException("Class not found", e);
110             } catch (InstantiationException JavaDoc e) {
111                 throw new DataStoreException("instantiation error", e);
112             } catch (IllegalAccessException JavaDoc e) {
113                 throw new DataStoreException("illegal access", e);
114             }
115         }
116
117         return sTableName;
118     }
119
120 }
121
Popular Tags