KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.rmi.server.transport.http;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.OutputStream JavaDoc;
6 import java.rmi.RemoteException JavaDoc;
7
8 import org.sapia.ubik.net.ServerAddress;
9 import org.sapia.ubik.rmi.server.VmId;
10 import org.sapia.ubik.rmi.server.transport.MarshalInputStream;
11 import org.sapia.ubik.rmi.server.transport.MarshalOutputStream;
12 import org.sapia.ubik.rmi.server.transport.RmiConnection;
13
14 import simple.http.Request;
15 import simple.http.Response;
16
17
18 /**
19  * Implements the <code>RmiConnection</code> interface over HTTP - more precisely,
20  * over <code>Request</code> and <code>Response</code> instances - from the Simple
21  * API - see the <a HREF="http://simpleweb.sf.net/">Simple website</a> for more info.
22  * </p>
23  * An instance of this class is used on the server side.
24  *
25  * @see HttpRmiClientConnection
26  *
27  * @author Yanick Duchesne
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 class HttpRmiServerConnection implements RmiConnection {
35   static final int DEFAULT_BUFSZ = 1024;
36   private int _bufsz = DEFAULT_BUFSZ;
37   private Request _req;
38   private Response _res;
39   private HttpAddress _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   HttpRmiServerConnection(HttpAddress address, Request req, Response res) {
46     _req = req;
47     _res = res;
48     _address = address;
49   }
50
51   /**
52    * @see org.sapia.ubik.rmi.server.transport.RmiConnection#send(java.lang.Object, org.sapia.ubik.rmi.server.VmId, java.lang.String)
53    */

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

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

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

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

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