KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.rmi.server.transport.http;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.rmi.RemoteException JavaDoc;
7
8 import org.apache.commons.httpclient.HttpClient;
9 import org.apache.commons.httpclient.methods.PostMethod;
10 import org.sapia.ubik.net.ServerAddress;
11 import org.sapia.ubik.rmi.server.VmId;
12 import org.sapia.ubik.rmi.server.transport.MarshalInputStream;
13 import org.sapia.ubik.rmi.server.transport.MarshalOutputStream;
14 import org.sapia.ubik.rmi.server.transport.RmiConnection;
15
16
17 /**
18  * Implements the <code>RmiConnection</code> interface over HTTP - more precisely,
19  * over a Jakarta HTTP client. Data is sent using the POST method.
20  * </p>
21  * An instance of this class is used on the client side.
22  *
23  * @see org.sapia.ubik.rmi.server.transport.http.HttpRmiServerConnection
24  *
25  * @author Yanick Duchesne
26  * <dl>
27  * <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>
28  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
29  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
30  * </dl>
31  */

32 public class HttpRmiClientConnection implements RmiConnection {
33   private static final int DEFAULT_BUFSZ = 1024;
34   private HttpAddress _address;
35   private HttpClient _client;
36   private PostMethod _post;
37   private boolean _closed;
38   private int _bufsz = DEFAULT_BUFSZ;
39
40   /**
41    * Creates an instance of this class with the given HTTP client and
42    * uri to connect to.
43    */

44   public HttpRmiClientConnection() {
45   }
46
47   /**
48    * @see org.sapia.ubik.rmi.server.transport.RmiConnection#send(java.lang.Object, org.sapia.ubik.rmi.server.VmId, java.lang.String)
49    */

50   public void send(Object JavaDoc o, VmId associated, String JavaDoc transportType)
51     throws IOException JavaDoc, RemoteException JavaDoc {
52     _post = new PostMethod(_address.toString());
53
54     ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(_bufsz);
55     MarshalOutputStream mos = new MarshalOutputStream(bos);
56
57     if ((associated != null) && (transportType != null)) {
58       mos.setUp(associated, transportType);
59     }
60
61     mos.writeObject(o);
62     mos.flush();
63     mos.close();
64
65     byte[] data = bos.toByteArray();
66
67     if (data.length > _bufsz) {
68       _bufsz = data.length;
69     }
70
71     _post.setRequestContentLength(data.length);
72     _post.setRequestBody(new ByteArrayInputStream JavaDoc(data));
73     _client.executeMethod(_post);
74   }
75
76   /**
77    * @see org.sapia.ubik.net.Connection#close()
78    */

79   public void close() {
80     if ((_post != null) && !_closed) {
81       _post.releaseConnection();
82       _closed = true;
83     }
84   }
85
86   /**
87    * @see org.sapia.ubik.net.Connection#getServerAddress()
88    */

89   public ServerAddress getServerAddress() {
90     return _address;
91   }
92
93   /**
94    * @see org.sapia.ubik.net.Connection#receive()
95    */

96   public Object JavaDoc receive()
97     throws IOException JavaDoc, ClassNotFoundException JavaDoc, RemoteException JavaDoc {
98     if (_post == null) {
99       throw new IllegalStateException JavaDoc("Cannot receive; data was not posted");
100     }
101
102     MarshalInputStream is = new MarshalInputStream(_post.getResponseBodyAsStream());
103
104     try {
105       return is.readObject();
106     } catch (IOException JavaDoc ioe) {
107       System.err.println(((ioe.getMessage() + "\nHTTP RESPONSE:\n" + _post) == null)
108         ? "null" : _post.getResponseBodyAsString());
109       throw ioe;
110     } finally {
111       is.close();
112     }
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     send(o, null, null);
120   }
121
122   HttpRmiClientConnection setUp(HttpClient client, HttpAddress addr) {
123     _closed = false;
124     _client = client;
125     _address = addr;
126
127     return this;
128   }
129 }
130
Popular Tags