KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.rmi.server.transport.http;
2
3 import java.io.IOException JavaDoc;
4 import java.net.ServerSocket JavaDoc;
5 import java.net.UnknownHostException JavaDoc;
6 import java.rmi.RemoteException JavaDoc;
7
8 import org.sapia.ubik.net.ServerAddress;
9 import org.sapia.ubik.net.Uri;
10 import org.sapia.ubik.rmi.server.Server;
11
12 import simple.http.connect.Connection;
13 import simple.http.connect.ConnectionFactory;
14
15
16 /**
17  * Implements a <code>Server</code> that receives requests from Ubik RMI clients
18  * over HTTP.
19  * <p>
20  * An instance of this class maintains a thread pool. The threads are used to process
21  * incoming commands. Threads are released to the pool once their execution has completed.
22  *
23  * @see org.sapia.ubik.rmi.Consts#SERVER_MAX_THREADS
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 class HttpRmiServer implements Server, HttpConsts {
33   private String JavaDoc _transportType;
34   private String JavaDoc _path;
35   private Uri _serverUrl;
36   private ServiceMapper _services;
37   private ServerSocket JavaDoc _server;
38   private HttpAddress _address;
39   private int _maxThreads = 0;
40   private int _localPort;
41
42   /**
43    * Creates an instance of this class.
44    *
45    * @param services a <code>ServiceMapper</code>.
46    * @param transportType a "transport type" identifier.
47    * @param path the URL path to which this server will correspond.
48    * @param port a port (must be >= 0).
49    */

50   HttpRmiServer(ServiceMapper services, String JavaDoc transportType, Uri serverUrl,
51     String JavaDoc path, int localPort) {
52     if (serverUrl.getPort() <= 0) {
53       throw new IllegalStateException JavaDoc("Server does not support dynamic port");
54     }
55
56     _transportType = transportType;
57     _serverUrl = serverUrl;
58     _path = path;
59     _services = services;
60     _localPort = localPort;
61   }
62
63   /**
64    * @see org.sapia.ubik.rmi.server.Server#close()
65    */

66   public void close() {
67     if (_server != null) {
68       try {
69         _server.close();
70       } catch (Exception JavaDoc e) {
71         // noop
72
}
73     }
74   }
75
76   /**
77    * @see org.sapia.ubik.rmi.server.Server#getServerAddress()
78    */

79   public ServerAddress getServerAddress() {
80     return _address;
81   }
82
83   /**
84    * This method should be called before the <code>start()</code> method.
85    *
86    * @param max the maximum number of threads that this instance will create.
87    */

88   void setMaxThreads(int max) {
89     _maxThreads = max;
90   }
91
92   /**
93    * @see org.sapia.ubik.rmi.server.Server#start()
94    */

95   public void start() throws RemoteException JavaDoc {
96     try {
97       _address = new HttpAddress(_serverUrl);
98
99       UbikHttpHandler svc = new UbikHttpHandler(_serverUrl,
100           _services.getContext(), _maxThreads);
101       _services.addService(_path, svc);
102
103       HeaderHandler hh = new HeaderHandler(_services);
104       Connection conn = ConnectionFactory.getConnection(_services);
105       _server = new ServerSocket JavaDoc(_localPort);
106       conn.connect(_server);
107     } catch (UnknownHostException JavaDoc e) {
108       throw new RemoteException JavaDoc("Could not acquire local address", e);
109     } catch (IOException JavaDoc e) {
110       throw new RemoteException JavaDoc("Could not instantiate server socket", e);
111     }
112   }
113 }
114
Popular Tags