KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > SimpleObjectFactory


1 package org.oddjob.arooa;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.lang.reflect.Modifier JavaDoc;
8 import java.util.Enumeration JavaDoc;
9 import java.util.Hashtable JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import org.apache.log4j.Logger;
13
14 /**
15  * A Simple object factory that uses property sets of name class pairs
16  * to create the objects.
17  */

18 public class SimpleObjectFactory implements ObjectFactory {
19     private static final Logger logger = Logger.getLogger(SimpleObjectFactory.class);
20
21     private final ObjectFactory parent;
22     
23     public SimpleObjectFactory(ObjectFactory parent) {
24         this.parent = parent;
25     }
26     
27     public SimpleObjectFactory() {
28         this(null);
29     }
30     
31     
32     /** Class definitions */
33     private final Hashtable JavaDoc classDefinitions = new Hashtable JavaDoc();
34     
35     /**
36      * Add definitions fom a resource property file, i.e. one
37      * that is on the class path.
38      *
39      * @param classDefFileName The file name.
40      */

41     public void addResource(String JavaDoc classDefFileName) {
42
43         Properties JavaDoc props = new Properties JavaDoc();
44
45         try {
46             InputStream JavaDoc in = this.getClass().getResourceAsStream(classDefFileName);
47     
48             if (in == null) {
49                     throw new ArooaException("Can't load class def list");
50             }
51             props.load(in);
52             in.close();
53         }
54         catch (IOException JavaDoc e) {
55             throw new ArooaException("Failed to load class def list.", e);
56         }
57
58         addProperties(props);
59     }
60             
61     /**
62      * Add definitions fom a property file.
63      *
64      * @param classDefFileName The file name.
65      */

66     public void addFile(File JavaDoc file) {
67         
68         Properties JavaDoc props = new Properties JavaDoc();
69
70         try {
71             InputStream JavaDoc in = new FileInputStream JavaDoc(file);
72     
73             if (in == null) {
74                     throw new ArooaException("Can't load class def list");
75             }
76             props.load(in);
77             in.close();
78         }
79         catch (IOException JavaDoc e) {
80             throw new ArooaException("Failed to load class def list.", e);
81         }
82
83         addProperties(props);
84     }
85     
86     /**
87      * Add class definitions from properties.
88      *
89      * @param props Class definition properties.
90      */

91     public void addProperties(Properties JavaDoc props) {
92         
93         Enumeration JavaDoc enumeration = props.propertyNames();
94         while (enumeration.hasMoreElements()) {
95             String JavaDoc key = (String JavaDoc) enumeration.nextElement();
96             String JavaDoc value = props.getProperty(key);
97             classDefinitions.put(key, value);
98         }
99     }
100
101     public void set(String JavaDoc key, String JavaDoc value) {
102         classDefinitions.put(key, value);
103     }
104
105     /**
106      * Checks whether or not a class is suitable for serving as Ant task.
107      * Ant task implementation classes must be public, concrete, and have
108      * a no-arg constructor.
109      *
110      * @param taskClass The class to be checked.
111      * Must not be <code>null</code>.
112      *
113      * @exception ArooaException if the class is unsuitable for being an Ant
114      * task. An error level message is logged before
115      * this exception is thrown.
116      */

117
118     public static void checkTaskClass(final Class JavaDoc taskClass) throws ArooaException {
119
120         if (!Modifier.isPublic(taskClass.getModifiers())) {
121             final String JavaDoc message = taskClass + " is not public";
122             throw new ArooaException(message);
123         }
124         if (Modifier.isAbstract(taskClass.getModifiers())) {
125             final String JavaDoc message = taskClass + " is abstract";
126             throw new ArooaException(message);
127         }
128         try {
129             taskClass.getConstructor(null);
130             // don't have to check for public, since
131
// getConstructor finds public constructors only.
132
} catch (NoSuchMethodException JavaDoc e) {
133             final String JavaDoc message = "No public no-arg constructor in "
134                 + taskClass;
135             throw new ArooaException(message);
136         }
137     }
138
139     public static Class JavaDoc loadClass(String JavaDoc className) {
140         Class JavaDoc c = null;
141         try {
142             c = Class.forName(className);
143         } catch (ClassNotFoundException JavaDoc e) {
144             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
145             try {
146                 c = cl.loadClass(className);
147             } catch (ClassNotFoundException JavaDoc cnfe) {
148                 throw new ArooaException("Could not load class " + className, cnfe);
149             } catch (NoClassDefFoundError JavaDoc ncdfe) {
150                 throw new ArooaException("Could not load a dependent class ("
151                     + ncdfe.getMessage() + ") for class " + c.getName() , ncdfe);
152             }
153         }
154         
155         logger.debug("Got class [" + className + "] with classloader ["
156                 + c.getClassLoader() + "]");
157         return c;
158     }
159     
160     public static Object JavaDoc createObjectFromClass(String JavaDoc className)
161             throws ArooaException, IllegalAccessException JavaDoc,
162                 InstantiationException JavaDoc {
163
164         Class JavaDoc c = loadClass(className);
165         checkTaskClass(c);
166
167         return c.newInstance();
168     }
169
170     /**
171      * Create the object for a given name.
172      */

173     public Object JavaDoc createObject(String JavaDoc name) throws ArooaException {
174
175         String JavaDoc value = (String JavaDoc)classDefinitions.get(name);
176         if (value == null) {
177             if (parent == null) {
178                 throw new ArooaException("No definition for [" + name + "]");
179             }
180             else {
181                 return parent.createObject(name);
182             }
183         }
184         
185         try {
186             return createObjectFromClass(value);
187         } catch (IllegalAccessException JavaDoc e) {
188             throw new ArooaException("Illegal access attempting to create object of class "
189                 + value + " for shorthand " + name, e);
190         } catch (InstantiationException JavaDoc e) {
191             throw new ArooaException("InstantiationException attempting to create object of class "
192                 + value + " for shorthand " + name, e);
193         }
194     }
195 }
196
Popular Tags