1 7 package java.rmi.server; 8 9 import java.io.InvalidObjectException ; 10 import java.lang.reflect.InvocationHandler ; 11 import java.lang.reflect.Method ; 12 import java.lang.reflect.Proxy ; 13 import java.rmi.Remote ; 14 import java.rmi.UnexpectedException ; 15 import java.rmi.activation.Activatable ; 16 import java.util.Map ; 17 import java.util.WeakHashMap ; 18 import sun.rmi.server.Util; 19 import sun.rmi.server.WeakClassHashMap; 20 21 36 public class RemoteObjectInvocationHandler 37 extends RemoteObject 38 implements InvocationHandler 39 { 40 private static final long serialVersionUID = 2L; 41 42 46 private static final MethodToHash_Maps methodToHash_Maps = 47 new MethodToHash_Maps(); 48 49 57 public RemoteObjectInvocationHandler(RemoteRef ref) { 58 super(ref); 59 if (ref == null) { 60 throw new NullPointerException (); 61 } 62 } 63 64 126 public Object invoke(Object proxy, Method method, Object [] args) 127 throws Throwable 128 { 129 if (method.getDeclaringClass() == Object .class) { 130 return invokeObjectMethod(proxy, method, args); 131 } else { 132 return invokeRemoteMethod(proxy, method, args); 133 } 134 } 135 136 139 private Object invokeObjectMethod(Object proxy, 140 Method method, 141 Object [] args) 142 { 143 String name = method.getName(); 144 145 if (name.equals("hashCode")) { 146 return new Integer (hashCode()); 147 148 } else if (name.equals("equals")) { 149 Object obj = args[0]; 150 boolean b = 151 proxy == obj || 152 (obj != null && 153 Proxy.isProxyClass(obj.getClass()) && 154 equals(Proxy.getInvocationHandler(obj))); 155 return Boolean.valueOf(b); 156 157 } else if (name.equals("toString")) { 158 return proxyToString(proxy); 159 160 } else { 161 throw new IllegalArgumentException ( 162 "unexpected Object method: " + method); 163 } 164 } 165 166 169 private Object invokeRemoteMethod(Object proxy, 170 Method method, 171 Object [] args) 172 throws Exception 173 { 174 try { 175 if (!(proxy instanceof Remote )) { 176 throw new IllegalArgumentException ( 177 "proxy not Remote instance"); 178 } 179 return ref.invoke((Remote ) proxy, method, args, 180 getMethodHash(method)); 181 } catch (Exception e) { 182 if (!(e instanceof RuntimeException )) { 183 Class cl = proxy.getClass(); 184 try { 185 method = cl.getMethod(method.getName(), 186 method.getParameterTypes()); 187 } catch (NoSuchMethodException nsme) { 188 throw (IllegalArgumentException ) 189 new IllegalArgumentException ().initCause(nsme); 190 } 191 Class [] exTypes = method.getExceptionTypes(); 192 Class thrownType = e.getClass(); 193 for (int i = 0; i < exTypes.length; i++) { 194 if (exTypes[i].isAssignableFrom(thrownType)) { 195 throw e; 196 } 197 } 198 e = new UnexpectedException ("unexpected exception", e); 199 } 200 throw e; 201 } 202 } 203 204 208 private String proxyToString(Object proxy) { 209 Class [] interfaces = proxy.getClass().getInterfaces(); 210 if (interfaces.length == 0) { 211 return "Proxy[" + this + "]"; 212 } 213 String iface = interfaces[0].getName(); 214 if (iface.equals("java.rmi.Remote") && interfaces.length > 1) { 215 iface = interfaces[1].getName(); 216 } 217 int dot = iface.lastIndexOf('.'); 218 if (dot >= 0) { 219 iface = iface.substring(dot + 1); 220 } 221 return "Proxy[" + iface + "," + this + "]"; 222 } 223 224 227 private void readObjectNoData() throws InvalidObjectException { 228 throw new InvalidObjectException ("no data in stream; class: " + 229 this.getClass().getName()); 230 } 231 232 242 private static long getMethodHash(Method method) { 243 Map map = methodToHash_Maps.getMap(method.getDeclaringClass()); 244 Long hash = (Long ) map.get(method); 245 return hash.longValue(); 246 } 247 248 252 private static class MethodToHash_Maps extends WeakClassHashMap { 253 254 MethodToHash_Maps() {} 255 256 protected Map createMap(Class remoteClass) { 257 return new WeakHashMap () { 258 public synchronized Object get(Object key) { 259 Object hash = super.get(key); 260 if (hash == null) { 261 Method method = (Method ) key; 262 hash = new Long (Util.computeMethodHash(method)); 263 put(method, hash); 264 } 265 return (Long ) hash; 266 } 267 }; 268 } 269 } 270 } 271 | Popular Tags |