KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.rmi.server.transport.http;
2
3 import java.io.File JavaDoc;
4 import java.net.InetAddress JavaDoc;
5 import java.net.UnknownHostException JavaDoc;
6 import java.rmi.RemoteException JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import org.sapia.ubik.net.ServerAddress;
13 import org.sapia.ubik.net.Uri;
14 import org.sapia.ubik.net.UriSyntaxException;
15 import org.sapia.ubik.rmi.Consts;
16 import org.sapia.ubik.rmi.server.Log;
17 import org.sapia.ubik.rmi.server.Server;
18 import org.sapia.ubik.rmi.server.transport.Connections;
19 import org.sapia.ubik.rmi.server.transport.TransportProvider;
20
21
22 /**
23  * An instance of this class creates <code>HttpRmiServer</code> instances,
24  * as well as client-side connections (using Jakartas HTTP client). It is the
25  * entry-point into Ubik's HTTP tranport layer.
26  * <p>
27  * For the properties that an instance of this class takes (and their default values),
28  * see the <code>HttpConsts</code> interface.
29  *
30  * @see org.sapia.ubik.rmi.server.transport.http.HttpConsts
31  * @see org.sapia.ubik.rmi.server.transport.http.HttpRmiServer
32  *
33  * @author Yanick Duchesne
34  * <dl>
35  * <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>
36  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
37  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
38  * </dl>
39  */

40 public class HttpTransportProvider implements TransportProvider, HttpConsts {
41   private static boolean _usesJakarta;
42
43   static {
44     try {
45       Class.forName("org.apache.commons.httpclient.HttpClient");
46       _usesJakarta = true;
47     } catch (Exception JavaDoc e) {
48     }
49   }
50
51   private String JavaDoc _transportType;
52   private ServiceMapper _services = new ServiceMapper();
53   private Map JavaDoc _pools = Collections.synchronizedMap(new HashMap JavaDoc());
54
55   public HttpTransportProvider() {
56     this(HttpConsts.DEFAULT_HTTP_TRANSPORT_TYPE,
57       new File JavaDoc(System.getProperty("user.dir")));
58   }
59
60   /**
61    * @param transportType a "transport type" identifier.
62    */

63   public HttpTransportProvider(String JavaDoc transportType) {
64     this(transportType, new File JavaDoc(System.getProperty("user.dir")));
65   }
66
67   /**
68    * @param transportType a "transport type" identifier.
69    */

70   public HttpTransportProvider(String JavaDoc transportType, File JavaDoc baseDir) {
71     _transportType = transportType;
72     _services.setBaseDir(baseDir);
73   }
74
75   /**
76    * @return the <code>ServiceMapper</code> that holds this instance`s request handlers ("services", in
77    * the Simple API's terms).
78    */

79   public ServiceMapper getServices() {
80     return _services;
81   }
82
83   /**
84    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#getPoolFor(org.sapia.ubik.net.ServerAddress)
85    */

86   public synchronized Connections getPoolFor(ServerAddress address)
87     throws RemoteException JavaDoc {
88     Connections conns;
89
90     if ((conns = (Connections) _pools.get(address)) == null) {
91       try {
92         if (_usesJakarta) {
93           conns = new HttpClientConnectionPool((HttpAddress) address);
94         } else {
95           conns = new JdkClientConnectionPool((HttpAddress) address);
96         }
97
98         _pools.put(address, conns);
99       } catch (UriSyntaxException e) {
100         throw new RemoteException JavaDoc("Could not process given address", e);
101       }
102     }
103
104     return conns;
105   }
106
107   /**
108    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#getTransportType()
109    */

110   public String JavaDoc getTransportType() {
111     return _transportType;
112   }
113
114   /**
115    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#newDefaultServer()
116    */

117   public Server newDefaultServer() throws RemoteException JavaDoc {
118     throw new UnsupportedOperationException JavaDoc(
119       "Transport provider does not support anonymous servers/dynamic ports");
120   }
121
122   /**
123    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#newServer(java.util.Properties)
124    */

125   public Server newServer(Properties JavaDoc props) throws RemoteException JavaDoc {
126     Uri serverUrl;
127     String JavaDoc portStr = props.getProperty(HTTP_PORT_KEY);
128     String JavaDoc contextPath;
129     int port = DEFAULT_HTTP_PORT;
130
131     if (portStr == null) {
132       Log.warning(getClass(),
133         "Property '" + HTTP_PORT_KEY + "' not specified; using default: " +
134         port);
135     } else {
136       port = Integer.parseInt(portStr);
137     }
138
139     if (props.getProperty(SERVER_URL_KEY) != null) {
140       try {
141         serverUrl = Uri.parse(props.getProperty(SERVER_URL_KEY));
142
143         if (serverUrl.getPort() == Uri.UNDEFINED_PORT) {
144           serverUrl.setPort(DEFAULT_HTTP_PORT);
145         }
146
147         contextPath = serverUrl.getPath();
148       } catch (UriSyntaxException e) {
149         throw new RemoteException JavaDoc("Could not parse server URL", e);
150       }
151     } else {
152       contextPath = props.getProperty(PATH_KEY, DEFAULT_CONTEXT_PATH);
153
154       try {
155         serverUrl = Uri.parse("http://" +
156             InetAddress.getLocalHost().getHostAddress() + ":" + port + "/" +
157             contextPath);
158       } catch (UriSyntaxException e) {
159         throw new RemoteException JavaDoc("Could not parse server URL", e);
160       } catch (UnknownHostException JavaDoc e) {
161         throw new RemoteException JavaDoc("Could not acquire local address", e);
162       }
163     }
164
165     String JavaDoc maxThreads = props.getProperty(Consts.SERVER_MAX_THREADS);
166     int max = 0;
167
168     if (maxThreads != null) {
169       try {
170         max = Integer.parseInt(maxThreads);
171       } catch (NumberFormatException JavaDoc e) {
172         throw new RemoteException JavaDoc("Invalid value for '" +
173           Consts.SERVER_MAX_THREADS + "' : " + maxThreads);
174       }
175
176       if (max < 0) {
177         max = 0;
178       }
179     }
180
181     HttpRmiServer svr = new HttpRmiServer(_services, _transportType, serverUrl,
182         contextPath, port);
183     svr.setMaxThreads(max);
184
185     return svr;
186   }
187
188   /**
189    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#shutdown()
190    */

191   public void shutdown() {
192   }
193
194   /**
195    * Returns the service mapper that is held within this instance. Other
196    * services can be added, associated to different context paths. This allows
197    * other types of requests (non-Ubik ones) to be processed by the same HTTP
198    * server.
199    *
200    * @return the <code>ServiceMapper</code> that this instance holds.
201    */

202   public ServiceMapper getServiceMapper() {
203     return _services;
204   }
205 }
206
Popular Tags