KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
25
26 import org.jboss.aop.Advisor;
27 import org.jboss.aop.ClassAdvisor;
28 import org.jboss.aop.GeneratedClassAdvisor;
29 import org.jboss.aop.joinpoint.FieldJoinpoint;
30 import org.jboss.aop.joinpoint.Joinpoint;
31
32 /**
33  * Comment
34  *
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 37406 $
37  */

38 public class ScopedInterceptorFactory implements InterceptorFactory
39 {
40    private AspectDefinition aspect;
41
42    public ScopedInterceptorFactory(AspectDefinition aspect)
43    {
44       this.aspect = aspect;
45    }
46
47    public AspectDefinition getAspect()
48    {
49       return aspect;
50    }
51
52    public String JavaDoc getAdvice()
53    {
54       return "invoke";
55    }
56
57    public boolean isDeployed()
58    {
59       return aspect.isDeployed();
60    }
61
62    public Interceptor create(Advisor advisor, Joinpoint joinpoint)
63    {
64       if (aspect.getScope() == Scope.PER_VM)
65       {
66          return (Interceptor) advisor.getManager().getPerVMAspect(aspect);
67       }
68       else if (aspect.getScope() == Scope.PER_CLASS)
69       {
70          Interceptor interceptor = advisor.getAdviceInterceptor(aspect, "invoke", null);
71          if (interceptor != null) return interceptor;
72          advisor.addPerClassAspect(aspect);
73          interceptor = (Interceptor)advisor.getPerClassAspect(aspect);
74          advisor.addAdviceInterceptor(aspect, "invoke", interceptor, null);
75          return interceptor;
76          
77       }
78       else if (aspect.getScope() == Scope.PER_INSTANCE)
79       {
80          return new PerInstanceInterceptor(aspect, advisor);
81       }
82       else if (aspect.getScope() == Scope.PER_JOINPOINT)
83       {
84          try
85          {
86             return PerJoinpointInterceptor.createInterceptor(advisor, joinpoint, aspect);
87          }
88          catch (Exception JavaDoc e)
89          {
90             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
91
}
92       }
93       else if (aspect.getScope() == Scope.PER_CLASS_JOINPOINT)
94       {
95          if (advisor instanceof GeneratedClassAdvisor)
96          {
97             //Generated advisors handle PER_CLASS_JOINPOINT slightly differently
98
Object JavaDoc icptr = ((GeneratedClassAdvisor)advisor).getPerClassJoinpointAspect(aspect, joinpoint);
99             if (icptr == null)
100             {
101                ((GeneratedClassAdvisor)advisor).addPerClassJoinpointAspect(aspect, joinpoint);
102                icptr = ((GeneratedClassAdvisor)advisor).getPerClassJoinpointAspect(aspect, joinpoint);
103             }
104             return (Interceptor)icptr;
105          }
106          else if (joinpoint instanceof FieldJoinpoint)
107          {
108             FieldJoinpoint field = (FieldJoinpoint) joinpoint;
109             ClassAdvisor classAdvisor = (ClassAdvisor) advisor;
110             return (Interceptor) classAdvisor.getFieldAspect(field, aspect);
111          }
112          
113          return (Interceptor) aspect.getFactory().createPerJoinpoint(advisor, joinpoint);
114       }
115       else
116       {
117          //if (aspect.getScope() == null) System.err.println("scope is null: " + aspect.getName() + "." + advice);
118
}
119       return null;
120    }
121
122    public String JavaDoc getName()
123    {
124       return aspect.getName();
125    }
126    
127    public boolean equals(Object JavaDoc obj)
128    {
129       if (this == obj) return true;
130       if (!(obj instanceof ScopedInterceptorFactory)) return false;
131       
132       AspectDefinition otherAspect = ((GenericInterceptorFactory)obj).getAspect();
133       
134       if (!this.aspect.getName().equals(otherAspect.getName())) return false;
135       if (!this.aspect.getFactory().getName().equals(otherAspect.getFactory().getName()))return false;
136
137       return true;
138    }
139 }
140
Popular Tags