?? dynamicstubhandler.java
字號(hào):
/*
* 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();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -