| 1 package org.sapia.ubik.rmi.server.transport.http.servlet; 2 3 import org.sapia.ubik.net.ServerAddress; 4 import org.sapia.ubik.rmi.server.VmId; 5 import org.sapia.ubik.rmi.server.transport.MarshalInputStream; 6 import org.sapia.ubik.rmi.server.transport.MarshalOutputStream; 7 import org.sapia.ubik.rmi.server.transport.RmiConnection; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.IOException ; 11 import java.io.OutputStream ; 12 13 import java.rmi.RemoteException ; 14 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 18 19 34 public class ServletRmiConnection implements RmiConnection { 35 static final int DEFAULT_BUFSZ = 1024; 36 private int _bufsz = DEFAULT_BUFSZ; 37 private HttpServletRequest _req; 38 private HttpServletResponse _res; 39 private ServletAddress _address; 40 41 45 ServletRmiConnection(ServletAddress address, HttpServletRequest req, 46 HttpServletResponse res) { 47 _req = req; 48 _res = res; 49 _address = address; 50 } 51 52 55 public void send(Object o, VmId associated, String transportType) 56 throws IOException , RemoteException { 57 try { 58 ByteArrayOutputStream bos = new ByteArrayOutputStream (_bufsz); 59 MarshalOutputStream mos = new MarshalOutputStream(bos); 60 61 if ((associated != null) && (transportType != null)) { 62 mos.setUp(associated, transportType); 63 } 64 65 mos.writeObject(o); 66 mos.flush(); 67 mos.close(); 68 69 byte[] data = bos.toByteArray(); 70 71 if (data.length > _bufsz) { 72 _bufsz = data.length; 73 } 74 75 _res.setContentLength(data.length); 76 77 OutputStream os = _res.getOutputStream(); 78 os.write(data); 79 os.flush(); 80 os.close(); 81 } catch (java.net.SocketException e) { 82 throw new RemoteException ("communication with server interrupted; server probably disappeared", 83 e); 84 } 85 } 86 87 90 public void close() { 91 try { 92 _res.getOutputStream().close(); 93 } catch (Exception e) { 94 } 96 } 97 98 101 public ServerAddress getServerAddress() { 102 return _address; 103 } 104 105 108 public Object receive() 109 throws IOException , ClassNotFoundException , RemoteException { 110 MarshalInputStream is = new MarshalInputStream(_req.getInputStream()); 111 112 return is.readObject(); 113 } 114 115 118 public void send(Object o) throws IOException , RemoteException { 119 try { 120 MarshalOutputStream os = new MarshalOutputStream(_res.getOutputStream()); 121 os.writeObject(o); 122 os.flush(); 123 } catch (java.net.SocketException e) { 124 throw new RemoteException ("communication with server interrupted; server probably disappeared", 125 e); 126 } 127 } 128 } 129 | Popular Tags |