KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > advice > GenericAspectFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.advice;
23
24 import java.beans.BeanInfo JavaDoc;
25 import java.beans.IntrospectionException JavaDoc;
26 import java.beans.Introspector JavaDoc;
27 import java.beans.PropertyDescriptor JavaDoc;
28 import java.beans.PropertyEditor JavaDoc;
29 import java.beans.PropertyEditorManager JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
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 JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38
39 /**
40  *
41  *
42  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
43  * @version $Revision: 56777 $
44  */

45 public class GenericAspectFactory extends AspectFactoryWithClassLoaderSupport
46 {
47    final static Class JavaDoc[] ADVISOR_INJECTOR_SIGNATURE = new Class JavaDoc[]{Advisor.class};
48    final static Class JavaDoc[] INSTANCE_ADVISOR_INJECTOR_SIGNATURE = new Class JavaDoc[]{InstanceAdvisor.class};
49    final static Class JavaDoc[] JOINPOINT_INJECTOR_SIGNATURE = new Class JavaDoc[]{Joinpoint.class};
50
51    private String JavaDoc classname;
52    private Element JavaDoc element;
53
54    public GenericAspectFactory(String JavaDoc classname, Element JavaDoc element)
55    {
56       this.classname = classname;
57       this.element = element;
58    }
59
60    /** Augment the PropertyEditorManager search path to incorporate the JBoss
61     * specific editors. This simply references the PropertyEditors.class to
62     * invoke its static initialization block.
63     */

64    static
65    {
66       SecurityActions.initEditors(); // I got ClassCirculatoryErrors so I don't reference PropertyEditors.class in jboss common
67
}
68
69
70    public String JavaDoc getClassname()
71    {
72       return classname;
73    }
74
75    public void setClassname(String JavaDoc classname)
76    {
77       this.classname = classname;
78    }
79
80    public String JavaDoc getName()
81    {
82       return classname;
83    }
84
85    public Element JavaDoc getElement()
86    {
87       return element;
88    }
89
90    public void setElement(Element JavaDoc element)
91    {
92       this.element = element;
93    }
94
95    public Class JavaDoc getClazz()
96    {
97       try
98       {
99          return loadClass(classname);
100       }
101       catch (ClassNotFoundException JavaDoc e)
102       {
103          throw new RuntimeException JavaDoc(e);
104       }
105    }
106
107    public Object JavaDoc createPerVM()
108    {
109       try
110       {
111          Object JavaDoc aspect = getClazz().newInstance();
112          configureInstance(aspect, null, null, null);
113          return aspect;
114       }
115       catch (Exception JavaDoc re)
116       {
117          if (re instanceof RuntimeException JavaDoc) throw (RuntimeException JavaDoc) re;
118          throw new RuntimeException JavaDoc(re);
119       }
120    }
121
122    public Object JavaDoc createPerClass(Advisor advisor)
123    {
124       try
125       {
126          Object JavaDoc aspect = getClazz().newInstance();
127          configureInstance(aspect, advisor, null, null);
128          return aspect;
129       }
130       catch (Exception JavaDoc re)
131       {
132          if (re instanceof RuntimeException JavaDoc) throw (RuntimeException JavaDoc) re;
133          throw new RuntimeException JavaDoc(re);
134       }
135    }
136
137    public Object JavaDoc createPerInstance(Advisor advisor, InstanceAdvisor instanceAdvisor)
138    {
139       try
140       {
141          Object JavaDoc aspect = getClazz().newInstance();
142          configureInstance(aspect, advisor, instanceAdvisor, null);
143          return aspect;
144       }
145       catch (Exception JavaDoc re)
146       {
147          if (re instanceof RuntimeException JavaDoc) throw (RuntimeException JavaDoc) re;
148          throw new RuntimeException JavaDoc(re);
149       }
150    }
151
152    public Object JavaDoc createPerJoinpoint(Advisor advisor, Joinpoint jp)
153    {
154       try
155       {
156          Object JavaDoc aspect = getClazz().newInstance();
157          configureInstance(aspect, advisor, null, jp);
158          return aspect;
159       }
160       catch (Exception JavaDoc re)
161       {
162          if (re instanceof RuntimeException JavaDoc) throw (RuntimeException JavaDoc) re;
163          throw new RuntimeException JavaDoc(re);
164       }
165    }
166
167    public Object JavaDoc createPerJoinpoint(Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp)
168    {
169       try
170       {
171          Object JavaDoc aspect = getClazz().newInstance();
172          configureInstance(aspect, advisor, instanceAdvisor, jp);
173          return aspect;
174       }
175       catch (Exception JavaDoc re)
176       {
177          if (re instanceof RuntimeException JavaDoc) throw (RuntimeException JavaDoc) re;
178          throw new RuntimeException JavaDoc(re);
179       }
180    }
181
182    protected void configureInstance(Object JavaDoc instance, Advisor advisor, InstanceAdvisor instanceAdvisor, Joinpoint jp)
183    {
184       if (element == null) return;
185       BeanInfo JavaDoc beanInfo = null;
186       try
187       {
188          beanInfo = Introspector.getBeanInfo(instance.getClass());
189       }
190       catch (IntrospectionException JavaDoc e)
191       {
192          throw new RuntimeException JavaDoc(e.getMessage(), e);
193       }
194       PropertyDescriptor JavaDoc[] descriptors = beanInfo.getPropertyDescriptors();
195       if (descriptors == null)
196       {
197          descriptors = new PropertyDescriptor JavaDoc[0];
198       }
199
200       NodeList JavaDoc 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 JavaDoc element = (Element JavaDoc) children.item(i);
207             String JavaDoc tagname = element.getTagName();
208
209             String JavaDoc attributeName = element.getAttribute("name");
210
211             if (tagname.equals("attribute"))
212             {
213                String JavaDoc 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 JavaDoc instance, PropertyDescriptor JavaDoc[] descriptors, String JavaDoc attributeName, String JavaDoc 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 JavaDoc typeClass = descriptors[i].getPropertyType();
242
243             Object JavaDoc value;
244             PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(typeClass);
245             if (editor == null)
246             {
247                throw new RuntimeException JavaDoc
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 JavaDoc[]{value});
257             }
258             catch (Exception JavaDoc e)
259             {
260                throw new RuntimeException JavaDoc("Error setting attribute '" +
261                        attributeName + "' in " + classname, e);
262             }
263             break;
264          }
265       }//for descriptors
266

