KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > transport > socket > SocketRmiServerThread


1 package org.sapia.ubik.rmi.server.transport.socket;
2
3 import java.io.EOFException JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InvalidClassException JavaDoc;
6 import java.io.NotSerializableException JavaDoc;
7
8 import org.sapia.ubik.net.Connection;
9 import org.sapia.ubik.net.PooledThread;
10 import org.sapia.ubik.net.Request;
11 import org.sapia.ubik.rmi.server.Config;
12 import org.sapia.ubik.rmi.server.Log;
13 import org.sapia.ubik.rmi.server.RMICommand;
14 import org.sapia.ubik.rmi.server.invocation.InvokeCommand;
15 import org.sapia.ubik.rmi.server.perf.PerfAnalyzer;
16 import org.sapia.ubik.rmi.server.transport.RmiConnection;
17
18
19 /**
20  * Implements a thread in a <code>SocketRmiServer</code> instance.
21  *
22  * @author Yanick Duchesne
23  * <dl>
24  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
25  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
26  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
27  * </dl>
28  */

29 public class SocketRmiServerThread extends PooledThread {
30   private PerfAnalyzer _perf = PerfAnalyzer.getInstance();
31   Connection _current;
32
33   SocketRmiServerThread() {
34   }
35
36   /**
37    * @see java.lang.Thread#interrupt()
38    */

39   public void interrupt() {
40     super.interrupt();
41
42     if (_current != null) {
43       _current.close();
44     }
45   }
46
47   /**
48    * @see org.sapia.ubik.net.PooledThread#doExec(Object)
49    */

50   protected void doExec(Object JavaDoc task) {
51     if (Log.isDebug()) {
52       Log.debug(getClass(), "handling request");
53     }
54
55     Request req = (Request) task;
56     _current = req.getConnection();
57
58     RMICommand cmd;
59     Object JavaDoc resp = null;
60
61     while (true) {
62       try {
63         if (Log.isDebug()) {
64           Log.debug(getClass(), "receiving command from: " + req.getConnection().getServerAddress());
65         }
66
67         cmd = (RMICommand) req.getConnection().receive();
68
69         if (Log.isDebug()) {
70           Log.debug(getClass(),
71             "command received: " + cmd.getClass().getName() + " from " +
72             req.getConnection().getServerAddress() + '@' + cmd.getVmId());
73         }
74
75         cmd.init(new Config(req.getServerAddress(), req.getConnection()));
76
77         try {
78           if (_perf.isEnabled()) {
79             if (cmd instanceof InvokeCommand) {
80               _perf.getTopic(getClass().getName() + ".RemoteCall").start();
81             }
82           }
83
84           resp = cmd.execute();
85
86           if (_perf.isEnabled()) {
87             if (cmd instanceof InvokeCommand) {
88               _perf.getTopic(getClass().getName() + ".RemoteCall").end();
89             }
90           }
91         } catch (Throwable JavaDoc t) {
92           t.printStackTrace();
93           t.fillInStackTrace();
94           resp = t;
95         }
96
97         if (_perf.isEnabled()) {
98           if (cmd instanceof InvokeCommand) {
99             _perf.getTopic(getClass().getName() + ".SendResponse").start();
100           }
101         }
102
103         ((RmiConnection) req.getConnection()).send(resp, cmd.getVmId(),
104           cmd.getServerAddress().getTransportType());
105
106         if (_perf.isEnabled()) {
107           if (cmd instanceof InvokeCommand) {
108             _perf.getTopic(getClass().getName() + ".SendResponse").end();
109           }
110         }
111       } catch (RuntimeException JavaDoc e) {
112         Log.error(getClass(), "RuntimeException caught sending response", e);
113
114         try {
115           e.fillInStackTrace();
116           req.getConnection().send(e);
117         } catch (IOException JavaDoc e2) {
118           req.getConnection().close();
119
120           break;
121         }
122       } catch (ClassNotFoundException JavaDoc e) {
123         e.fillInStackTrace();
124         Log.error(getClass(),
125           "Class not found while receiving sending request", e);
126
127         try {
128           req.getConnection().send(e);
129         } catch (IOException JavaDoc e2) {
130           e2.fillInStackTrace();
131           req.getConnection().close();
132
133           break;
134         }
135       } catch (EOFException JavaDoc e) {
136         e.fillInStackTrace();
137         req.getConnection().close();
138
139         break;
140       } catch (java.net.SocketException JavaDoc e) {
141         e.fillInStackTrace();
142         req.getConnection().close();
143
144         break;
145       } catch (NotSerializableException JavaDoc e) {
146         e.fillInStackTrace();
147         Log.error(getClass().getName(),
148           "Could not serialize class while sending response", e);
149
150         try {
151           req.getConnection().send(e);
152         } catch (IOException JavaDoc e2) {
153           req.getConnection().close();
154
155           break;
156         }
157       } catch (InvalidClassException JavaDoc e) {
158         e.fillInStackTrace();
159         Log.error(getClass(), "Class is invalid; object could not be sent", e);
160
161         e.fillInStackTrace();
162
163         try {
164           req.getConnection().send(e);
165         } catch (IOException JavaDoc e2) {
166           req.getConnection().close();
167
168           break;
169         }
170       } catch (java.io.IOException JavaDoc e) {
171         e.fillInStackTrace();
172
173         try {
174           req.getConnection().send(e);
175         } catch (IOException JavaDoc e2) {
176           req.getConnection().close();
177
178           break;
179         }
180       }
181     }
182   }
183 }
184
Popular Tags