1 15 16 package javassist.rmi; 17 18 import java.io.*; 19 import java.net.*; 20 import java.applet.Applet ; 21 import java.lang.reflect.*; 22 23 73 public class ObjectImporter implements java.io.Serializable { 74 private final byte[] endofline = { 0x0d, 0x0a }; 75 private String servername, orgServername; 76 private int port, orgPort; 77 78 protected byte[] lookupCommand = "POST /lookup HTTP/1.0".getBytes(); 79 protected byte[] rmiCommand = "POST /rmi HTTP/1.0".getBytes(); 80 81 89 public ObjectImporter(Applet applet) { 90 URL codebase = applet.getCodeBase(); 91 orgServername = servername = codebase.getHost(); 92 orgPort = port = codebase.getPort(); 93 } 94 95 108 public ObjectImporter(String servername, int port) { 109 this.orgServername = this.servername = servername; 110 this.orgPort = this.port = port; 111 } 112 113 120 public Object getObject(String name) { 121 try { 122 return lookupObject(name); 123 } 124 catch (ObjectNotFoundException e) { 125 return null; 126 } 127 } 128 129 133 public void setHttpProxy(String host, int port) { 134 String proxyHeader = "POST http://" + orgServername + ":" + orgPort; 135 String cmd = proxyHeader + "/lookup HTTP/1.0"; 136 lookupCommand = cmd.getBytes(); 137 cmd = proxyHeader + "/rmi HTTP/1.0"; 138 rmiCommand = cmd.getBytes(); 139 this.servername = host; 140 this.port = port; 141 } 142 143 151 public Object lookupObject(String name) throws ObjectNotFoundException 152 { 153 try { 154 Socket sock = new Socket(servername, port); 155 OutputStream out = sock.getOutputStream(); 156 out.write(lookupCommand); 157 out.write(endofline); 158 out.write(endofline); 159 160 ObjectOutputStream dout = new ObjectOutputStream(out); 161 dout.writeUTF(name); 162 dout.flush(); 163 164 InputStream in = new BufferedInputStream(sock.getInputStream()); 165 skipHeader(in); 166 ObjectInputStream din = new ObjectInputStream(in); 167 int n = din.readInt(); 168 String classname = din.readUTF(); 169 din.close(); 170 dout.close(); 171 sock.close(); 172 173 if (n >= 0) 174 return createProxy(n, classname); 175 } 176 catch (Exception e) { 177 e.printStackTrace(); 178 throw new ObjectNotFoundException(name, e); 179 } 180 181 throw new ObjectNotFoundException(name); 182 } 183 184 private static final Class [] proxyConstructorParamTypes 185 = new Class [] { ObjectImporter.class, int.class }; 186 187 private Object createProxy(int oid, String classname) throws Exception { 188 Class c = Class.forName(classname); 189 Constructor cons = c.getConstructor(proxyConstructorParamTypes); 190 return cons.newInstance(new Object [] { this, new Integer (oid) }); 191 } 192 193 200 public Object call(int objectid, int methodid, Object [] args) 201 throws RemoteException 202 { 203 boolean result; 204 Object rvalue; 205 String errmsg; 206 207 try { 208 223 Socket sock = new Socket(servername, port); 224 OutputStream out = new BufferedOutputStream( 225 sock.getOutputStream()); 226 out.write(rmiCommand); 227 out.write(endofline); 228 out.write(endofline); 229 230 ObjectOutputStream dout = new ObjectOutputStream(out); 231 dout.writeInt(objectid); 232 dout.writeInt(methodid); 233 writeParameters(dout, args); 234 dout.flush(); 235 236 InputStream ins = new BufferedInputStream(sock.getInputStream()); 237 skipHeader(ins); 238 ObjectInputStream din = new ObjectInputStream(ins); 239 result = din.readBoolean(); 240 rvalue = null; 241 errmsg = null; 242 if (result) 243 rvalue = din.readObject(); 244 else 245 errmsg = din.readUTF(); 246 247 din.close(); 248 dout.close(); 249 sock.close(); 250 251 if (rvalue instanceof RemoteRef) { 252 RemoteRef ref = (RemoteRef)rvalue; 253 rvalue = createProxy(ref.oid, ref.classname); 254 } 255 } 256 catch (ClassNotFoundException e) { 257 throw new RemoteException(e); 258 } 259 catch (IOException e) { 260 throw new RemoteException(e); 261 } 262 catch (Exception e) { 263 throw new RemoteException(e); 264 } 265 266 if (result) 267 return rvalue; 268 else 269 throw new RemoteException(errmsg); 270 } 271 272 private void skipHeader(InputStream in) throws IOException { 273 int len; 274 do { 275 int c; 276 len = 0; 277 while ((c = in.read()) >= 0 && c != 0x0d) 278 ++len; 279 280 in.read(); 281 } while (len > 0); 282 } 283 284 private void writeParameters(ObjectOutputStream dout, Object [] params) 285 throws IOException 286 { 287 int n = params.length; 288 dout.writeInt(n); 289 for (int i = 0; i < n; ++i) 290 if (params[i] instanceof Proxy) { 291 Proxy p = (Proxy)params[i]; 292 dout.writeObject(new RemoteRef(p._getObjectId())); 293 } 294 else 295 dout.writeObject(params[i]); 296 } 297 } 298 | Popular Tags |