1 9 package org.jboss.portal.server; 10 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.util.Collections ; 14 import java.util.Enumeration ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 import java.util.Properties ; 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 30 public class AbstractObjectFactory implements ObjectFactory 31 { 32 33 34 protected Logger log = Logger.getLogger(getClass()); 35 36 37 protected Map metaDataClassToObjectClass; 38 39 public AbstractObjectFactory(Properties props) 40 { 41 metaDataClassToObjectClass = Collections.synchronizedMap(new HashMap ()); 42 init(props); 43 } 44 45 public AbstractObjectFactory() 46 { 47 metaDataClassToObjectClass = Collections.synchronizedMap(new HashMap ()); 48 } 49 50 public AbstractObjectFactory(String resourceName) 51 { 52 metaDataClassToObjectClass = Collections.synchronizedMap(new HashMap ()); 53 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 54 InputStream in = null; 55 try 56 { 57 in = loader.getResourceAsStream(resourceName); 58 if (in != null) 59 { 60 Properties props = new Properties (); 61 props.load(in); 62 init(props); 63 } 64 } 65 catch (IOException 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 create(MetaData metaData) throws Exception 76 { 77 Class objectClazz = (Class )metaDataClassToObjectClass.get(metaData.getClass()); 78 Object o = objectClazz.newInstance(); 79 return o; 80 } 81 82 private void init(Properties props) 83 { 84 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 85 for (Enumeration en = props.propertyNames();en.hasMoreElements();) 86 { 87 String metaDataClassName = (String )en.nextElement(); 88 String objectClassName = props.getProperty(metaDataClassName); 89 log.debug("Associate " + metaDataClassName + " to " + objectClassName); 90 91 try 92 { 93 Class metaDataClass = loader.loadClass(metaDataClassName); 94 log.debug("Loaded meta data class " + metaDataClassName); 95 96 Class objectClass = loader.loadClass(objectClassName); 97 log.debug("Loaded object class " + objectClassName); 98 99 metaDataClassToObjectClass.put(metaDataClass, objectClass); 100 } 101 catch (ClassNotFoundException e) 102 { 103 log.error("Cannot load class " + e); 104 } 105 } 106 } 107 } 108 | Popular Tags |