KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.aop.Advised;
25 import org.jboss.aop.Advisor;
26 import org.jboss.aop.ClassAdvisor;
27 import org.jboss.aop.InstanceAdvisor;
28 import org.jboss.aop.joinpoint.CallerInvocation;
29 import org.jboss.aop.joinpoint.ConstructorCalledByConstructorJoinpoint;
30 import org.jboss.aop.joinpoint.ConstructorCalledByMethodInvocation;
31 import org.jboss.aop.joinpoint.ConstructorCalledByMethodJoinpoint;
32 import org.jboss.aop.joinpoint.ConstructorJoinpoint;
33 import org.jboss.aop.joinpoint.FieldJoinpoint;
34 import org.jboss.aop.joinpoint.Invocation;
35 import org.jboss.aop.joinpoint.Joinpoint;
36 import org.jboss.aop.joinpoint.MethodCalledByConstructorJoinpoint;
37 import org.jboss.aop.joinpoint.MethodCalledByMethodInvocation;
38 import org.jboss.aop.joinpoint.MethodCalledByMethodJoinpoint;
39 import org.jboss.aop.joinpoint.MethodJoinpoint;
40
41 import java.lang.reflect.InvocationTargetException JavaDoc;
42 import java.lang.reflect.Method JavaDoc;
43 import java.lang.reflect.Modifier JavaDoc;
44
45 /**
46  * Comment
47  *
48  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
49  * @version $Revision: 37406 $
50  */

51 public class PerJoinpointAdvice extends AbstractAdvice
52 {
53    public static Interceptor createInterceptor(Advisor advisor, Joinpoint joinpoint, AspectDefinition def, String JavaDoc adviceName) throws Exception JavaDoc
54    {
55       if (joinpoint instanceof MethodJoinpoint)
56       {
57          MethodJoinpoint method = (MethodJoinpoint) joinpoint;
58          if (Modifier.isStatic(method.getMethod().getModifiers()))
59          {
60             return PerVmAdvice.generateInterceptor(joinpoint, def.getFactory().createPerJoinpoint(advisor, joinpoint), adviceName);
61          }
62       }
63       else if (joinpoint instanceof ConstructorJoinpoint
64       || joinpoint instanceof ConstructorCalledByConstructorJoinpoint
65       || joinpoint instanceof MethodCalledByConstructorJoinpoint)
66       {
67          return PerVmAdvice.generateInterceptor(joinpoint, def.getFactory().createPerJoinpoint(advisor, joinpoint), adviceName);
68       }
69       else if (joinpoint instanceof MethodCalledByMethodJoinpoint)
70       {
71          MethodCalledByMethodJoinpoint method = (MethodCalledByMethodJoinpoint) joinpoint;
72          if (Modifier.isStatic(method.getCalling().getModifiers()))
73          {
74             return PerVmAdvice.generateInterceptor(joinpoint, def.getFactory().createPerJoinpoint(advisor, joinpoint), adviceName);
75          }
76       }
77       else if (joinpoint instanceof ConstructorCalledByMethodJoinpoint)
78       {
79          ConstructorCalledByMethodJoinpoint method = (ConstructorCalledByMethodJoinpoint) joinpoint;
80          if (Modifier.isStatic(method.getCalling().getModifiers()))
81          {
82             return PerVmAdvice.generateInterceptor(joinpoint, def.getFactory().createPerJoinpoint(advisor, joinpoint), adviceName);
83          }
84       }
85       else if (joinpoint instanceof FieldJoinpoint)
86       {
87          FieldJoinpoint field = (FieldJoinpoint) joinpoint;
88          if (Modifier.isStatic(field.getField().getModifiers()))
89          {
90             ClassAdvisor classAdvisor = (ClassAdvisor) advisor;
91             Object JavaDoc aspect = classAdvisor.getFieldAspect(field, def);
92             return PerVmAdvice.generateInterceptor(joinpoint, aspect, adviceName);
93          }
94       }
95       return new PerJoinpointAdvice(adviceName, def, advisor, joinpoint);
96    }
97
98    private boolean initialized = false;
99    AspectDefinition aspectDefinition;
100    Joinpoint joinpoint;
101
102    public PerJoinpointAdvice(String JavaDoc adviceName, AspectDefinition a, Advisor advisor, Joinpoint joinpoint)
103    {
104       aspectDefinition = a;
105       this.adviceName = adviceName;
106       advisor.addPerInstanceJoinpointAspect(joinpoint, a);
107       this.joinpoint = joinpoint;
108    }
109
110    public String JavaDoc getName()
111    {
112       return aspectDefinition.getName() + "." + adviceName;
113    }
114
115    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
116    {
117       Object JavaDoc aspect = null;
118       if (invocation instanceof CallerInvocation)
119       {
120          //TODO: Naive implementation. Ideally callers should be able to look up the aspect by target instance
121
//to make sure that there is only one instance per target rather than caller
122
Object JavaDoc callingObject = null;
123
124          if (invocation instanceof ConstructorCalledByMethodInvocation)
125          {
126             callingObject = ((ConstructorCalledByMethodInvocation)invocation).getCallingObject();
127          }
128          else if (invocation instanceof MethodCalledByMethodInvocation)
129          {
130             callingObject = ((MethodCalledByMethodInvocation)invocation).getCallingObject();
131          }
132
133          if (callingObject == null) return invocation.invokeNext(); // called from static method
134

135          Advised advised = (Advised) callingObject;
136          InstanceAdvisor advisor = advised._getInstanceAdvisor();
137          aspect = advisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition);
138       }
139       else
140       {
141          Object JavaDoc targetObject = invocation.getTargetObject();
142          if (targetObject == null) return invocation.invokeNext(); // static method call or static field call
143

144          Advised advised = (Advised) targetObject;
145          InstanceAdvisor advisor = advised._getInstanceAdvisor();
146          aspect = advisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition);
147       }
148       
149       if (!initialized)
150       {
151          init(adviceName, aspect.getClass());
152          initialized = true;
153       }
154       Method JavaDoc advice = resolveAdvice(invocation);
155       Object JavaDoc[] args = {invocation};
156
157       try
158       {
159          return advice.invoke(aspect, args);
160       }
161       catch (InvocationTargetException JavaDoc e)
162       {
163          throw e.getCause(); //To change body of catch statement use Options | File Templates.
164
}
165    }
166
167
168 }
169
Popular Tags