KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 public class AdviceFactory implements InterceptorFactory
37 {
38    private String JavaDoc advice;
39    private AspectDefinition aspect;
40
41    public AdviceFactory(AspectDefinition aspect, String JavaDoc adviceName)
42    {
43       this.aspect = aspect;
44       this.advice = adviceName;
45    }
46
47    public AspectDefinition getAspect()
48    {
49       return aspect;
50    }
51
52    public String JavaDoc getAdvice()
53    {
54       return advice;
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          try
67          {
68             return PerVmAdvice.generateOptimized(joinpoint, advisor.getManager(), advice, aspect);
69          }
70          catch (Exception JavaDoc e)
71          {
72             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
73
}
74       }
75       else if (aspect.getScope() == Scope.PER_CLASS)
76       {
77          try
78          {
79             return PerClassAdvice.generate(joinpoint, advisor, advice, aspect);
80          }
81          catch (Exception JavaDoc e)
82          {
83             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
84
}
85       }
86       else if (aspect.getScope() == Scope.PER_INSTANCE)
87       {
88          return new PerInstanceAdvice(advice, aspect, advisor);
89       }
90       else if (aspect.getScope() == Scope.PER_JOINPOINT)
91       {
92          try
93          {
94             return PerJoinpointAdvice.createInterceptor(advisor, joinpoint, aspect, advice);
95          }
96          catch (Exception JavaDoc e)
97          {
98             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
99
}
100       }
101       else if (aspect.getScope() == Scope.PER_CLASS_JOINPOINT)
102       {
103          Object JavaDoc instance = null;
104          
105          if (advisor instanceof GeneratedClassAdvisor)
106          {
107             //Generated advisors handle PER_CLASS_JOINPOINT slightly differently
108
instance = ((GeneratedClassAdvisor)advisor).getPerClassJoinpointAspect(aspect, joinpoint);
109             if (instance == null)
110             {
111                ((GeneratedClassAdvisor)advisor).addPerClassJoinpointAspect(aspect, joinpoint);
112                instance = ((GeneratedClassAdvisor)advisor).getPerClassJoinpointAspect(aspect, joinpoint);
113             }
114          }
115          else if (joinpoint instanceof FieldJoinpoint)
116          {
117             FieldJoinpoint field = (FieldJoinpoint) joinpoint;
118             ClassAdvisor classAdvisor = (ClassAdvisor) advisor;
119             instance = classAdvisor.getFieldAspect(field, aspect);
120          }
121          else
122          {
123             instance = aspect.getFactory().createPerJoinpoint(advisor, joinpoint);
124          }
125          
126          try
127          {
128             return PerVmAdvice.generateInterceptor(joinpoint, instance, advice);
129          }
130          catch (Exception JavaDoc e)
131          {
132             throw new RuntimeException JavaDoc(e);
133          }
134       }
135       else
136       {
137          //if (aspect.getScope() == null) System.err.println("scope is null: " + aspect.getName() + "." + advice);
138
}
139       return null;
140    }
141
142    public String JavaDoc getName()
143    {
144       return aspect.getName() + "." + advice;
145    }
146
147    public boolean equals(Object JavaDoc obj)
148    {
149       if (this == obj) return true;
150       if (!(obj instanceof AdviceFactory)) return false;
151       
152       AspectDefinition otherAspect = ((AdviceFactory)obj).getAspect();
153       
154       if (!this.aspect.getName().equals(otherAspect.getName())) return false;
155       if (!this.aspect.getFactory().getName().equals(otherAspect.getFactory().getName()))return false;
156       if (!this.advice.equals(((AdviceFactory)obj).advice))return false;
157
158       return true;
159    }
160 }
161
Popular Tags