KickJava   Java API By Example, From Geeks To Geeks.

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


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.InstanceAdvisor;
27 import org.jboss.aop.joinpoint.CallerInvocation;
28 import org.jboss.aop.joinpoint.ConstructorCalledByMethodInvocation;
29 import org.jboss.aop.joinpoint.Invocation;
30 import org.jboss.aop.joinpoint.MethodCalledByMethodInvocation;
31 import org.jboss.aop.proxy.container.ClassProxyContainer;
32 import org.jboss.aop.proxy.container.ContainerProxyMethodInvocation;
33
34 /**
35  * Comment
36  *
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @version $Revision: 41231 $
39  */

40 public class PerInstanceInterceptor implements Interceptor
41 {
42    AspectDefinition aspectDefinition;
43
44    public PerInstanceInterceptor(AspectDefinition a, Advisor advisor)
45    {
46       aspectDefinition = a;
47       advisor.addPerInstanceAspect(a);
48    }
49
50    public String JavaDoc getName()
51    {
52       return aspectDefinition.getName();
53    }
54
55    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
56    {
57       if (invocation instanceof CallerInvocation)
58       {
59          //TODO: Naive implementation. Ideally callers should be able to look up the aspect by target instance
60
//to make sure that there is only one instance per target rather than caller
61
Object JavaDoc callingObject = null;
62
63          if (invocation instanceof ConstructorCalledByMethodInvocation)
64          {
65             callingObject = ((ConstructorCalledByMethodInvocation)invocation).getCallingObject();
66          }
67          else if (invocation instanceof MethodCalledByMethodInvocation)
68          {
69             callingObject = ((MethodCalledByMethodInvocation)invocation).getCallingObject();
70          }
71
72          if (callingObject == null) return invocation.invokeNext(); // called from static method
73

74          Advised advised = (Advised) callingObject;
75          InstanceAdvisor advisor = advised._getInstanceAdvisor();
76          Interceptor interceptor = (Interceptor) advisor.getPerInstanceAspect(aspectDefinition);
77          return interceptor.invoke(invocation);
78          
79       }
80       else
81       {
82          Object JavaDoc targetObject = invocation.getTargetObject();
83          if (targetObject == null) return invocation.invokeNext(); // static method call or static field call
84

85          InstanceAdvisor instanceAdvisor = null;
86          if (targetObject instanceof Advised)
87          {
88             Advised advised = (Advised) targetObject;
89             instanceAdvisor = advised._getInstanceAdvisor();
90          }
91          else
92          {
93             Advisor advisor = invocation.getAdvisor();
94             if (advisor == null)
95             {
96                return invocation.invokeNext();
97             }
98             
99             if (advisor instanceof InstanceAdvisor)
100             {
101                instanceAdvisor = (InstanceAdvisor) advisor;
102             }
103             else
104             {
105                if (advisor instanceof ClassProxyContainer && invocation instanceof ContainerProxyMethodInvocation)
106                {
107                   ContainerProxyMethodInvocation pi = (ContainerProxyMethodInvocation)invocation;
108                   instanceAdvisor = pi.getProxy().getInstanceAdvisor();
109                }
110                else
111                {
112                   return invocation.invokeNext();
113                }
114             }
115          }
116          Interceptor interceptor = (Interceptor) instanceAdvisor.getPerInstanceAspect(aspectDefinition);
117          return interceptor.invoke(invocation);
118       }
119    }
120
121 }
122
Popular Tags