1 22 package org.jboss.aop.advice; 23 24 import java.beans.BeanInfo ; 25 import java.beans.IntrospectionException ; 26 import java.beans.Introspector ; 27 import java.beans.PropertyDescriptor ; 28 import java.beans.PropertyEditor ; 29 import java.beans.PropertyEditorManager ; 30 import java.lang.reflect.Method ; 31 import org.jboss.aop.Advisor; 32 import org.jboss.aop.AspectManager; 33 import org.jboss.aop.InstanceAdvisor; 34 import org.jboss.aop.joinpoint.Joinpoint; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 39 45 public class GenericAspectFactory extends AspectFactoryWithClassLoaderSupport 46 { 47 final static Class [] ADVISOR_INJECTOR_SIGNATURE = new Class []{Advisor.class}; 48 final static Class [] INSTANCE_ADVISOR_INJECTOR_SIGNATURE = new Class []{InstanceAdvisor.class}; 49 final static Class [] JOINPOINT_INJECTOR_SIGNATURE = new Class []{Joinpoint.class}; 50 51 private String classname; 52 private Element element; 53 54 public GenericAspectFactory(String classname, Element element) 55 { 56 this.classname = classname; 57 this.element = element; 58 } 59 60 64 static 65 { 66 SecurityActions.initEditors(); } 68 69 70 public String getClassname() 71 { 72 return classname; 73 } 74 75 public void setClassname(String classname) 76 { 77 this.classname = classname; 78 } 79 80 public String getName() 81 { 82 return classname; 83 } 84 85 public Element getElement() 86 { 87 return element; 88 } 89 90 public void setElement(Element element) 91 { 92 this.element = element; 93 } 94 95 public Class getClazz() 96 { 97 try 98 { 99 return loadClass(classname); 100 } 101 catch (ClassNotFoundException e) 102 { 103 throw new RuntimeException (e); 104 } 105 } 106 107 public Object createPerVM() 108 { 109 try 110 { 111 Object aspect = getClazz().newInstance(); 112 configureInstance(aspect, null, null, null); 113 return aspect; 114 } 115 catch (Exception re) 116 { 117 if (re instanceof RuntimeException ) throw (RuntimeException ) re; 118 throw new RuntimeException (re); 119 } 120 } 121 122 public Object createPerClass(Advisor advisor) 123 { 124 try 125 { 126 Object aspect = getClazz().newInstance(); 127 configureInstance(aspect, advisor, null, null); 128 return aspect; 129 } 130 catch (Exception re) 131 { 132 if (re instanceof RuntimeException ) throw (RuntimeException ) re; 133 throw new RuntimeException (re); 134 } 135 } 136 137 public Object createPerInstance(Advisor advisor, InstanceAdvisor instanceAdvisor) 138 { 139 try 140 { 141 Object aspect = getClazz().newInstance(); 142 configureInstance(aspect, advisor, instanceAdvisor, null); 143 return aspect; 144 } 145 catch (Exception re) 146 { 147 if (re instanceof RuntimeException ) throw (RuntimeException ) re; 148 throw new RuntimeException (re); 149 } 150 } 151 152 public Object createPerJoinpoint(Advisor advisor, Joinpoint jp) 153 { 154 try 155 { 156 Object aspect = getClazz().newInstance(); 157 configureInstance(aspect, advisor, null, jp); 158 return aspect; 159 } 160 catch (Exception re) 161 { 162 if (re instanceof RuntimeException ) throw (RuntimeException ) re; 163 throw new RuntimeException (re); 164 } 165 } 166 167 public Object createPerJoinpoint(Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp) 168 { 169 try 170 { 171 Object aspect = getClazz().newInstance(); 172 configureInstance(aspect, advisor, instanceAdvisor, jp); 173 return aspect; 174 } 175 catch (Exception re) 176 { 177 if (re instanceof RuntimeException ) throw (RuntimeException ) re; 178 throw new RuntimeException (re); 179 } 180 } 181 182 protected void configureInstance(Object instance, Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp) 183 { 184 if (element == null) return; 185 BeanInfo beanInfo = null; 186 try 187 { 188 beanInfo = Introspector.getBeanInfo(instance.getClass()); 189 } 190 catch (IntrospectionException e) 191 { 192 throw new RuntimeException (e.getMessage(), e); 193 } 194 PropertyDescriptor [] descriptors = beanInfo.getPropertyDescriptors(); 195 if (descriptors == null) 196 { 197 descriptors = new PropertyDescriptor [0]; 198 } 199 200 NodeList children = element.getChildNodes(); 201 202 for (int i = 0; i < children.getLength(); i++) 203 { 204 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) 205 { 206 Element element = (Element ) children.item(i); 207 String tagname = element.getTagName(); 208 209 String attributeName = element.getAttribute("name"); 210 211 if (tagname.equals("attribute")) 212 { 213 String attributeText = element.getFirstChild().getNodeValue(); 214 setAttribute(instance, descriptors, attributeName, attributeText); 215 } 216 else if (tagname.equals("advisor-attribute")) 217 { 218 injectAdvisor(instance, advisor, attributeName); 219 } 220 else if (tagname.equals("joinpoint-attribute")) 221 { 222 injectJoinpoint(instance, jp, attributeName); 223 } 224 else if (tagname.equals("instance-advisor-attribute")) 225 { 226 injectInstanceAdvisor(instance, instanceAdvisor, attributeName); 227 } 228 } 229 } 230 } 231 232 protected void setAttribute(Object instance, PropertyDescriptor [] descriptors, String attributeName, String attributeText) 233 { 234 boolean foundProperty = false; 235 236 for (int i = 0; i < descriptors.length; i++) 237 { 238 if (attributeName.equalsIgnoreCase(descriptors[i].getName())) 239 { 240 foundProperty = true; 241 Class typeClass = descriptors[i].getPropertyType(); 242 243 Object value; 244 PropertyEditor editor = PropertyEditorManager.findEditor(typeClass); 245 if (editor == null) 246 { 247 throw new RuntimeException 248 ("No property editor for attribute: " + attributeName + 249 "; type=" + typeClass + " in " + classname); 250 } 251 252 editor.setAsText(attributeText); 253 value = editor.getValue(); 254 try 255 { 256 descriptors[i].getWriteMethod().invoke(instance, new Object []{value}); 257 } 258 catch (Exception e) 259 { 260 throw new RuntimeException ("Error setting attribute '" + 261 attributeName + "' in " + classname, e); 262 } 263 break; 264 } 265 } 267 if (!foundProperty) 268 { 269 throw new RuntimeException ("Could not find attribute '" + attributeName 270 + "' in aspect/interceptor class " + classname); 271 } 272 } 273 274 protected void injectAdvisor(Object instance, Advisor advisor, String attributeName) 275 { 276 if (advisor == null) 277 { 278 if (AspectManager.verbose) 279 { 280 System.out.println("[warn] Ignoring attempt to set advisor attribute on PER_VM scoped aspect/interceptor: " + classname); 281 } 282 return; 283 } 284 285 String injector = getInjectorName(attributeName); 286 try 287 { 288 Method m = instance.getClass().getMethod(injector, ADVISOR_INJECTOR_SIGNATURE); 289 m.invoke(instance, new Object []{advisor}); 290 } 291 catch (Exception e) 292 { 293 throw new RuntimeException ("Aspect/interceptor " + classname + " does not contain a public org.jboss.aop.Advisor injector called " + injector); 294 } 295 } 296 297 protected void injectJoinpoint(Object instance, Joinpoint jp, String attributeName) 298 { 299 if (jp == null) 300 { 301 if (AspectManager.verbose) 302 { 303 System.out.println("[warn] Ignoring attempt to set joinpoint attribute on aspect/interceptor: " + classname + " which is not scoped PER_JOINPOINT"); 304 } 305 return; 306 } 307 308 String injector = getInjectorName(attributeName); 309 try 310 { 311 Method m = instance.getClass().getMethod(injector, JOINPOINT_INJECTOR_SIGNATURE); 312 m.invoke(instance, new Object []{jp}); 313 } 314 catch (Exception e) 315 { 316 throw new RuntimeException ("Aspect/interceptor " + classname + " does not contain a public org.jboss.aop.Joinpoint injector called " + injector); 317 } 318 } 319 320 protected void injectInstanceAdvisor(Object instance, InstanceAdvisor instanceAdvisor, String attributeName) 321 { 322 if (instanceAdvisor == null) 323 { 324 if (AspectManager.verbose) 325 { 326 System.out.println("[warn] Ignoring attempt to set instance advisor attribute on aspect/interceptor: " + classname + " which is not scoped PER_INSTANCE or PER_JOINPOINT"); 327 } 328 return; 329 } 330 331 String injector = getInjectorName(attributeName); 332 try 333 { 334 Method m = instance.getClass().getMethod(injector, INSTANCE_ADVISOR_INJECTOR_SIGNATURE); 335 m.invoke(instance, new Object []{instanceAdvisor}); 336 } 337 catch (Exception e) 338 { 339 throw new RuntimeException ("Aspect/interceptor " + classname + " does not contain a public org.jboss.aop.InstanceAdvisor injector called " + injector); 340 } 341 } 342 343 protected String getInjectorName(String attributeName) 344 { 345 char firstChar = attributeName.charAt(0); 347 if (Character.isLowerCase(firstChar)) 348 { 349 attributeName = Character.toUpperCase(firstChar) + attributeName.substring(1); 350 } 351 352 return "set" + attributeName; 353 } 354 } 355 356 | Popular Tags |