KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > transport > http > HttpRmiServerThread


1 package org.sapia.ubik.rmi.server.transport.http;
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.PooledThread;
9 import org.sapia.ubik.net.Request;
10 import org.sapia.ubik.rmi.server.Config;
11 import org.sapia.ubik.rmi.server.Log;
12 import org.sapia.ubik.rmi.server.RMICommand;
13 import org.sapia.ubik.rmi.server.invocation.InvokeCommand;
14 import org.sapia.ubik.rmi.server.perf.PerfAnalyzer;
15 import org.sapia.ubik.rmi.server.transport.RmiConnection;
16
17
18 /**
19  * An instance of this class executes Ubik RMI commands.
20  *
21  * @author Yanick Duchesne
22  * <dl>
23  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
24  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
25  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
26  * </dl>
27  */

28 class HttpRmiServerThread extends PooledThread {
29   private PerfAnalyzer _perf = PerfAnalyzer.getInstance();
30
31   /**
32    * @see org.sapia.ubik.net.PooledThread#doExec(java.lang.Object)
33    */

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