1 package org.sapia.ubik.rmi.server.transport.http; 2 3 import java.io.File ; 4 import java.net.InetAddress ; 5 import java.net.UnknownHostException ; 6 import java.rmi.RemoteException ; 7 import java.util.Collections ; 8 import java.util.HashMap ; 9 import java.util.Map ; 10 import java.util.Properties ; 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 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 e) { 48 } 49 } 50 51 private String _transportType; 52 private ServiceMapper _services = new ServiceMapper(); 53 private Map _pools = Collections.synchronizedMap(new HashMap ()); 54 55 public HttpTransportProvider() { 56 this(HttpConsts.DEFAULT_HTTP_TRANSPORT_TYPE, 57 new File (System.getProperty("user.dir"))); 58 } 59 60 63 public HttpTransportProvider(String transportType) { 64 this(transportType, new File (System.getProperty("user.dir"))); 65 } 66 67 70 public HttpTransportProvider(String transportType, File baseDir) { 71 _transportType = transportType; 72 _services.setBaseDir(baseDir); 73 } 74 75 79 public ServiceMapper getServices() { 80 return _services; 81 } 82 83 86 public synchronized Connections getPoolFor(ServerAddress address) 87 throws RemoteException { 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 ("Could not process given address", e); 101 } 102 } 103 104 return conns; 105 } 106 107 110 public String getTransportType() { 111 return _transportType; 112 } 113 114 117 public Server newDefaultServer() throws RemoteException { 118 throw new UnsupportedOperationException ( 119 "Transport provider does not support anonymous servers/dynamic ports"); 120 } 121 122 125 public Server newServer(Properties props) throws RemoteException { 126 Uri serverUrl; 127 String portStr = props.getProperty(HTTP_PORT_KEY); 128 String 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 ("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 ("Could not parse server URL", e); 160 } catch (UnknownHostException e) { 161 throw new RemoteException ("Could not acquire local address", e); 162 } 163 } 164 165 String 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 e) { 172 throw new RemoteException ("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 191 public void shutdown() { 192 } 193 194 202 public ServiceMapper getServiceMapper() { 203 return _services; 204 } 205 } 206 | Popular Tags |