1 16 package org.directwebremoting.impl; 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.TreeMap ; 25 26 import javax.servlet.Servlet ; 27 28 import org.directwebremoting.Container; 29 import org.directwebremoting.util.LocalUtil; 30 import org.directwebremoting.util.Logger; 31 32 40 public class DefaultContainer extends AbstractContainer implements Container 41 { 42 49 public void addParameter(Object askFor, Object valueParam) throws InstantiationException , IllegalAccessException 50 { 51 Object value = valueParam; 52 53 if (value instanceof String ) 55 { 56 try 57 { 58 Class impl = LocalUtil.classForName((String ) value); 59 value = impl.newInstance(); 60 } 61 catch (ClassNotFoundException ex) 62 { 63 } 65 } 66 67 if (!(value instanceof String ) && askFor instanceof String ) 70 { 71 try 72 { 73 Class iface = LocalUtil.classForName((String ) askFor); 74 if (!iface.isAssignableFrom(value.getClass())) 75 { 76 log.error("Can't cast: " + value + " to " + askFor); 77 } 78 } 79 catch (ClassNotFoundException ex) 80 { 81 } 83 } 84 85 if (log.isDebugEnabled()) 86 { 87 if (value instanceof String ) 88 { 89 log.debug("Adding IoC setting: " + askFor + "=" + value); 90 } 91 else 92 { 93 log.debug("Adding IoC implementation: " + askFor + "=" + value.getClass().getName()); 94 } 95 } 96 97 beans.put(askFor, value); 98 } 99 100 110 public void setupFinished() 111 { 112 for (Iterator it = beans.entrySet().iterator(); it.hasNext();) 114 { 115 Map.Entry entry = (Map.Entry ) it.next(); 116 Object ovalue = entry.getValue(); 118 119 if (!(ovalue instanceof String )) 120 { 121 log.debug("Trying to autowire: " + ovalue.getClass().getName()); 122 123 Method [] methods = ovalue.getClass().getMethods(); 124 methods: 125 for (int i = 0; i < methods.length; i++) 126 { 127 Method setter = methods[i]; 128 129 if (setter.getName().startsWith("set") && 130 setter.getName().length() > 3 && 131 setter.getParameterTypes().length == 1) 132 { 133 String name = Character.toLowerCase(setter.getName().charAt(3)) + setter.getName().substring(4); 134 Class propertyType = setter.getParameterTypes()[0]; 135 136 Object setting = beans.get(name); 138 if (setting != null) 139 { 140 if (propertyType.isAssignableFrom(setting.getClass())) 141 { 142 log.debug("- autowire-by-name: " + name + "=" + setting); 143 invoke(setter, ovalue, setting); 144 145 continue methods; 146 } 147 else if (setting.getClass() == String .class) 148 { 149 try 150 { 151 Object value = LocalUtil.simpleConvert((String ) setting, propertyType); 152 153 log.debug("- autowire-by-name: " + name + "=" + value); 154 invoke(setter, ovalue, value); 155 } 156 catch (IllegalArgumentException ex) 157 { 158 } 160 161 continue methods; 162 } 163 } 164 165 Object value = beans.get(propertyType.getName()); 167 if (value != null) 168 { 169 log.debug("- autowire-by-type: " + name + "=" + value.getClass().getName()); 170 invoke(setter, ovalue, value); 171 172 continue methods; 173 } 174 175 log.debug("- skipped autowire: " + name); 176 } 177 } 178 } 179 } 180 181 callInitializingBeans(); 182 } 183 184 191 private static void invoke(Method setter, Object bean, Object value) 192 { 193 try 194 { 195 setter.invoke(bean, new Object [] { value }); 196 } 197 catch (IllegalArgumentException ex) 198 { 199 log.error("- Internal error: " + ex.getMessage()); 200 } 201 catch (IllegalAccessException ex) 202 { 203 log.error("- Permission error: " + ex.getMessage()); 204 } 205 catch (InvocationTargetException ex) 206 { 207 log.error("- Exception during auto-wire: ", ex.getTargetException()); 208 } 209 } 210 211 214 public Object getBean(String id) 215 { 216 Object reply = beans.get(id); 217 if (reply == null) 218 { 219 log.debug("DefaultContainer: No bean with id=" + id); 220 } 221 222 return reply; 223 } 224 225 228 public Collection getBeanNames() 229 { 230 return Collections.unmodifiableCollection(beans.keySet()); 231 } 232 233 236 protected Map beans = new TreeMap (); 237 238 241 private static final Logger log = Logger.getLogger(DefaultContainer.class); 242 } 243 | Popular Tags |