KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > factory > ClassFinderFactory


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.factory;
20
21 import java.lang.reflect.*;
22 import java.util.*;
23 import java.util.logging.*;
24
25 import org.openharmonise.commons.dsi.*;
26
27
28 /**
29  * Factory singleton class providing methods for obtaining <code>ClassFinder</code>
30  * instances.
31  *
32  * @author Michael Bell
33  * @version $Revision: 1.1 $
34  *
35  */

36 public class ClassFinderFactory {
37     
38     /**
39      * The singlton instance for this class
40      */

41     protected static ClassFinderFactory m_instance = null;
42     
43     /**
44      * The data store interface
45      */

46     protected AbstractDataStoreInterface m_dsi = null;
47     
48     /**
49      * The <code>Map</code> to store all instances of
50      * <code>ClassFinder</code>
51      */

52     protected Map m_finders;
53     
54     /**
55      * Logger for this class
56      */

57     private static final Logger m_logger = Logger.getLogger(ClassFinderFactory.class.getName());
58
59     /**
60      * Constructs an instance of the factory
61      *
62      * @param dsi the data store interface
63      */

64     private ClassFinderFactory(AbstractDataStoreInterface dsi) {
65         m_dsi = dsi;
66         m_finders = Collections.synchronizedMap(new HashMap(50));
67     }
68
69     /**
70      * Returns the singleton instance of this factory.
71      *
72      * @param dsi the data store interface
73      * @return the singleton instance of this factory
74      */

75     public static ClassFinderFactory getInstance(AbstractDataStoreInterface dsi) {
76         if (m_instance == null) {
77             m_instance = new ClassFinderFactory(dsi);
78         }
79
80         return m_instance;
81     }
82
83     /**
84      * Returns the appropriate <code>ClassFinder</code> for the specified
85      * class name.
86      *
87      * @param sClassname the class name
88      * @return the appropriate <code>ClassFinder</code> for the specified
89      * class name
90      * @throws ClassFinderException if an error occurs creating the new
91      * <code>ClassFinder</code>
92      */

93     public ClassFinder getClassFinder(String JavaDoc sClassname)
94                                throws ClassFinderException {
95         ClassFinder finder = null;
96
97         if (m_finders.containsKey(sClassname) == true) {
98             finder = (ClassFinder) m_finders.get(sClassname);
99         } else {
100             try {
101                 Class JavaDoc[] initArgsClass = null;
102                 Object JavaDoc[] initArgs = null;
103
104                 initArgsClass = new Class JavaDoc[] { AbstractDataStoreInterface.class };
105                 initArgs = new Object JavaDoc[] { this.m_dsi };
106
107                 Class JavaDoc clss = Class.forName(sClassname);
108                 Constructor initArgsConstructor = clss.getConstructor(
109                                                           initArgsClass);
110                 finder = (ClassFinder) initArgsConstructor.newInstance(initArgs);
111
112                 m_finders.put(sClassname, finder);
113             } catch (Exception JavaDoc e) {
114                 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
115                 throw new ClassFinderException(e.getMessage());
116             }
117         }
118
119         return finder;
120     }
121 }
Popular Tags