KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > proxy > container > ContainerProxyCacheKey


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.proxy.container;
23
24 import java.io.Serializable JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Comparator JavaDoc;
28
29 //import org.jboss.repository.spi.MetaDataContext;
30

31 /**
32  *
33  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
34  * @version $Revision: 57019 $
35  */

36 public class ContainerProxyCacheKey implements Serializable JavaDoc
37 {
38    private static final long serialVersionUID = 8758283842273747310L;
39    private static final WeakReference JavaDoc[] EMTPY_WR_ARRAY = new WeakReference JavaDoc[0];
40    private static final AOPProxyFactoryMixin[] EMPTY_MIXIN_ARRAY = new AOPProxyFactoryMixin[0];
41    
42    private WeakReference JavaDoc clazzRef;
43    private WeakReference JavaDoc[] addedInterfaces = EMTPY_WR_ARRAY;
44    
45    //FIXME convert back to MetaDataContext once the mc 2.0 has been released
46
//private MetaDataContext metaDataContext;
47
private Object JavaDoc metaDataContext;
48    
49    private AOPProxyFactoryMixin[] addedMixins = EMPTY_MIXIN_ARRAY;
50    private int hashcode = 0;
51    
52    public ContainerProxyCacheKey(Class JavaDoc clazz)
53    {
54       this.clazzRef = new WeakReference JavaDoc(clazz);
55    }
56    
57    //public ContainerProxyCacheKey(Class clazz, Class[] addedInterfaces, MetaDataContext metaDataContext)
58
public ContainerProxyCacheKey(Class JavaDoc clazz, Class JavaDoc[] addedInterfaces, Object JavaDoc metaDataContext)
59    {
60       this(clazz);
61       this.addedInterfaces = ContainerCacheUtil.getSortedWeakReferenceForInterfaces(addedInterfaces);
62       this.metaDataContext = metaDataContext;
63    }
64
65    public ContainerProxyCacheKey(Class JavaDoc clazz, Class JavaDoc[] addedInterfaces, AOPProxyFactoryMixin[] addedMixins, Object JavaDoc metaDataContext)
66    {
67       this(clazz, addedInterfaces, metaDataContext);
68       
69       if (addedMixins != null)
70       {
71          this.addedMixins = addedMixins;
72          Arrays.sort(this.addedMixins, MixinAlphabetical.singleton);
73       }
74    }
75
76    public Class JavaDoc getClazz()
77    {
78       Class JavaDoc clazz = (Class JavaDoc)clazzRef.get();
79       if (clazz != null)
80       {
81          return clazz;
82       }
83       return null;
84    }
85    
86    public boolean equals(Object JavaDoc obj)
87    {
88       if (this == obj)
89       {
90          return true;
91       }
92       
93       if (obj.getClass() != ContainerProxyCacheKey.class)
94       {
95          return false;
96       }
97       
98       ContainerProxyCacheKey other = (ContainerProxyCacheKey)obj;
99
100       if (!compareMetadataContext(other))
101       {
102          return false;
103       }
104       if (!compareClass(other))
105       {
106          return false;
107       }
108       if (!compareAddedInterfaces(other))
109       {
110          return false;
111       }
112       if (!compareAddedMixins(other))
113       {
114          return false;
115       }
116       
117       return true;
118    }
119
120    public int hashCode()
121    {
122       if (hashcode == 0)
123       {
124          
125          Class JavaDoc clazz = (Class JavaDoc)clazzRef.get();
126          StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
127          
128          if (clazz != null)
129          {
130             sb.append(clazz.getName());
131          }
132          
133          if (addedInterfaces != null)
134          {
135             for (int i = 0 ; i < addedInterfaces.length ; i++)
136             {
137                sb.append(";");
138                sb.append(((Class JavaDoc)addedInterfaces[i].get()).getName());
139             }
140          }
141          
142          hashcode = sb.toString().hashCode();
143          
144          if (metaDataContext != null)
145          {
146             hashcode += metaDataContext.hashCode();
147          }
148       }
149       
150       return hashcode;
151    }
152    
153    public String JavaDoc toString()
154    {
155       StringBuffer JavaDoc buf = new StringBuffer JavaDoc("ContainerProxyCache");
156       buf.append(((Class JavaDoc)clazzRef.get()).getName());
157       buf.append(";interfaces=");
158       if (addedInterfaces == null)
159       {
160          buf.append("null");
161       }
162       else
163       {
164          buf.append(Arrays.asList(addedInterfaces));
165       }
166       buf.append(";mixins=");
167       if (addedMixins == null)
168       {
169          buf.append("null");
170       }
171       else
172       {
173          buf.append(Arrays.asList(addedMixins));
174       }
175       return buf.toString();
176    }
177    
178    private boolean compareMetadataContext(ContainerProxyCacheKey other)
179    {
180       if (this.metaDataContext == null && other.metaDataContext == null)
181       {
182       }
183       else if ((this.metaDataContext != null && other.metaDataContext != null))
184       {
185          if (!this.metaDataContext.equals(other.metaDataContext))
186          {
187             return false;
188          }
189       }
190       else
191       {
192          return false;
193       }
194       return true;
195    }
196    
197    private boolean compareClass(ContainerProxyCacheKey other)
198    {
199       return ContainerCacheUtil.compareClassRefs(this.clazzRef, other.clazzRef);
200    }
201    
202    private boolean compareAddedInterfaces(ContainerProxyCacheKey other)
203    {
204       return ContainerCacheUtil.compareInterfaceRefs(this.addedInterfaces, other.addedInterfaces);
205    }
206
207    private boolean compareAddedMixins(ContainerProxyCacheKey other)
208    {
209       if ((this.addedMixins == null && other.addedMixins != null) ||
210             (this.addedMixins == null && other.addedMixins != null))
211       {
212          return false;
213       }
214       
215       if (this.addedMixins != null && other.addedMixins != null)
216       {
217          if (this.addedMixins.length != other.addedMixins.length)
218          {
219             return false;
220          }
221          
222          for (int i = 0 ; i < this.addedMixins.length ; i++)
223          {
224             if (!this.addedMixins[i].equals(other.addedMixins[i]))
225             {
226                return false;
227             }
228          }
229       }
230       
231       return true;
232    }
233    
234    static class MixinAlphabetical implements Comparator JavaDoc
235    {
236       static MixinAlphabetical singleton = new MixinAlphabetical();
237       
238       public int compare(Object JavaDoc o1, Object JavaDoc o2)
239       {
240          String JavaDoc name1 = ((AOPProxyFactoryMixin)o1).getMixin().getName();
241          String JavaDoc name2 = ((AOPProxyFactoryMixin)o2).getMixin().getName();
242          return (name1).compareTo(name2);
243       }
244    }
245 }
Popular Tags