1 4 package org.oddjob.arooa; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.util.Enumeration ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 import java.util.Properties ; 14 15 import org.apache.log4j.Logger; 16 import org.oddjob.arooa.reflect.IntrospectionHelper; 17 18 25 public class PropertyProxyResolver { 26 private static final Logger logger = Logger.getLogger(PropertyProxyResolver.class); 27 28 29 private final Map classDefinitions = new HashMap (); 30 private Class [] definitionsAsArray = new Class [0]; 31 32 private PropertyProxyResolver parent; 33 34 public PropertyProxyResolver(PropertyProxyResolver parent) { 35 this.parent = parent; 36 } 37 38 public PropertyProxyResolver() { 39 this(null); 40 } 41 42 48 public void addResource(String classDefFileName) { 49 50 Properties props = new Properties (); 51 try { 52 InputStream in = this.getClass().getResourceAsStream(classDefFileName); 53 54 if (in == null) { 55 throw new ArooaException("Can't load class def list"); 56 } 57 props.load(in); 58 in.close(); 59 } 60 catch (IOException e) { 61 throw new ArooaException("Failed to load class def list.", e); 62 } 63 64 addProperties(props); 65 } 66 67 72 public void addFile(File file) { 73 74 Properties props = new Properties (); 75 76 try { 77 InputStream in = new FileInputStream (file); 78 79 if (in == null) { 80 throw new ArooaException("Can't load class def list"); 81 } 82 props.load(in); 83 in.close(); 84 } 85 catch (IOException e) { 86 throw new ArooaException("Failed to load class def list.", e); 87 } 88 89 addProperties(props); 90 } 91 92 97 public void addProperties(Properties props) { 98 99 Enumeration enumuration = props.propertyNames(); 100 while (enumuration.hasMoreElements()) { 101 String key = (String ) enumuration.nextElement(); 102 String value = props.getProperty(key); 103 setProxy(key, value); 104 } 105 } 106 107 public void setProxy(String key, String value) { 108 Class classKey = SimpleObjectFactory.loadClass(key); 109 Class classValue = SimpleObjectFactory.loadClass(value); 110 synchronized (classDefinitions) { 111 classDefinitions.put(classKey, classValue); 112 definitionsAsArray = (Class []) classDefinitions.keySet().toArray(new Class [0]); 113 } 114 } 115 116 Class proxyClassFor(Class clazz) { 117 Class key = IntrospectionHelper.selectBestMatchingClass(clazz, definitionsAsArray); 118 if (key == null) { 119 if (parent == null) { 120 return null; 121 } 122 else { 123 return parent.proxyClassFor(clazz); 124 } 125 } 126 Class result = null; 127 synchronized (classDefinitions) { 128 result = (Class ) classDefinitions.get(key); 129 } 130 logger.debug("Providing " + result.getName() + " as proxy for " + clazz.getName()); 131 return result; 132 } 133 134 145 public Object proxyFor(Object bean, String property) { 146 IntrospectionHelper pih = IntrospectionHelper.getHelper(bean.getClass()); 148 Class clazz = pih.getAttributeType(property); 149 if (clazz == null) { 150 throw new ArooaException("No such property [" + property 151 + "] for class " + bean.getClass()); 152 } 153 Class c = proxyClassFor(clazz); 154 if (c == null) { 155 return null; 156 } 157 try { 158 return c.newInstance(); 159 } catch (IllegalAccessException e) { 160 throw new ArooaException("Illegal access attempting to create object of class " 161 + c, e); 162 } catch (InstantiationException e) { 163 throw new ArooaException("InstantiationException attempting to create object of class " 164 + c, e); 165 } 166 } 167 168 } 169 | Popular Tags |