KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > remoting > jmx > MarshalledMethod


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26
27 import org.apache.geronimo.kernel.ClassLoading;
28
29 /**
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class MarshalledMethod implements Serializable JavaDoc {
33
34     String JavaDoc declaringClass;
35     String JavaDoc signature;
36
37     /**
38      *
39      */

40     public MarshalledMethod() {
41     }
42
43     /**
44      * @param method
45      */

46     public MarshalledMethod(Method JavaDoc method) {
47         declaringClass = method.getDeclaringClass().getName();
48         signature = getSignature( method );
49     }
50
51     /**
52      * @param method
53      * @return
54      */

55     static public String JavaDoc getSignature(Method JavaDoc method) {
56         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
57         sb.append(method.getName());
58         sb.append(' ');
59         Class JavaDoc[] 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 JavaDoc getMethod() throws ClassNotFoundException JavaDoc {
71         Class JavaDoc c = Thread.currentThread().getContextClassLoader().loadClass(declaringClass);
72         Map JavaDoc sigs = getCachedSignatureMap(c);
73         return (Method JavaDoc) sigs.get(signature);
74     }
75
76     /**
77      * TODO: try to cache the results.
78      * @param clazz
79      * @return
80      */

81     static private Map JavaDoc getSignatureMapFor(Class JavaDoc clazz) {
82         Map JavaDoc rc = new HashMap JavaDoc();
83         Method JavaDoc[] methods = clazz.getDeclaredMethods();
84         for (int i = 0; i < methods.length; i++) {
85             Method JavaDoc method = methods[i];
86             rc.put(getSignature(method), method);
87         }
88         return rc;
89     }
90
91     private static Map JavaDoc SignatureMapCache= Collections.synchronizedMap(new WeakHashMap JavaDoc());
92     static class CacheValue {
93         Class JavaDoc clazz;
94         Map JavaDoc sigs;
95     }
96
97
98     public static Map JavaDoc getCachedSignatureMap(Class JavaDoc clazz) {
99         String JavaDoc 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 }
120
Popular Tags