1 15 16 package javassist.rmi; 17 18 import java.io.*; 19 import javassist.web.*; 20 import javassist.CannotCompileException; 21 import javassist.NotFoundException; 22 import javassist.ClassPool; 23 import java.lang.reflect.Method ; 24 import java.util.Hashtable ; 25 import java.util.Vector ; 26 27 36 public class AppletServer extends Webserver { 37 private StubGenerator stubGen; 38 private Hashtable exportedNames; 39 private Vector exportedObjects; 40 41 private static final byte[] okHeader 42 = "HTTP/1.0 200 OK\r\n\r\n".getBytes(); 43 44 49 public AppletServer(String port) 50 throws IOException, NotFoundException, CannotCompileException 51 { 52 this(Integer.parseInt(port)); 53 } 54 55 60 public AppletServer(int port) 61 throws IOException, NotFoundException, CannotCompileException 62 { 63 this(ClassPool.getDefault(), new StubGenerator(), port); 64 } 65 66 72 public AppletServer(int port, ClassPool src) 73 throws IOException, NotFoundException, CannotCompileException 74 { 75 this(new ClassPool(src), new StubGenerator(), port); 76 } 77 78 private AppletServer(ClassPool loader, StubGenerator gen, int port) 79 throws IOException, NotFoundException, CannotCompileException 80 { 81 super(port); 82 exportedNames = new Hashtable (); 83 exportedObjects = new Vector (); 84 stubGen = gen; 85 addTranslator(loader, gen); 86 } 87 88 91 public void run() { 92 super.run(); 93 } 94 95 107 public synchronized int exportObject(String name, Object obj) 108 throws CannotCompileException 109 { 110 Class clazz = obj.getClass(); 111 ExportedObject eo = new ExportedObject(); 112 eo.object = obj; 113 eo.methods = clazz.getMethods(); 114 exportedObjects.addElement(eo); 115 eo.identifier = exportedObjects.size() - 1; 116 if (name != null) 117 exportedNames.put(name, eo); 118 119 try { 120 stubGen.makeProxyClass(clazz); 121 } 122 catch (NotFoundException e) { 123 throw new CannotCompileException(e); 124 } 125 126 return eo.identifier; 127 } 128 129 132 public void doReply(InputStream in, OutputStream out, String cmd) 133 throws IOException, BadHttpRequest 134 { 135 if (cmd.startsWith("POST /rmi ")) 136 processRMI(in, out); 137 else if (cmd.startsWith("POST /lookup ")) 138 lookupName(cmd, in, out); 139 else 140 super.doReply(in, out, cmd); 141 } 142 143 private void processRMI(InputStream ins, OutputStream outs) 144 throws IOException 145 { 146 ObjectInputStream in = new ObjectInputStream(ins); 147 148 int objectId = in.readInt(); 149 int methodId = in.readInt(); 150 Exception err = null; 151 Object rvalue = null; 152 try { 153 ExportedObject eo 154 = (ExportedObject)exportedObjects.elementAt(objectId); 155 Object [] args = readParameters(in); 156 rvalue = convertRvalue(eo.methods[methodId].invoke(eo.object, 157 args)); 158 } 159 catch(Exception e) { 160 err = e; 161 logging2(e.toString()); 162 } 163 164 outs.write(okHeader); 165 ObjectOutputStream out = new ObjectOutputStream(outs); 166 if (err != null) { 167 out.writeBoolean(false); 168 out.writeUTF(err.toString()); 169 } 170 else 171 try { 172 out.writeBoolean(true); 173 out.writeObject(rvalue); 174 } 175 catch (NotSerializableException e) { 176 logging2(e.toString()); 177 } 178 catch (InvalidClassException e) { 179 logging2(e.toString()); 180 } 181 182 out.flush(); 183 out.close(); 184 in.close(); 185 } 186 187 private Object [] readParameters(ObjectInputStream in) 188 throws IOException, ClassNotFoundException 189 { 190 int n = in.readInt(); 191 Object [] args = new Object [n]; 192 for (int i = 0; i < n; ++i) { 193 Object a = in.readObject(); 194 if (a instanceof RemoteRef) { 195 RemoteRef ref = (RemoteRef)a; 196 ExportedObject eo 197 = (ExportedObject)exportedObjects.elementAt(ref.oid); 198 a = eo.object; 199 } 200 201 args[i] = a; 202 } 203 204 return args; 205 } 206 207 private Object convertRvalue(Object rvalue) 208 throws CannotCompileException 209 { 210 if (rvalue == null) 211 return null; 213 String classname = rvalue.getClass().getName(); 214 if (stubGen.isProxyClass(classname)) 215 return new RemoteRef(exportObject(null, rvalue), classname); 216 else 217 return rvalue; 218 } 219 220 private void lookupName(String cmd, InputStream ins, OutputStream outs) 221 throws IOException 222 { 223 ObjectInputStream in = new ObjectInputStream(ins); 224 String name = DataInputStream.readUTF(in); 225 ExportedObject found = (ExportedObject)exportedNames.get(name); 226 outs.write(okHeader); 227 ObjectOutputStream out = new ObjectOutputStream(outs); 228 if (found == null) { 229 logging2(name + "not found."); 230 out.writeInt(-1); out.writeUTF("error"); 232 } 233 else { 234 logging2(name); 235 out.writeInt(found.identifier); 236 out.writeUTF(found.object.getClass().getName()); 237 } 238 239 out.flush(); 240 out.close(); 241 in.close(); 242 } 243 } 244 245 class ExportedObject { 246 public int identifier; 247 public Object object; 248 public Method [] methods; 249 } 250 | Popular Tags |