KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > sockets > MethodHash


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.net.sockets;
23 import java.util.WeakHashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.io.DataOutputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.security.DigestOutputStream JavaDoc;
30 import java.security.MessageDigest JavaDoc;
31
32 public class MethodHash
33 {
34
35    // Static --------------------------------------------------------
36
static Map JavaDoc hashMap = new WeakHashMap JavaDoc();
37    
38    /**
39    * Calculate method hashes. This algo is taken from RMI.
40    *
41    * @param intf
42    * @return
43    */

44    public static Map JavaDoc getInterfaceHashes(Class JavaDoc intf)
45    {
46       // Create method hashes
47
Method JavaDoc[] methods = intf.getMethods();
48       HashMap JavaDoc map = new HashMap JavaDoc();
49       for (int i = 0; i < methods.length; i++)
50       {
51          Method JavaDoc method = methods[i];
52          Class JavaDoc[] parameterTypes = method.getParameterTypes();
53          String JavaDoc methodDesc = method.getName()+"(";
54          for(int j = 0; j < parameterTypes.length; j++)
55          {
56             methodDesc += getTypeString(parameterTypes[j]);
57          }
58          methodDesc += ")"+getTypeString(method.getReturnType());
59          
60          try
61          {
62             long hash = 0;
63             ByteArrayOutputStream JavaDoc bytearrayoutputstream = new ByteArrayOutputStream JavaDoc(512);
64             MessageDigest JavaDoc messagedigest = MessageDigest.getInstance("SHA");
65             DataOutputStream JavaDoc dataoutputstream = new DataOutputStream JavaDoc(new DigestOutputStream JavaDoc(bytearrayoutputstream, messagedigest));
66             dataoutputstream.writeUTF(methodDesc);
67             dataoutputstream.flush();
68             byte abyte0[] = messagedigest.digest();
69             for(int j = 0; j < Math.min(8, abyte0.length); j++)
70                hash += (long)(abyte0[j] & 0xff) << j * 8;
71             map.put(method.toString(), new Long JavaDoc(hash));
72          }
73          catch (Exception JavaDoc e)
74          {
75             e.printStackTrace();
76          }
77       }
78       
79       return map;
80    }
81    
82    static String JavaDoc getTypeString(Class JavaDoc cl)
83    {
84       if (cl == Byte.TYPE)
85       {
86          return "B";
87       } else if (cl == Character.TYPE)
88       {
89          return "C";
90       } else if (cl == Double.TYPE)
91       {
92          return "D";
93       } else if (cl == Float.TYPE)
94       {
95          return "F";
96       } else if (cl == Integer.TYPE)
97       {
98          return "I";
99       } else if (cl == Long.TYPE)
100       {
101          return "J";
102       } else if (cl == Short.TYPE)
103       {
104          return "S";
105       } else if (cl == Boolean.TYPE)
106       {
107          return "Z";
108       } else if (cl == Void.TYPE)
109       {
110          return "V";
111       } else if (cl.isArray())
112       {
113          return "["+getTypeString(cl.getComponentType());
114       } else
115       {
116          return "L"+cl.getName().replace('.','/')+";";
117       }
118    }
119    
120    /*
121    * The use of hashCode is not enough to differenciate methods
122    * we override the hashCode
123    *
124    * The hashes are cached in a static for efficiency
125    * RO: WeakHashMap needed to support undeploy
126    */

127    public static long calculateHash(Method JavaDoc method)
128    {
129       Map JavaDoc methodHashes = (Map JavaDoc)hashMap.get(method.getDeclaringClass());
130       
131       if (methodHashes == null)
132       {
133          methodHashes = getInterfaceHashes(method.getDeclaringClass());
134          
135          // Copy and add
136
WeakHashMap JavaDoc newHashMap = new WeakHashMap JavaDoc();
137          newHashMap.putAll(hashMap);
138          newHashMap.put(method.getDeclaringClass(), methodHashes);
139          hashMap = newHashMap;
140       }
141       
142       return ((Long JavaDoc)methodHashes.get(method.toString())).longValue();
143    }
144
145 }
146
Popular Tags