KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > connectivity > RemoteProxyServerThread


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.connectivity;
5
6 import com.tc.aspectwerkz.exception.WrappedRuntimeException;
7
8 import java.io.IOException JavaDoc;
9 import java.io.ObjectInputStream JavaDoc;
10 import java.io.ObjectOutputStream JavaDoc;
11 import java.net.Socket JavaDoc;
12
13 /**
14  * Implements a server thread. Each request from the client gets its own instance. <p/>Response to three different
15  * commands: <br/>Command.CREATE, Command.INVOKE and Command.CLOSE. <p/>It redirects the method invocation to the
16  * Invoker for the class.
17  *
18  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
19  */

20 public class RemoteProxyServerThread implements Runnable JavaDoc {
21   /**
22    * The socket.
23    */

24   private final Socket JavaDoc m_socket;
25
26   /**
27    * The input stream.
28    */

29   private ObjectInputStream JavaDoc m_in = null;
30
31   /**
32    * The output stream.
33    */

34   private ObjectOutputStream JavaDoc m_out = null;
35
36   /**
37    * The class loader to use.
38    */

39   private ClassLoader JavaDoc m_loader = null;
40
41   /**
42    * The custom invoker instance.
43    */

44   private Invoker m_invoker = null;
45
46   /**
47    * The time-out for the socket.
48    */

49   private int m_timeout = 60000;
50
51   /**
52    * Is-running flag.
53    */

54   private boolean m_running = true;
55
56   /**
57    * Creates a new instance.
58    *
59    * @param clientSocket the client socket
60    * @param loader the classloader to use
61    * @param invoker the invoker that makes the method invocation in the client thread
62    */

63   public RemoteProxyServerThread(final Socket JavaDoc clientSocket,
64                                  final ClassLoader JavaDoc loader,
65                                  final Invoker invoker,
66                                  final int timeout) {
67     if (clientSocket == null) {
68       throw new IllegalArgumentException JavaDoc("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   /**
77    * Does the actual work of serving the client.
78    */

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 JavaDoc(m_socket.getInputStream());
85       m_out = new ObjectOutputStream JavaDoc(m_socket.getOutputStream());
86     } catch (IOException JavaDoc 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 JavaDoc e) {
105         close();
106         throw new WrappedRuntimeException(e);
107       }
108     }
109     close();
110   }
111
112   /**
113    * Handles the command CREATE.
114    *
115    * @throws IOException
116    * @throws ClassNotFoundException
117    * @throws InstantiationException
118    * @throws IllegalAccessException
119    */

120   private void handleCreateCommand() throws IOException JavaDoc,
121           ClassNotFoundException JavaDoc,
122           InstantiationException JavaDoc,
123           IllegalAccessException JavaDoc {
124     final String JavaDoc className = (String JavaDoc) m_in.readObject();
125     Class JavaDoc klass = Class.forName(className, false, m_loader);
126     final Object JavaDoc instance = klass.newInstance();
127     final String JavaDoc handle = RemoteProxy.wrapInstance(instance);
128     m_out.writeObject(handle);
129     m_out.flush();
130   }
131
132   /**
133    * Handles the command INVOKE.
134    *
135    * @throws IOException
136    * @throws ClassNotFoundException
137    */

138   private void handleInvocationCommand() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
139     final Object JavaDoc context = m_in.readObject();
140     final String JavaDoc handle = (String JavaDoc) m_in.readObject();
141     final String JavaDoc methodName = (String JavaDoc) m_in.readObject();
142     final Class JavaDoc[] paramTypes = (Class JavaDoc[]) m_in.readObject();
143     final Object JavaDoc[] args = (Object JavaDoc[]) m_in.readObject();
144     Object JavaDoc result = null;
145     try {
146       result = m_invoker.invoke(handle, methodName, paramTypes, args, context);
147     } catch (Exception JavaDoc e) {
148       result = e;
149     }
150     m_out.writeObject(result);
151     m_out.flush();
152   }
153
154   /**
155    * Close the input/output streams along with the socket.
156    */

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 JavaDoc e) {
169       throw new WrappedRuntimeException(e);
170     }
171   }
172 }
Popular Tags