1 4 package com.tc.aspectwerkz.connectivity; 5 6 7 import com.tc.aspectwerkz.exception.WrappedRuntimeException; 8 import com.tc.aspectwerkz.util.UuidGenerator; 9 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.io.Serializable ; 14 import java.lang.reflect.InvocationHandler ; 15 import java.lang.reflect.Method ; 16 import java.lang.reflect.Proxy ; 17 import java.net.InetAddress ; 18 import java.net.Socket ; 19 import java.util.Map ; 20 import java.util.WeakHashMap ; 21 22 29 public class RemoteProxy implements InvocationHandler , Serializable { 30 35 private static final long serialVersionUID = 1L; 36 37 40 private transient static Map s_instances = new WeakHashMap (); 41 42 45 private final String m_address; 46 47 50 private final int m_port; 51 52 55 private String m_handle = null; 56 57 60 private Class [] m_targetInterfaces = null; 61 62 65 private String [] m_targetInterfaceNames = null; 66 67 70 private String m_targetImplName = null; 71 72 75 private transient Socket m_socket; 76 77 80 private transient ObjectInputStream m_in; 81 82 85 private transient ObjectOutputStream m_out; 86 87 90 private transient ClassLoader m_loader; 91 92 95 private transient Object m_context = null; 96 97 100 private transient Object m_proxy = null; 101 102 113 private RemoteProxy(final String [] interfaces, 114 final String impl, 115 final String address, 116 final int port, 117 final Object context, 118 final ClassLoader loader) { 119 if ((interfaces == null) || (interfaces.length == 0)) { 120 throw new IllegalArgumentException ("at least one interface must be specified"); 121 } 122 if (impl == null) { 123 throw new IllegalArgumentException ("implementation class name can not be null"); 124 } 125 if (address == null) { 126 throw new IllegalArgumentException ("address can not be null"); 127 } 128 if (port < 0) { 129 throw new IllegalArgumentException ("port not valid"); 130 } 131 m_targetInterfaceNames = interfaces; 132 m_targetImplName = impl; 133 m_address = address; 134 m_port = port; 135 m_context = context; 136 m_loader = loader; 137 } 138 139 147 private RemoteProxy(final Object targetInstance, final String address, final int port) { 148 if (targetInstance == null) { 149 throw new IllegalArgumentException ("target instance can not be null"); 150 } 151 if (address == null) { 152 throw new IllegalArgumentException ("address can not be null"); 153 } 154 if (port < 0) { 155 throw new IllegalArgumentException ("port not valid"); 156 } 157 m_targetInterfaces = targetInstance.getClass().getInterfaces(); 158 m_address = address; 159 m_port = port; 160 m_handle = wrapInstance(targetInstance); 161 } 162 163 172 public static RemoteProxy createClientProxy(final String [] interfaces, 173 final String impl, 174 final String address, 175 final int port) { 176 return RemoteProxy.createClientProxy( 177 interfaces, impl, address, port, Thread.currentThread().getContextClassLoader() 178 ); 179 } 180 181 191 public static RemoteProxy createClientProxy(final String [] interfaces, 192 final String impl, 193 final String address, 194 final int port, 195 final Object context) { 196 return RemoteProxy.createClientProxy( 197 interfaces, impl, address, port, context, Thread.currentThread() 198 .getContextClassLoader() 199 ); 200 } 201 202 212 public static RemoteProxy createClientProxy(final String [] interfaces, 213 final String impl, 214 final String address, 215 final int port, 216 final ClassLoader loader) { 217 return RemoteProxy.createClientProxy(interfaces, impl, address, port, null, loader); 218 } 219 220 231 public static RemoteProxy createClientProxy(final String [] interfaces, 232 final String impl, 233 final String address, 234 final int port, 235 final Object context, 236 final ClassLoader loader) { 237 return new RemoteProxy(interfaces, impl, address, port, context, loader); 238 } 239 240 249 public static RemoteProxy createServerProxy(final Object targetlInstance, final String address, final int port) { 250 return new RemoteProxy(targetlInstance, address, port); 251 } 252 253 259 public Object getInstance(final ClassLoader loader) { 260 m_loader = loader; 261 return getInstance(); 262 } 263 264 269 public Object getInstance() { 270 if (m_proxy != null) { 271 return m_proxy; 272 } 273 if (m_loader == null) { 274 m_loader = Thread.currentThread().getContextClassLoader(); 275 } 276 try { 277 m_socket = new Socket (InetAddress.getByName(m_address), m_port); 278 m_socket.setTcpNoDelay(true); 279 m_out = new ObjectOutputStream (m_socket.getOutputStream()); 280 m_in = new ObjectInputStream (m_socket.getInputStream()); 281 } catch (Exception e) { 282 throw new WrappedRuntimeException(e); 283 } 284 if (m_handle == null) { 285 if (m_targetInterfaceNames == null) { 287 throw new IllegalStateException ("interface class name can not be null"); 288 } 289 if (m_targetImplName == null) { 290 throw new IllegalStateException ("implementation class name can not be null"); 291 } 292 try { 293 m_out.write(Command.CREATE); 295 m_out.writeObject(m_targetImplName); 296 m_out.flush(); 297 m_handle = (String ) m_in.readObject(); 298 m_targetInterfaces = new Class [m_targetInterfaceNames.length]; 299 for (int i = 0; i < m_targetInterfaceNames.length; i++) { 300 try { 301 m_targetInterfaces[i] = Class.forName(m_targetInterfaceNames[i], false, m_loader); 302 } catch (ClassNotFoundException e) { 303 throw new WrappedRuntimeException(e); 304 } 305 } 306 } catch (Exception e) { 307 throw new WrappedRuntimeException(e); 308 } 309 } 310 m_proxy = Proxy.newProxyInstance(m_loader, m_targetInterfaces, this); 311 return m_proxy; 312 } 313 314 323 public Object invoke(final Object proxy, final Method method, final Object [] args) { 324 try { 325 m_out.write(Command.INVOKE); 326 m_out.writeObject(m_context); 327 m_out.writeObject(m_handle); 328 m_out.writeObject(method.getName()); 329 m_out.writeObject(method.getParameterTypes()); 330 m_out.writeObject(args); 331 m_out.flush(); 332 final Object response = m_in.readObject(); 333 if (response instanceof Exception ) { 334 throw (Exception ) response; 335 } 336 return response; 337 } catch (Exception e) { 338 throw new WrappedRuntimeException(e); 339 } 340 } 341 342 345 public void close() { 346 try { 347 m_out.write(Command.CLOSE); 348 m_out.flush(); 349 m_out.close(); 350 m_in.close(); 351 m_socket.close(); 352 } catch (IOException e) { 353 throw new WrappedRuntimeException(e); 354 } 355 } 356 357 363 public static Object getWrappedInstance(final String handle) { 364 return s_instances.get(handle); 365 } 366 367 373 public static String wrapInstance(final Object instance) { 374 final String handle = UuidGenerator.generate(instance); 375 s_instances.put(handle, instance); 376 return handle; 377 } 378 } | Popular Tags |