1 package org.sapia.ubik.rmi.server.transport.http; 2 3 import java.io.IOException ; 4 import java.net.ServerSocket ; 5 import java.net.UnknownHostException ; 6 import java.rmi.RemoteException ; 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 32 class HttpRmiServer implements Server, HttpConsts { 33 private String _transportType; 34 private String _path; 35 private Uri _serverUrl; 36 private ServiceMapper _services; 37 private ServerSocket _server; 38 private HttpAddress _address; 39 private int _maxThreads = 0; 40 private int _localPort; 41 42 50 HttpRmiServer(ServiceMapper services, String transportType, Uri serverUrl, 51 String path, int localPort) { 52 if (serverUrl.getPort() <= 0) { 53 throw new IllegalStateException ("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 66 public void close() { 67 if (_server != null) { 68 try { 69 _server.close(); 70 } catch (Exception e) { 71 } 73 } 74 } 75 76 79 public ServerAddress getServerAddress() { 80 return _address; 81 } 82 83 88 void setMaxThreads(int max) { 89 _maxThreads = max; 90 } 91 92 95 public void start() throws RemoteException { 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 (_localPort); 106 conn.connect(_server); 107 } catch (UnknownHostException e) { 108 throw new RemoteException ("Could not acquire local address", e); 109 } catch (IOException e) { 110 throw new RemoteException ("Could not instantiate server socket", e); 111 } 112 } 113 } 114 | Popular Tags |