1 package org.oddjob.arooa; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.lang.reflect.Modifier ; 8 import java.util.Enumeration ; 9 import java.util.Hashtable ; 10 import java.util.Properties ; 11 12 import org.apache.log4j.Logger; 13 14 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 33 private final Hashtable classDefinitions = new Hashtable (); 34 35 41 public void addResource(String classDefFileName) { 42 43 Properties props = new Properties (); 44 45 try { 46 InputStream 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 e) { 55 throw new ArooaException("Failed to load class def list.", e); 56 } 57 58 addProperties(props); 59 } 60 61 66 public void addFile(File file) { 67 68 Properties props = new Properties (); 69 70 try { 71 InputStream in = new FileInputStream (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 e) { 80 throw new ArooaException("Failed to load class def list.", e); 81 } 82 83 addProperties(props); 84 } 85 86 91 public void addProperties(Properties props) { 92 93 Enumeration enumeration = props.propertyNames(); 94 while (enumeration.hasMoreElements()) { 95 String key = (String ) enumeration.nextElement(); 96 String value = props.getProperty(key); 97 classDefinitions.put(key, value); 98 } 99 } 100 101 public void set(String key, String value) { 102 classDefinitions.put(key, value); 103 } 104 105 117 118 public static void checkTaskClass(final Class taskClass) throws ArooaException { 119 120 if (!Modifier.isPublic(taskClass.getModifiers())) { 121 final String message = taskClass + " is not public"; 122 throw new ArooaException(message); 123 } 124 if (Modifier.isAbstract(taskClass.getModifiers())) { 125 final String message = taskClass + " is abstract"; 126 throw new ArooaException(message); 127 } 128 try { 129 taskClass.getConstructor(null); 130 } catch (NoSuchMethodException e) { 133 final String message = "No public no-arg constructor in " 134 + taskClass; 135 throw new ArooaException(message); 136 } 137 } 138 139 public static Class loadClass(String className) { 140 Class c = null; 141 try { 142 c = Class.forName(className); 143 } catch (ClassNotFoundException e) { 144 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 145 try { 146 c = cl.loadClass(className); 147 } catch (ClassNotFoundException cnfe) { 148 throw new ArooaException("Could not load class " + className, cnfe); 149 } catch (NoClassDefFoundError 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 createObjectFromClass(String className) 161 throws ArooaException, IllegalAccessException , 162 InstantiationException { 163 164 Class c = loadClass(className); 165 checkTaskClass(c); 166 167 return c.newInstance(); 168 } 169 170 173 public Object createObject(String name) throws ArooaException { 174 175 String value = (String )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 e) { 188 throw new ArooaException("Illegal access attempting to create object of class " 189 + value + " for shorthand " + name, e); 190 } catch (InstantiationException e) { 191 throw new ArooaException("InstantiationException attempting to create object of class " 192 + value + " for shorthand " + name, e); 193 } 194 } 195 } 196 | Popular Tags |