KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > AbstractObjectFactory


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server;
10
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 import org.apache.log4j.Logger;
20 import org.jboss.portal.common.metadata.MetaData;
21 import org.jboss.portal.common.util.Tools;
22
23 /**
24  * A simple implementation that uses a map to know the
25  * class objects used to instantiate objects.
26  *
27  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
28  * @version $Revision: 1.2 $
29  */

30 public class AbstractObjectFactory implements ObjectFactory
31 {
32
33    /** The logger. */
34    protected Logger log = Logger.getLogger(getClass());
35
36    /** . */
37    protected Map JavaDoc metaDataClassToObjectClass;
38
39    public AbstractObjectFactory(Properties JavaDoc props)
40    {
41       metaDataClassToObjectClass = Collections.synchronizedMap(new HashMap JavaDoc());
42       init(props);
43    }
44
45    public AbstractObjectFactory()
46    {
47       metaDataClassToObjectClass = Collections.synchronizedMap(new HashMap JavaDoc());
48    }
49
50    public AbstractObjectFactory(String JavaDoc resourceName)
51    {
52       metaDataClassToObjectClass = Collections.synchronizedMap(new HashMap JavaDoc());
53       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
54       InputStream JavaDoc in = null;
55       try
56       {
57          in = loader.getResourceAsStream(resourceName);
58          if (in != null)
59          {
60             Properties JavaDoc props = new Properties JavaDoc();
61             props.load(in);
62             init(props);
63          }
64       }
65       catch (IOException JavaDoc e)
66       {
67          log.error("Cannot load specified property file " + resourceName, e);
68       }
69       finally
70       {
71          Tools.safeClose(in);
72       }
73    }
74
75    public Object JavaDoc create(MetaData metaData) throws Exception JavaDoc
76    {
77       Class JavaDoc objectClazz = (Class JavaDoc)metaDataClassToObjectClass.get(metaData.getClass());
78       Object JavaDoc o = objectClazz.newInstance();
79       return o;
80    }
81
82    private void init(Properties JavaDoc props)
83    {
84       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
85       for (Enumeration JavaDoc en = props.propertyNames();en.hasMoreElements();)
86       {
87          String JavaDoc metaDataClassName = (String JavaDoc)en.nextElement();
88          String JavaDoc objectClassName = props.getProperty(metaDataClassName);
89          log.debug("Associate " + metaDataClassName + " to " + objectClassName);
90
91          try
92          {
93             Class JavaDoc metaDataClass = loader.loadClass(metaDataClassName);
94             log.debug("Loaded meta data class " + metaDataClassName);
95
96             Class JavaDoc objectClass = loader.loadClass(objectClassName);
97             log.debug("Loaded object class " + objectClassName);
98
99             metaDataClassToObjectClass.put(metaDataClass, objectClass);
100          }
101          catch (ClassNotFoundException JavaDoc e)
102          {
103             log.error("Cannot load class " + e);
104          }
105       }
106    }
107 }
108
Popular Tags