1 28 29 package com.caucho.ejb.protocol; 30 31 import com.caucho.ejb.RemoteExceptionWrapper; 32 import com.caucho.server.util.CauchoSystem; 33 import com.caucho.vfs.ReadStream; 34 import com.caucho.vfs.TempStream; 35 import com.caucho.vfs.WriteStream; 36 37 import java.io.*; 38 import java.rmi.RemoteException ; 39 42 public class SelfSerializer { 43 TempStream trs; 44 45 ReadStream is; 46 WriteStream os; 47 48 ObjectInputStream ois; 49 ObjectOutputStream oos; 50 51 public static SelfSerializer allocate() 52 throws RemoteException 53 { 54 SelfSerializer ser = new SelfSerializer(); 55 56 ser.clear(); 57 58 return ser; 59 } 60 61 64 public void clear() 65 throws RemoteException 66 { 67 try { 68 trs = new TempStream(null); 69 os = new WriteStream(trs); 70 oos = new ObjectOutputStream(os); 71 is = null; 72 ois = null; 73 } catch (Exception e) { 74 throw new RemoteExceptionWrapper(e); 75 } 76 } 77 78 83 public void write(Object obj) 84 throws RemoteException 85 { 86 try { 87 oos.writeObject(obj); 88 } catch (Exception e) { 89 throw new RemoteExceptionWrapper(e); 90 } 91 } 92 93 96 public Object read() 97 throws RemoteException 98 { 99 try { 100 if (is == null) { 101 oos.flush(); 102 is = trs.openRead(true); 103 ois = new LoaderObjectInputStream(is); 104 } 105 106 return ois.readObject(); 107 } catch (Exception e) { 108 throw new RemoteExceptionWrapper(e); 109 } 110 } 111 112 115 public void close() 116 { 117 try { 118 InputStream is = this.is; 119 this.is = null; 120 OutputStream os = this.os; 121 this.os = null; 122 if (is != null) 123 is.close(); 124 if (os != null) 125 os.close(); 126 } catch (IOException e) { 127 } 128 } 129 130 134 static class LoaderObjectInputStream extends ObjectInputStream { 135 ClassLoader loader; 136 137 LoaderObjectInputStream(InputStream is) 138 throws IOException, StreamCorruptedException 139 { 140 super(is); 141 142 loader = Thread.currentThread().getContextClassLoader(); 143 } 144 145 148 protected Class resolveClass(ObjectStreamClass v) 149 throws IOException, ClassNotFoundException 150 { 151 return CauchoSystem.loadClass(v.getName(), false, loader); 152 } 153 } 154 } 155 | Popular Tags |