KickJava   Java API By Example, From Geeks To Geeks.

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


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.Modifier JavaDoc;
42
43 /**
44  * Comment
45  *
46  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
47  * @version $Revision: 37406 $
48  */

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

130          Advised advised = (Advised) callingObject;
131          InstanceAdvisor advisor = advised._getInstanceAdvisor();
132          Interceptor interceptor = (Interceptor) advisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition);
133          return interceptor.invoke(invocation);
134          
135       }
136       else
137       {
138          Object JavaDoc targetObject = invocation.getTargetObject();
139          if (targetObject == null) return invocation.invokeNext(); // static method call or static field call
140

141          Advised advised = (Advised) targetObject;
142          InstanceAdvisor advisor = advised._getInstanceAdvisor();
143          Interceptor interceptor = (Interceptor) advisor.getPerInstanceJoinpointAspect(joinpoint, aspectDefinition);
144          return interceptor.invoke(invocation);
145       }
146    }
147
148
149 }
150
Popular Tags