View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.security.remoting.jmx;
19  
20  import java.io.Serializable;
21  import java.lang.reflect.Method;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.WeakHashMap;
26  
27  import org.apache.geronimo.kernel.ClassLoading;
28  
29  /**
30   * @version $Rev: 417891 $ $Date: 2006-06-28 15:45:07 -0700 (Wed, 28 Jun 2006) $
31   */
32  public class MarshalledMethod implements Serializable {
33  
34      String declaringClass;
35      String signature;
36  
37      /**
38       * 
39       */
40      public MarshalledMethod() {
41      }
42  
43      /**
44       * @param method
45       */
46      public MarshalledMethod(Method method) {
47          declaringClass = method.getDeclaringClass().getName();
48          signature = getSignature( method );
49      }
50  
51      /**
52       * @param method
53       * @return
54       */
55      static public String getSignature(Method method) {
56          StringBuffer sb = new StringBuffer();
57          sb.append(method.getName());
58          sb.append(' ');
59          Class[] args = method.getParameterTypes();
60          for (int i = 0; i < args.length; i++) {
61              sb.append(' ');
62              sb.append( ClassLoading.getClassName(args[i]) );
63          }
64          return sb.toString();
65      }
66  
67      /**
68       * @return
69       */
70      public Method getMethod() throws ClassNotFoundException {
71          Class c = Thread.currentThread().getContextClassLoader().loadClass(declaringClass);
72          Map sigs = getCachedSignatureMap(c);        
73          return (Method) sigs.get(signature);
74      }
75  
76      /**
77       * TODO: try to cache the results.
78       * @param clazz
79       * @return
80       */
81      static private Map getSignatureMapFor(Class clazz) {
82          Map rc = new HashMap();
83          Method[] methods = clazz.getDeclaredMethods();
84          for (int i = 0; i < methods.length; i++) {
85              Method method = methods[i];
86              rc.put(getSignature(method), method);
87          }
88          return rc;
89      }
90  
91      private static Map SignatureMapCache= Collections.synchronizedMap(new WeakHashMap());
92      static class CacheValue {
93          Class clazz;
94          Map sigs;
95      }
96  
97  
98      public static Map getCachedSignatureMap(Class clazz) {
99          String cacheKey = clazz.getName();
100         CacheValue rc = (CacheValue) SignatureMapCache.get(cacheKey);
101         if (rc == null) {
102             rc = new CacheValue();
103             rc.clazz = clazz;
104             rc.sigs = getSignatureMapFor(clazz);
105             SignatureMapCache.put(cacheKey, rc);
106             return rc.sigs;
107         } else if ( rc.clazz.equals( clazz ) ) {
108             return rc.sigs;
109         } else {
110             // the previously cache class name might not be the same class
111             // due to classloader issues.
112             return getSignatureMapFor(clazz);
113         }
114         
115     }
116 
117     
118 
119 }