001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.dsi;
020:
021: import java.util.*;
022:
023: import org.openharmonise.commons.dsi.*;
024:
025: /**
026: * This class provides utility operations for finding out information
027: * regarding how objects within Harmonise are mapped to the database.
028: *
029: * @author Michael Bell
030: * @version $Revision: 1.1 $
031: *
032: */
033: public class DatabaseInfo {
034:
035: /**
036: * <code>Map</code> of mappings between classnames and database table names
037: */
038: private Map m_mappings = null;
039:
040: /**
041: * Singleton instance of this class
042: */
043: private static DatabaseInfo m_instance = null;
044:
045: /**
046: * Constructs an instance of <code>DatabaseInfo</code>
047: */
048: private DatabaseInfo() {
049: m_mappings = Collections.synchronizedMap(new HashMap());
050: }
051:
052: /**
053: * Returns the singleton instance of <code>DatabaseInfo</code>
054: *
055: * @return the singleton instance of <code>DatabaseInfo</code>
056: */
057: public static DatabaseInfo getInstance() {
058: if (m_instance == null) {
059: m_instance = new DatabaseInfo();
060: }
061:
062: return m_instance;
063: }
064:
065: /**
066: * Register a database table name against the specified class name
067: *
068: * @param sClassname the class name
069: * @param sTableName the database table name
070: */
071: public void registerTableName(String sClassname, String sTableName) {
072: m_mappings.put(sClassname, sTableName);
073:
074: }
075:
076: /**
077: * Returns the database table name associated to the specified class name
078: *
079: * @param sClassname the class name
080: * @return the database table name associated to the specified class name
081: * @throws DataStoreException if the specified class name can not be found
082: * by the class loader or an error occurs trying to find the associated
083: * table name
084: */
085: public String getTableName(String sClassname)
086: throws DataStoreException {
087: String sTableName = null;
088:
089: sTableName = (String) m_mappings.get(sClassname);
090:
091: if (sTableName == null) {
092: //Need to make sure class is loaded and try again
093: try {
094: Class clss = Class.forName(sClassname);
095:
096: sTableName = (String) m_mappings.get(sClassname);
097:
098: if (sTableName == null) {
099: if (DataStoreObject.class.isAssignableFrom(clss)) {
100: DataStoreObject dsObj = (DataStoreObject) clss
101: .newInstance();
102:
103: sTableName = dsObj.getDBTableName();
104:
105: registerTableName(sClassname, sTableName);
106: }
107: }
108: } catch (ClassNotFoundException e) {
109: throw new DataStoreException("Class not found", e);
110: } catch (InstantiationException e) {
111: throw new DataStoreException("instantiation error", e);
112: } catch (IllegalAccessException e) {
113: throw new DataStoreException("illegal access", e);
114: }
115: }
116:
117: return sTableName;
118: }
119:
120: }
|