1 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 32 public class MarshalledMethod implements Serializable { 33 34 String declaringClass; 35 String signature; 36 37 40 public MarshalledMethod() { 41 } 42 43 46 public MarshalledMethod(Method method) { 47 declaringClass = method.getDeclaringClass().getName(); 48 signature = getSignature( method ); 49 } 50 51 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 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 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 return getSignatureMapFor(clazz); 113 } 114 115 } 116 117 118 119 } 120 | Popular Tags |