⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynamicstubhandler.java

📁 《精通RMI:Java与EJB企业级应用开发》书籍的源代码. 本书是讲述RMI技术的经典著作
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package smartproxy.proxies;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.RemoteStub;
import java.security.*;
import java.io.*;
import java.util.*;

/**
 *   This class implements the InvocationHandler interface. It delegates call to an embedded RemoteStub
 *   Most of the class is dedicated to method hash computation, as defined in the RMI spec.
 *      
 *   @author Rickard 謆erg (rickard@dreambean.com)
 */
public class DynamicStubHandler
   implements InvocationHandler, java.io.Serializable
{
   public static void init()
   {
      System.setProperty("java.protocol.handler.pkgs", "smartproxy");
   }

   public static Object resolve(RemoteStub obj, Class cl)
   {
      DynamicStubHandler stubHandler = new DynamicStubHandler();
      Object proxy = Proxy.newProxyInstance(obj.getClass().getClassLoader(),
                                          new Class[] { cl },
                                          stubHandler);
      stubHandler.setProxy(obj);
      
      return proxy;
   }
   
   RemoteStub stub;
   transient HashMap hashes;
   
   static HashMap interfaceMethodHashes = new HashMap();

   static HashMap getInterfaceHashes(Class intf)
   {
      synchronized(DynamicStubHandler.class)
      {
         HashMap map = (HashMap)interfaceMethodHashes.get(intf);
         if (map == null)
         {
            // Create method hashes
            Method[] methods = intf.getDeclaredMethods();
            map = new HashMap();
            for (int i = 0; i < methods.length; i++)
            {
               Method method = methods[i];
               Class[] parameterTypes = method.getParameterTypes();
               String methodDesc = method.getName()+"(";
               for(int j = 0; j < parameterTypes.length; j++)
               {
                  methodDesc += getTypeString(parameterTypes[j]);
               }
               methodDesc += ")"+getTypeString(method.getReturnType());
            
               try
               {
                  long hash = 0;
                  ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(512);
                  MessageDigest messagedigest = MessageDigest.getInstance("SHA");
                  DataOutputStream dataoutputstream = new DataOutputStream(new DigestOutputStream(bytearrayoutputstream, messagedigest));
                  dataoutputstream.writeUTF(methodDesc);
                  dataoutputstream.flush();
                  byte abyte0[] = messagedigest.digest();
                  for(int j = 0; j < Math.min(8, abyte0.length); j++)
                    hash += (long)(abyte0[j] & 0xff) << j * 8;
                  map.put(method, new Long(hash));
               } catch (Exception e)
               {
                  e.printStackTrace();
               }
            }
            
            interfaceMethodHashes.put(intf, map);
         }
         
         return map;
      }
   }
   
   static String getTypeString(Class cl)
   {
      if (cl == Byte.TYPE)
      {
         return "B";
      } else if (cl == Character.TYPE)
      {
         return "C";
      } else if (cl == Double.TYPE)
      {
         return "D";
      } else if (cl == Float.TYPE)
      {
         return "F";
      } else if (cl == Integer.TYPE)
      {
         return "I";
      } else if (cl == Long.TYPE)
      {
         return "J";
      } else if (cl == Short.TYPE)
      {
         return "S";
      } else if (cl == Boolean.TYPE)
      {
         return "Z";
      } else if (cl == Void.TYPE)
      {
         return "V";
      } else if (cl.isArray())
      {
         return "["+getTypeString(cl.getComponentType());
      } else
      {
         return "L"+cl.getName().replace('.','/')+";";
      }
   }
   
   // Public --------------------------------------------------------
   public void setProxy(RemoteStub stub)
   {
      this.stub = stub;
   }
   
   public void setHash(HashMap hashes)
   {
      this.hashes = hashes;
   }
   
   // InvocationHandler implementation ------------------------------
   public Object invoke(Object proxy,
                     Method method,
                     Object[] args)
              throws Throwable
   {
      return stub.getRef().invoke(stub, method, args, getHash(method, proxy));
   }
   
   protected long getHash(Method m, Object proxy)
   {
      if (hashes == null)
      {
         hashes = getInterfaceHashes(proxy.getClass().getInterfaces()[0]);
      }
      
      Long l = (Long)hashes.get(m);
      if (l != null)
         return l.longValue();
      else
         return ((Long)getInterfaceHashes(m.getDeclaringClass()).get(m)).longValue();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -