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.factory;
020:
021: import java.lang.reflect.*;
022: import java.util.*;
023: import java.util.logging.*;
024:
025: import org.openharmonise.commons.dsi.*;
026:
027: /**
028: * Factory singleton class providing methods for obtaining <code>ClassFinder</code>
029: * instances.
030: *
031: * @author Michael Bell
032: * @version $Revision: 1.1 $
033: *
034: */
035: public class ClassFinderFactory {
036:
037: /**
038: * The singlton instance for this class
039: */
040: protected static ClassFinderFactory m_instance = null;
041:
042: /**
043: * The data store interface
044: */
045: protected AbstractDataStoreInterface m_dsi = null;
046:
047: /**
048: * The <code>Map</code> to store all instances of
049: * <code>ClassFinder</code>
050: */
051: protected Map m_finders;
052:
053: /**
054: * Logger for this class
055: */
056: private static final Logger m_logger = Logger
057: .getLogger(ClassFinderFactory.class.getName());
058:
059: /**
060: * Constructs an instance of the factory
061: *
062: * @param dsi the data store interface
063: */
064: private ClassFinderFactory(AbstractDataStoreInterface dsi) {
065: m_dsi = dsi;
066: m_finders = Collections.synchronizedMap(new HashMap(50));
067: }
068:
069: /**
070: * Returns the singleton instance of this factory.
071: *
072: * @param dsi the data store interface
073: * @return the singleton instance of this factory
074: */
075: public static ClassFinderFactory getInstance(
076: AbstractDataStoreInterface dsi) {
077: if (m_instance == null) {
078: m_instance = new ClassFinderFactory(dsi);
079: }
080:
081: return m_instance;
082: }
083:
084: /**
085: * Returns the appropriate <code>ClassFinder</code> for the specified
086: * class name.
087: *
088: * @param sClassname the class name
089: * @return the appropriate <code>ClassFinder</code> for the specified
090: * class name
091: * @throws ClassFinderException if an error occurs creating the new
092: * <code>ClassFinder</code>
093: */
094: public ClassFinder getClassFinder(String sClassname)
095: throws ClassFinderException {
096: ClassFinder finder = null;
097:
098: if (m_finders.containsKey(sClassname) == true) {
099: finder = (ClassFinder) m_finders.get(sClassname);
100: } else {
101: try {
102: Class[] initArgsClass = null;
103: Object[] initArgs = null;
104:
105: initArgsClass = new Class[] { AbstractDataStoreInterface.class };
106: initArgs = new Object[] { this .m_dsi };
107:
108: Class clss = Class.forName(sClassname);
109: Constructor initArgsConstructor = clss
110: .getConstructor(initArgsClass);
111: finder = (ClassFinder) initArgsConstructor
112: .newInstance(initArgs);
113:
114: m_finders.put(sClassname, finder);
115: } catch (Exception e) {
116: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
117: throw new ClassFinderException(e.getMessage());
118: }
119: }
120:
121: return finder;
122: }
123: }
|