KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.StringReader JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.jboss.aop.Advisor;
34 import org.jboss.aop.AspectManager;
35 import org.jboss.aop.pointcut.Pointcut;
36 import org.jboss.aop.pointcut.PointcutExpression;
37 import org.jboss.aop.pointcut.ast.ASTCFlowExpression;
38 import org.jboss.aop.pointcut.ast.ParseException;
39 import org.jboss.aop.pointcut.ast.PointcutExpressionParser;
40
41 /**
42  * Comment
43  *
44  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
45  * @version $Revision: 42489 $
46  */

47 public class AdviceBinding
48 {
49    private static volatile long counter = 0;
50
51    protected String JavaDoc name;
52    protected Pointcut pointcut;
53    protected ASTCFlowExpression cflow;
54    protected String JavaDoc cflowString;
55
56    // not list because of redundancy caused by successive calls of ClassAdvisor.rebuildInterceptors
57
protected Collection JavaDoc advisors = new HashSet JavaDoc();
58    protected InterceptorFactory[] interceptorFactories = new InterceptorFactory[0];
59
60    public AdviceBinding() {}
61
62    public AdviceBinding(String JavaDoc name, Pointcut p, ASTCFlowExpression cflow, String JavaDoc cflowString, InterceptorFactory[] factories) throws ParseException
63    {
64       this.name = name;
65       interceptorFactories = factories;
66       this.cflow = cflow;
67
68       pointcut = p;
69       this.cflowString = cflowString;
70    }
71
72    /**
73     * This constructor is used for creation of AdviceBinding programmatically
74     *
75     * @param pointcutExpression
76     * @param cflow
77     * @throws ParseException
78     */

79    public AdviceBinding(String JavaDoc pointcutExpression, String JavaDoc cflow) throws ParseException
80    {
81       this(Long.toString(System.currentTimeMillis()) + ":" + Long.toString(counter++), pointcutExpression, cflow);
82    }
83
84    /**
85     * This constructor is used for creation of AdviceBinding programmatically
86     *
87     * @param pointcutExpression
88     * @param cflow
89     * @throws ParseException
90     */

91    public AdviceBinding(String JavaDoc name, String JavaDoc pointcutExpression, String JavaDoc cflow) throws ParseException
92    {
93       this.name = name;
94       setPointcutExpression(pointcutExpression);
95       setCFlowExpression(cflow);
96       interceptorFactories = new InterceptorFactory[0];
97    }
98
99    public void setCFlowExpression(String JavaDoc cflow)
100            throws ParseException
101    {
102       if (cflow != null)
103       {
104          cflowString = cflow;
105          this.cflow = new PointcutExpressionParser(new StringReader JavaDoc(cflowString)).CFlowExpression();
106       }
107    }
108
109    public void setPointcutExpression(String JavaDoc pointcutExpression)
110            throws ParseException
111    {
112       pointcut = new PointcutExpression(Long.toString(System.currentTimeMillis()) + ":" + Long.toString(counter++), pointcutExpression);
113    }
114
115    public void addInterceptorFactory(InterceptorFactory factory)
116    {
117       List JavaDoc list = Arrays.asList(interceptorFactories);
118       list = new ArrayList JavaDoc(list);
119       list.add(factory);
120       interceptorFactories = (InterceptorFactory[]) list.toArray(new InterceptorFactory[list.size()]);
121    }
122
123
124    /**
125     * Add an interceptor to chain. This is an actual class
126     * that implements Interceptor. A GenericInterceptorFactory will
127     * be created to wrap the class.
128     *
129     * @param clazz
130     */

131    public void addInterceptor(Class JavaDoc clazz)
132    {
133       addInterceptorFactory(new GenericInterceptorFactory(clazz));
134    }
135
136    public String JavaDoc getName()
137    {
138       return name;
139    }
140
141    public InterceptorFactory[] getInterceptorFactories()
142    {
143       return interceptorFactories;
144    }
145
146    public void setName(String JavaDoc name)
147    {
148       this.name = name;
149    }
150
151    public void addAdvisor(Advisor advisor)
152    {
153       if (AspectManager.verbose) System.out.println("[debug] added advisor: " + advisor.getName() + " from binding: " + name);
154       // Don't hold a direct reference to an advisor because of undeploy and redeploy. Use WeakRefrences because
155
// we may be having in the future an Advisor per instance.
156
synchronized (advisors)
157       {
158          if (advisors.size() > 0)
159          {
160             Iterator JavaDoc it = advisors.iterator();
161             while (it.hasNext())
162             {
163                WeakReference JavaDoc ref = (WeakReference JavaDoc) it.next();
164                Object JavaDoc obj = ref.get();
165                if (obj == null) it.remove();
166                else if(obj.equals(advisor))
167                {
168                   return; // don't add duplicate advisor
169
}
170             }
171          }
172          advisors.add(new WeakReference JavaDoc(advisor));
173       }
174       
175    }
176
177    public boolean hasAdvisors()
178    {
179       return advisors.size() > 0;
180    }
181
182    public ArrayList JavaDoc getAdvisors()
183    {
184       ArrayList JavaDoc list = new ArrayList JavaDoc(advisors.size());
185       synchronized (advisors)
186       {
187          Iterator JavaDoc it = advisors.iterator();
188          while (it.hasNext())
189          {
190             WeakReference JavaDoc ref = (WeakReference JavaDoc) it.next();
191             Object JavaDoc advisor = ref.get();
192             if (advisor != null)
193             {
194                list.add(advisor);
195             }
196             else
197             {
198                it.remove();
199             }
200          }
201       }
202       return list;
203    }
204
205    public void clearAdvisors()
206    {
207       synchronized (advisors)
208       {
209          for (Iterator JavaDoc it = advisors.iterator(); it.hasNext();)
210          {
211             WeakReference JavaDoc ref = (WeakReference JavaDoc) it.next();
212             Object JavaDoc obj = ref.get();
213             if (obj != null)
214             {
215                Advisor advisor = (Advisor) obj;
216                if (advisor.getManager().isAdvisorRegistered(advisor))
217                {
218                   advisor.removeAdviceBinding(this);
219                }
220             }
221          }
222          advisors.clear();
223       }
224    }
225
226    public boolean equals(Object JavaDoc obj)
227    {
228       if (obj == this) return true;
229       if (!(obj instanceof AdviceBinding)) return false;
230       return ((AdviceBinding) obj).getName().equals(name);
231    }
232
233    public int hashCode()
234    {
235       return name.hashCode();
236    }
237
238    public Pointcut getPointcut()
239    {
240       return pointcut;
241    }
242
243    public ASTCFlowExpression getCFlow()
244    {
245       return cflow;
246    }
247
248    public String JavaDoc getCFlowString()
249    {
250       return cflowString;
251    }
252 }
253
Popular Tags