KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > rmi > AppletServer


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

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 JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  * An AppletServer object is a web server that an ObjectImporter
29  * communicates with. It makes the objects specified by
30  * <code>exportObject()</code> remotely accessible from applets.
31  * If the classes of the exported objects are requested by the client-side
32  * JVM, this web server sends proxy classes for the requested classes.
33  *
34  * @see javassist.rmi.ObjectImporter
35  */

36 public class AppletServer extends Webserver {
37     private StubGenerator stubGen;
38     private Hashtable JavaDoc exportedNames;
39     private Vector JavaDoc exportedObjects;
40
41     private static final byte[] okHeader
42                                 = "HTTP/1.0 200 OK\r\n\r\n".getBytes();
43
44     /**
45      * Constructs a web server.
46      *
47      * @param port port number
48      */

49     public AppletServer(String JavaDoc port)
50         throws IOException, NotFoundException, CannotCompileException
51     {
52         this(Integer.parseInt(port));
53     }
54
55     /**
56      * Constructs a web server.
57      *
58      * @param port port number
59      */

60     public AppletServer(int port)
61         throws IOException, NotFoundException, CannotCompileException
62     {
63         this(ClassPool.getDefault(), new StubGenerator(), port);
64     }
65
66     /**
67      * Constructs a web server.
68      *
69      * @param port port number
70      * @param src the source of classs files.
71      */

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 JavaDoc();
83         exportedObjects = new Vector JavaDoc();
84         stubGen = gen;
85         addTranslator(loader, gen);
86     }
87
88     /**
89      * Begins the HTTP service.
90      */

91     public void run() {
92         super.run();
93     }
94
95     /**
96      * Exports an object.
97      * This method produces the bytecode of the proxy class used
98      * to access the exported object. A remote applet can load
99      * the proxy class and call a method on the exported object.
100      *
101      * @param name the name used for looking the object up.
102      * @param obj the exported object.
103      * @return the object identifier
104      *
105      * @see javassist.rmi.ObjectImporter#lookupObject(String)
106      */

107     public synchronized int exportObject(String JavaDoc name, Object JavaDoc obj)
108         throws CannotCompileException
109     {
110         Class JavaDoc 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     /**
130      * Processes a request from a web browser (an ObjectImporter).
131      */

132     public void doReply(InputStream in, OutputStream out, String JavaDoc 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 JavaDoc err = null;
151         Object JavaDoc rvalue = null;
152         try {
153             ExportedObject eo
154                 = (ExportedObject)exportedObjects.elementAt(objectId);
155             Object JavaDoc[] args = readParameters(in);
156             rvalue = convertRvalue(eo.methods[methodId].invoke(eo.object,
157                                                                args));
158         }
159         catch(Exception JavaDoc 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 JavaDoc[] readParameters(ObjectInputStream in)
188         throws IOException, ClassNotFoundException JavaDoc
189     {
190         int n = in.readInt();
191         Object JavaDoc[] args = new Object JavaDoc[n];
192         for (int i = 0; i < n; ++i) {
193             Object JavaDoc 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 JavaDoc convertRvalue(Object JavaDoc rvalue)
208         throws CannotCompileException
209     {
210         if (rvalue == null)
211             return null; // the return type is void.
212

213         String JavaDoc 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 JavaDoc cmd, InputStream ins, OutputStream outs)
221         throws IOException
222     {
223         ObjectInputStream in = new ObjectInputStream(ins);
224         String JavaDoc 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); // error code
231
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 JavaDoc object;
248     public Method JavaDoc[] methods;
249 }
250
Popular Tags