KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.rmi.server.transport.http.servlet;
2
3 import org.sapia.ubik.net.ServerAddress;
4 import org.sapia.ubik.rmi.server.VmId;
5 import org.sapia.ubik.rmi.server.transport.MarshalInputStream;
6 import org.sapia.ubik.rmi.server.transport.MarshalOutputStream;
7 import org.sapia.ubik.rmi.server.transport.RmiConnection;
8
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.OutputStream JavaDoc;
12
13 import java.rmi.RemoteException JavaDoc;
14
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17
18
19 /**
20  * Implements the <code>RmiConnection</code> interface over HTTP - more precisely,
21  * over <code>HttpServletRequest</code> and <code>HttpServletResponse</code> instances - from the Servlet
22  * API.
23  * </p>
24  * An instance of this class is used on the servlet-side only.
25  *
26  * @author Yanick Duchesne
27  *
28  * <dl>
29  * <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>
30  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
31  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
32  * </dl>
33  */

34 public class ServletRmiConnection implements RmiConnection {
35   static final int DEFAULT_BUFSZ = 1024;
36   private int _bufsz = DEFAULT_BUFSZ;
37   private HttpServletRequest JavaDoc _req;
38   private HttpServletResponse JavaDoc _res;
39   private ServletAddress _address;
40
41   /**
42    * Creates an instance of this class with the given request and
43    * response objects, as well as http server address.
44    */

45   ServletRmiConnection(ServletAddress address, HttpServletRequest JavaDoc req,
46     HttpServletResponse JavaDoc res) {
47     _req = req;
48     _res = res;
49     _address = address;
50   }
51
52   /**
53    * @see org.sapia.ubik.rmi.server.transport.RmiConnection#send(java.lang.Object, org.sapia.ubik.rmi.server.VmId, java.lang.String)
54    */

55   public void send(Object JavaDoc o, VmId associated, String JavaDoc transportType)
56     throws IOException JavaDoc, RemoteException JavaDoc {
57     try {
58       ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(_bufsz);
59       MarshalOutputStream mos = new MarshalOutputStream(bos);
60
61       if ((associated != null) && (transportType != null)) {
62         mos.setUp(associated, transportType);
63       }
64
65       mos.writeObject(o);
66       mos.flush();
67       mos.close();
68
69       byte[] data = bos.toByteArray();
70
71       if (data.length > _bufsz) {
72         _bufsz = data.length;
73       }
74
75       _res.setContentLength(data.length);
76
77       OutputStream os = _res.getOutputStream();
78       os.write(data);
79       os.flush();
80       os.close();
81     } catch (java.net.SocketException JavaDoc e) {
82       throw new RemoteException JavaDoc("communication with server interrupted; server probably disappeared",
83         e);
84     }
85   }
86
87   /**
88    * @see org.sapia.ubik.net.Connection#close()
89    */

90   public void close() {
91     try {
92       _res.getOutputStream().close();
93     } catch (Exception JavaDoc e) {
94       // noop
95
}
96   }
97
98   /**
99    * @see org.sapia.ubik.net.Connection#getServerAddress()
100    */

101   public ServerAddress getServerAddress() {
102     return _address;
103   }
104
105   /**
106    * @see org.sapia.ubik.net.Connection#receive()
107    */

108   public Object JavaDoc receive()
109     throws IOException JavaDoc, ClassNotFoundException JavaDoc, RemoteException JavaDoc {
110     MarshalInputStream is = new MarshalInputStream(_req.getInputStream());
111
112     return is.readObject();
113   }
114
115   /**
116    * @see org.sapia.ubik.net.Connection#send(java.lang.Object)
117    */

118   public void send(Object JavaDoc o) throws IOException JavaDoc, RemoteException JavaDoc {
119     try {
120       MarshalOutputStream os = new MarshalOutputStream(_res.getOutputStream());
121       os.writeObject(o);
122       os.flush();
123     } catch (java.net.SocketException JavaDoc e) {
124       throw new RemoteException JavaDoc("communication with server interrupted; server probably disappeared",
125         e);
126     }
127   }
128 }
129
Popular Tags