1 package org.picocontainer.defaults; 2 3 import org.picocontainer.ComponentAdapter; 4 import org.picocontainer.PicoContainer; 5 import org.picocontainer.PicoInitializationException; 6 import org.picocontainer.PicoIntrospectionException; 7 8 import java.io.File ; 9 import java.lang.reflect.Method ; 10 import java.net.MalformedURLException ; 11 import java.net.URL ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import java.util.Set ; 15 16 34 public class BeanPropertyComponentAdapter extends DecoratingComponentAdapter { 35 private Map properties; 36 private transient Map setters = null; 37 38 44 public BeanPropertyComponentAdapter(ComponentAdapter delegate) throws PicoInitializationException { 45 super(delegate); 46 } 47 48 60 public Object getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException { 61 final Object componentInstance = super.getComponentInstance(container); 62 if (setters == null) { 63 setters = new SetterIntrospector().getSetters(getComponentImplementation()); 64 } 65 66 if (properties != null) { 67 Set propertyNames = properties.keySet(); 68 for (Iterator iterator = propertyNames.iterator(); iterator.hasNext();) { 69 final String propertyName = (String ) iterator.next(); 70 final String propertyValue = (String ) properties.get(propertyName); 71 Method setter = (Method ) setters.get(propertyName); 72 Object value = null; 73 try { 74 value = convertType(container, setter, propertyValue); 75 } catch (ClassNotFoundException e) { 76 throw new PicoInvocationTargetInitializationException(e); 77 } 78 try { 79 setter.invoke(componentInstance, new Object []{value}); 80 } catch (final Exception e) { 81 throw new PicoInitializationException("Failed to set property " + propertyName + " to " + propertyValue + ": " + e.getMessage(), e); 82 } 83 } 84 } 85 return componentInstance; 86 } 87 88 private Object convertType(PicoContainer container, Method setter, String propertyValue) throws ClassNotFoundException { 89 if (propertyValue == null) { 90 return null; 91 } 92 Class type = setter.getParameterTypes()[0]; 93 String typeName = type.getName(); 94 95 Object result = convert(typeName, propertyValue, Thread.currentThread().getContextClassLoader()); 96 97 if (result == null) { 98 99 103 if (container != null) { 105 Object component = container.getComponentInstance(propertyValue); 106 if (component != null && type.isAssignableFrom(component.getClass())) { 107 return component; 108 } 109 } 110 } 111 return result; 112 } 113 114 123 public static Object convert(String typeName, String value, ClassLoader classLoader) throws ClassNotFoundException { 124 if (typeName.equals(Boolean .class.getName()) || typeName.equals(boolean.class.getName())) { 125 return Boolean.valueOf(value); 126 } else if (typeName.equals(Byte .class.getName()) || typeName.equals(byte.class.getName())) { 127 return Byte.valueOf(value); 128 } else if (typeName.equals(Short .class.getName()) || typeName.equals(short.class.getName())) { 129 return Short.valueOf(value); 130 } else if (typeName.equals(Integer .class.getName()) || typeName.equals(int.class.getName())) { 131 return Integer.valueOf(value); 132 } else if (typeName.equals(Long .class.getName()) || typeName.equals(long.class.getName())) { 133 return Long.valueOf(value); 134 } else if (typeName.equals(Float .class.getName()) || typeName.equals(float.class.getName())) { 135 return Float.valueOf(value); 136 } else if (typeName.equals(Double .class.getName()) || typeName.equals(double.class.getName())) { 137 return Double.valueOf(value); 138 } else if (typeName.equals(Character .class.getName()) || typeName.equals(char.class.getName())) { 139 return new Character (value.toCharArray()[0]); 140 } else if (typeName.equals(String .class.getName()) || typeName.equals("string")) { 141 return value; 142 } else if (typeName.equals(File .class.getName()) || typeName.equals("file")) { 143 return new File (value); 144 } else if (typeName.equals(URL .class.getName()) || typeName.equals("url")) { 145 try { 146 return new URL (value); 147 } catch (MalformedURLException e) { 148 throw new PicoInitializationException(e); 149 } 150 } else if (typeName.equals(Class .class.getName()) || typeName.equals("class")) { 151 return classLoader.loadClass(value); 152 } 153 return null; 154 } 155 156 161 public void setProperties(Map properties) { 162 this.properties = properties; 163 } 164 } | Popular Tags |