KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javassist.CtMethod;
24 import org.jboss.aop.joinpoint.Invocation;
25 import org.jboss.aop.joinpoint.MethodInvocation;
26 import org.jboss.aop.util.PayloadKey;
27
28 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
29
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 /**
35  *
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @version $Revision: 45996 $
38  *
39  */

40 public class MethodMetaData implements MetaDataResolver
41 {
42    Map methodMetaData = new ConcurrentReaderHashMap();
43    HashMap inexactMatches;
44
45    public boolean hasTag(String JavaDoc group)
46    {
47       Iterator JavaDoc values = methodMetaData.values().iterator();
48       while (values.hasNext())
49       {
50          SimpleMetaData map = (SimpleMetaData)values.next();
51          if (map.hasTag(group)) return true;
52       }
53       return false;
54    }
55    public void tagMethod(Method method, Object JavaDoc tag)
56    {
57       addMethodMetaData(method, tag, EMPTY_TAG, new Object JavaDoc(), PayloadKey.TRANSIENT);
58    }
59    public void addMethodMetaData(Method method, Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value)
60    {
61       addMethodMetaData(method, tag, attr, value, PayloadKey.MARSHALLED, true);
62    }
63    public void addMethodMetaData(Method method, Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value, boolean exactMatch)
64    {
65       addMethodMetaData(method, tag, attr, value, PayloadKey.MARSHALLED, exactMatch);
66    }
67    public void addMethodMetaData(Method method, Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value, PayloadKey type)
68    {
69       addMethodMetaData(method.toString(), tag, attr, value, type, true);
70    }
71    public void addMethodMetaData(Method method, Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value, PayloadKey type, boolean exactMatch)
72    {
73       addMethodMetaData(method.toString(), tag, attr, value, type, exactMatch);
74    }
75
76    private void addMethodMetaData(String JavaDoc key, Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value, PayloadKey type, boolean exactMatch)
77    {
78       synchronized (methodMetaData)
79       {
80          SimpleMetaData methodData = (SimpleMetaData)methodMetaData.get(key);
81          if (methodData == null)
82          {
83             methodData = new SimpleMetaData();
84             methodMetaData.put(key, methodData);
85          }
86          methodData.addMetaData(tag, attr, value, type);
87       }
88       manageInexactMatches(key, tag, attr, exactMatch);
89    }
90    
91    private synchronized void manageInexactMatches(String JavaDoc key, Object JavaDoc tag, Object JavaDoc attr, boolean exactMatch)
92    {
93       if (!exactMatch)
94       {
95          if (inexactMatches == null)
96          {
97             inexactMatches = new HashMap();
98          }
99          
100          HashMap tags = (HashMap)inexactMatches.get(key);
101          if (tags == null)
102          {
103             tags = new HashMap();
104             inexactMatches.put(key, tags);
105          }
106          
107          HashMap attrs = (HashMap)tags.get(tag);
108          if (attrs == null)
109          {
110             attrs = new HashMap();
111             tags.put(tag, attrs);
112          }
113          attrs.put(attr, Boolean.TRUE);
114       }
115       else
116       {
117          if (inexactMatches == null)return;
118          HashMap tags = (HashMap)inexactMatches.get(key);
119          if (tags == null) return;
120          HashMap attrs = (HashMap)tags.get(tag);
121          if (attrs == null) return;
122          attrs.remove(attr);
123       }
124
125    }
126
127    public synchronized boolean tagWasMatchedInexactly(Method method, Object JavaDoc tag, Object JavaDoc attr)
128    {
129       if (inexactMatches == null) return false;
130       HashMap tags = (HashMap)inexactMatches.get(method.toString());
131       if (tags == null) return false;
132       HashMap attrs = (HashMap)tags.get(tag);
133       if (attrs == null) return false;
134       return (attrs.get(attr) != null);
135    }
136    
137    public boolean hasTag(Method method, String JavaDoc tag)
138    {
139       SimpleMetaData meta = getMethodMetaData(method);
140       if (meta == null) return false;
141       return meta.hasTag(tag);
142    }
143
144    public Iterator JavaDoc getMethods()
145    {
146       return methodMetaData.keySet().iterator();
147    }
148
149    /**
150     * This requires a method signature derived from java.lang.reflect.Method.toString()
151     * @param method
152     * @return
153     */

154    public SimpleMetaData getMethodMetaData(String JavaDoc method)
155    {
156       return (SimpleMetaData)methodMetaData.get(method);
157    }
158
159    public SimpleMetaData getMethodMetaData(Method method)
160    {
161       return getMethodMetaData(method.toString());
162    }
163
164    public Object JavaDoc getMethodMetaData(Method method, Object JavaDoc tag, Object JavaDoc attr)
165    {
166       SimpleMetaData methodData = (SimpleMetaData)methodMetaData.get(method.toString());
167       if (methodData == null) return null;
168       return methodData.getMetaData(tag, attr);
169    }
170
171    public void clear()
172    {
173       methodMetaData.clear();
174    }
175
176    public Object JavaDoc resolve(Invocation invocation, Object JavaDoc tag, Object JavaDoc attr)
177    {
178       MethodInvocation methodInvocation = (MethodInvocation)invocation;
179       return getMethodMetaData(methodInvocation.getMethod(), tag, attr);
180    }
181
182    public SimpleMetaData getAllMetaData(Invocation invocation)
183    {
184       MethodInvocation methodInvocation = (MethodInvocation)invocation;
185       return (SimpleMetaData)methodMetaData.get(methodInvocation.getMethod().toString());
186    }
187
188    //--- temporary interface until metadata is bound to actual class, this is needed for loader/compiler to
189
// match annotations to pointcuts
190

191    private String JavaDoc getCtMethodKey(CtMethod method)
192    {
193       return method.getName() + ":" + method.getSignature();
194    }
195
196    public void tagMethod(CtMethod method, Object JavaDoc tag)
197    {
198       addMethodMetaData(method, tag, EMPTY_TAG, PayloadKey.TRANSIENT);
199    }
200    public void addMethodMetaData(CtMethod method, Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value)
201    {
202       addMethodMetaData(getCtMethodKey(method), tag, attr, value, PayloadKey.MARSHALLED, true);
203    }
204    public void addMethodMetaData(CtMethod method, Object JavaDoc tag, Object JavaDoc attr, Object JavaDoc value, PayloadKey type)
205    {
206       addMethodMetaData(getCtMethodKey(method), tag, attr, value, type, true);
207    }
208
209    public boolean hasGroup(CtMethod method, String JavaDoc tag)
210    {
211       SimpleMetaData meta = getMethodMetaData(getCtMethodKey(method));
212       if (meta == null) return false;
213       return meta.hasTag(tag);
214    }
215
216
217 }
218
Popular Tags