267       if (!foundProperty)
268       {
269          throw new RuntimeException JavaDoc("Could not find attribute '" + attributeName
270                  + "' in aspect/interceptor class " + classname);
271       }
272    }
273
274    protected void injectAdvisor(Object JavaDoc instance, Advisor advisor, String JavaDoc 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 JavaDoc injector = getInjectorName(attributeName);
286       try
287       {
288          Method JavaDoc m = instance.getClass().getMethod(injector, ADVISOR_INJECTOR_SIGNATURE);
289          m.invoke(instance, new Object JavaDoc[]{advisor});
290       }
291       catch (Exception JavaDoc e)
292       {
293          throw new RuntimeException JavaDoc("Aspect/interceptor " + classname + " does not contain a public org.jboss.aop.Advisor injector called " + injector);
294       }
295    }
296
297    protected void injectJoinpoint(Object JavaDoc instance, Joinpoint jp, String JavaDoc 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 JavaDoc injector = getInjectorName(attributeName);
309       try
310       {
311          Method JavaDoc m = instance.getClass().getMethod(injector, JOINPOINT_INJECTOR_SIGNATURE);
312          m.invoke(instance, new Object JavaDoc[]{jp});
313       }
314       catch (Exception JavaDoc e)
315       {
316          throw new RuntimeException JavaDoc("Aspect/interceptor " + classname + " does not contain a public org.jboss.aop.Joinpoint injector called " + injector);
317       }
318    }
319
320    protected void injectInstanceAdvisor(Object JavaDoc instance, InstanceAdvisor instanceAdvisor, String JavaDoc 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 JavaDoc injector = getInjectorName(attributeName);
332       try
333       {
334          Method JavaDoc m = instance.getClass().getMethod(injector, INSTANCE_ADVISOR_INJECTOR_SIGNATURE);
335          m.invoke(instance, new Object JavaDoc[]{instanceAdvisor});
336       }
337       catch (Exception JavaDoc e)
338       {
339          throw new RuntimeException JavaDoc("Aspect/interceptor " + classname + " does not contain a public org.jboss.aop.InstanceAdvisor injector called " + injector);
340       }
341    }
342
343    protected String JavaDoc getInjectorName(String JavaDoc attributeName)
344    {
345       //For now assume that the Attribute name begins with upper case
346
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