KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > metadata > ClassMetaDataBinding


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.metadata;
23
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.jboss.aop.Advisor;
29 import org.jboss.aop.pointcut.Util;
30 import org.jboss.aop.pointcut.ast.ClassExpression;
31 import javassist.CtClass;
32 import javassist.NotFoundException;
33
34 /**
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 38741 $
37  */

38 public abstract class ClassMetaDataBinding
39 {
40    protected ClassExpression classExpr;
41    protected String JavaDoc expr;
42    protected String JavaDoc name;
43    protected String JavaDoc tag;
44    protected ArrayList JavaDoc advisors = new ArrayList JavaDoc();
45    protected ClassMetaDataLoader loader;
46
47    public ClassMetaDataBinding(ClassMetaDataLoader loader, String JavaDoc name, String JavaDoc tag, String JavaDoc exp)
48    {
49       this.name = name;
50       this.tag = tag;
51       this.loader = loader;
52       expr = exp;
53       this.classExpr = new ClassExpression(expr);
54    }
55
56    public ClassMetaDataLoader getLoader()
57    {
58       return loader;
59    }
60
61    public String JavaDoc getName()
62    {
63       return name;
64    }
65    
66    public String JavaDoc getTag()
67    {
68       return tag;
69    }
70
71    public void addAdvisor(Advisor advisor)
72    {
73       // Don't hold a direct reference to an advisor because of undeploy and redeploy. Use WeakRefrences because
74
// we may be having in the future an Advisor per instance.
75
synchronized (advisors)
76       {
77          Iterator JavaDoc it = advisors.iterator();
78          while (it.hasNext())
79          {
80             WeakReference JavaDoc ref = (WeakReference JavaDoc) it.next();
81             Object JavaDoc obj = ref.get();
82             if (obj == null) it.remove();
83          }
84          advisors.add(new WeakReference JavaDoc(advisor));
85       }
86       advisor.addClassMetaData(this);
87    }
88
89    public void clearAdvisors()
90    {
91       synchronized (advisors)
92       {
93          for (int i = 0; i < advisors.size(); i++)
94          {
95             WeakReference JavaDoc ref = (WeakReference JavaDoc) advisors.get(i);
96             Advisor advisor = (Advisor) ref.get();
97             if (advisor != null)
98                advisor.removeClassMetaData(this);
99          }
100          advisors.clear();
101       }
102    }
103
104    public boolean equals(Object JavaDoc obj)
105    {
106       if (obj == this) return true;
107       if (!(obj instanceof ClassMetaDataBinding)) return false;
108       return ((ClassMetaDataBinding) obj).getName().equals(name);
109    }
110
111    public int hashCode()
112    {
113       return name.hashCode();
114    }
115
116    public boolean matches(Advisor advisor, Class JavaDoc clazz)
117    {
118       return Util.matchesClassExpr(classExpr, clazz, advisor);
119    }
120
121    public boolean matches(Advisor advisor, CtClass clazz) throws NotFoundException
122    {
123       return Util.matchesClassExpr(classExpr, clazz, advisor);
124    }
125
126    public String JavaDoc getClassExpr()
127    {
128       return expr;
129    }
130 }
131
Popular Tags