1 4 package com.tc.aspectwerkz.connectivity; 5 6 import com.tc.aspectwerkz.exception.WrappedRuntimeException; 7 8 import java.io.IOException ; 9 import java.io.ObjectInputStream ; 10 import java.io.ObjectOutputStream ; 11 import java.net.Socket ; 12 13 20 public class RemoteProxyServerThread implements Runnable { 21 24 private final Socket m_socket; 25 26 29 private ObjectInputStream m_in = null; 30 31 34 private ObjectOutputStream m_out = null; 35 36 39 private ClassLoader m_loader = null; 40 41 44 private Invoker m_invoker = null; 45 46 49 private int m_timeout = 60000; 50 51 54 private boolean m_running = true; 55 56 63 public RemoteProxyServerThread(final Socket clientSocket, 64 final ClassLoader loader, 65 final Invoker invoker, 66 final int timeout) { 67 if (clientSocket == null) { 68 throw new IllegalArgumentException ("client socket can not be null"); 69 } 70 m_socket = clientSocket; 71 m_loader = loader; 72 m_invoker = invoker; 73 m_timeout = timeout; 74 } 75 76 79 public void run() { 80 Thread.currentThread().setContextClassLoader(m_loader); 81 try { 82 m_socket.setTcpNoDelay(true); 83 m_socket.setSoTimeout(m_timeout); 84 m_in = new ObjectInputStream (m_socket.getInputStream()); 85 m_out = new ObjectOutputStream (m_socket.getOutputStream()); 86 } catch (IOException e) { 87 throw new WrappedRuntimeException(e); 88 } 89 while (m_running) { 90 try { 91 switch (m_in.read()) { 92 case Command.CREATE: 93 handleCreateCommand(); 94 break; 95 case Command.INVOKE: 96 handleInvocationCommand(); 97 break; 98 case Command.CLOSE: 99 m_running = false; 100 break; 101 default: 102 break; 103 } 104 } catch (Exception e) { 105 close(); 106 throw new WrappedRuntimeException(e); 107 } 108 } 109 close(); 110 } 111 112 120 private void handleCreateCommand() throws IOException , 121 ClassNotFoundException , 122 InstantiationException , 123 IllegalAccessException { 124 final String className = (String ) m_in.readObject(); 125 Class klass = Class.forName(className, false, m_loader); 126 final Object instance = klass.newInstance(); 127 final String handle = RemoteProxy.wrapInstance(instance); 128 m_out.writeObject(handle); 129 m_out.flush(); 130 } 131 132 138 private void handleInvocationCommand() throws IOException , ClassNotFoundException { 139 final Object context = m_in.readObject(); 140 final String handle = (String ) m_in.readObject(); 141 final String methodName = (String ) m_in.readObject(); 142 final Class [] paramTypes = (Class []) m_in.readObject(); 143 final Object [] args = (Object []) m_in.readObject(); 144 Object result = null; 145 try { 146 result = m_invoker.invoke(handle, methodName, paramTypes, args, context); 147 } catch (Exception e) { 148 result = e; 149 } 150 m_out.writeObject(result); 151 m_out.flush(); 152 } 153 154 157 private void close() { 158 try { 159 if (m_in != null) { 160 m_in.close(); 161 } 162 if (m_out != null) { 163 m_out.close(); 164 } 165 if (m_socket != null) { 166 m_socket.close(); 167 } 168 } catch (IOException e) { 169 throw new WrappedRuntimeException(e); 170 } 171 } 172 } | Popular Tags |