KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.InvocationTargetException JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 41285 $
42  *
43  **/

44 public class PerInstanceAdvice extends AbstractAdvice
45 {
46    private boolean initialized = false;
47    AspectDefinition aspectDefinition;
48
49    public PerInstanceAdvice(String JavaDoc adviceName, AspectDefinition a, Advisor advisor)
50    {
51       aspectDefinition = a;
52       this.adviceName = adviceName;
53       advisor.addPerInstanceAspect(a);
54    }
55
56    public String JavaDoc getName()
57    {
58       return aspectDefinition.getName() + "." + adviceName;
59    }
60
61    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
62    {
63       Object JavaDoc aspect = null;
64       if (invocation instanceof CallerInvocation)
65       {
66          //TODO: Naive implementation. Ideally callers should be able to look up the aspect by target instance
67
//to make sure that there is only one instance per target rather than caller
68
Object JavaDoc callingObject = null;
69
70          if (invocation instanceof ConstructorCalledByMethodInvocation)
71          {
72             callingObject = ((ConstructorCalledByMethodInvocation)invocation).getCallingObject();
73          }
74          else if (invocation instanceof MethodCalledByMethodInvocation)
75          {
76             callingObject = ((MethodCalledByMethodInvocation)invocation).getCallingObject();
77          }
78
79          if (callingObject == null) return invocation.invokeNext(); // called from static method
80

81          Advised advised = (Advised) callingObject;
82          InstanceAdvisor advisor = advised._getInstanceAdvisor();
83          aspect = advisor.getPerInstanceAspect(aspectDefinition);
84       }
85       else
86       {
87           Object JavaDoc targetObject = invocation.getTargetObject();
88           if (targetObject == null) return invocation.invokeNext(); // static method call or static field call
89

90           InstanceAdvisor instanceAdvisor = null;
91           if (targetObject instanceof Advised)
92           {
93              Advised advised = (Advised)targetObject;
94              instanceAdvisor = advised._getInstanceAdvisor();
95           }
96           else
97           {
98              Advisor advisor = invocation.getAdvisor();
99              if (advisor == null)
100              {
101                 return invocation.invokeNext();
102              }
103              else if (advisor instanceof InstanceAdvisor)
104              {
105                 instanceAdvisor = (InstanceAdvisor) advisor;
106              }
107              else if (advisor instanceof ClassProxyContainer && invocation instanceof ContainerProxyMethodInvocation)
108              {
109                 ContainerProxyMethodInvocation pi = (ContainerProxyMethodInvocation)invocation;
110                 instanceAdvisor = pi.getProxy().getInstanceAdvisor();
111              }
112              else
113              {
114                 return invocation.invokeNext();
115              }
116           }
117           aspect = instanceAdvisor.getPerInstanceAspect(aspectDefinition);
118       }
119       
120       if (!initialized)
121       {
122          init(adviceName, aspect.getClass());
123          initialized = true;
124       }
125       Method JavaDoc advice = resolveAdvice(invocation);
126       Object JavaDoc[] args = {invocation};
127
128       try
129       {
130          return advice.invoke(aspect, args);
131       }
132       catch (InvocationTargetException JavaDoc e)
133       {
134          throw e.getCause(); //To change body of catch statement use Options | File Templates.
135
}
136    }
137
138 }
139
Popular Tags