1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*
* This file is part of the Javassist toolkit.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Javassist.
*
* The Initial Developer of the Original Code is Shigeru Chiba. Portions
* created by Shigeru Chiba are Copyright (C) 1999-2003 Shigeru Chiba.
* All Rights Reserved.
*
* Contributor(s):
*
* The development of this software is supported in part by the PRESTO
* program (Sakigake Kenkyu 21) of Japan Science and Technology Corporation.
*/
package javassist.rmi;
import java.io.*;
import javassist.web.*;
import javassist.CannotCompileException;
import javassist.NotFoundException;
import javassist.ClassPool;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Vector;
/**
* An AppletServer object is a web server that an ObjectImporter
* communicates with. It makes the objects specified by
* <code>exportObject()</code> remotely accessible from applets.
* If the classes of the exported objects are requested by the client-side
* JVM, this web server sends proxy classes for the requested classes.
*
* @see javassist.rmi.ObjectImporter
*/
public class AppletServer extends Webserver {
private StubGenerator stubGen;
private Hashtable exportedNames;
private Vector exportedObjects;
private static final byte[] okHeader
= "HTTP/1.0 200 OK\r\n\r\n".getBytes();
/**
* Constructs a web server.
*
* @param port port number
*/
public AppletServer(String port)
throws IOException, NotFoundException, CannotCompileException
{
this(Integer.parseInt(port));
}
/**
* Constructs a web server.
*
* @param port port number
*/
public AppletServer(int port)
throws IOException, NotFoundException, CannotCompileException
{
this(ClassPool.getDefault(new StubGenerator()), port);
}
/**
* Constructs a web server.
*
* @param port port number
* @param src the source of classs files.
*/
public AppletServer(int port, ClassPool src)
throws IOException, NotFoundException, CannotCompileException
{
this(new ClassPool(src, new StubGenerator()), port);
}
private AppletServer(ClassPool loader, int port)
throws IOException, NotFoundException, CannotCompileException
{
super(port);
exportedNames = new Hashtable();
exportedObjects = new Vector();
stubGen = (StubGenerator)loader.getTranslator();
setClassPool(loader);
}
/**
* Begins the HTTP service.
*/
public void run() {
super.run();
}
/**
* Exports an object.
* This method produces the bytecode of the proxy class used
* to access the exported object. A remote applet can load
* the proxy class and call a method on the exported object.
*
* @param name the name used for looking the object up.
* @param object the exported object.
* @return the object identifier
*
* @see javassist.rmi.ObjectImporter#lookupObject(String)
*/
public synchronized int exportObject(String name, Object obj)
throws CannotCompileException
{
Class clazz = obj.getClass();
ExportedObject eo = new ExportedObject();
eo.object = obj;
eo.methods = clazz.getMethods();
exportedObjects.addElement(eo);
eo.identifier = exportedObjects.size() - 1;
if (name != null)
exportedNames.put(name, eo);
try {
stubGen.makeProxyClass(clazz);
}
catch (NotFoundException e) {
throw new CannotCompileException(e);
}
return eo.identifier;
}
/**
* Processes a request from a web browser (an ObjectImporter).
*/
public void doReply(InputStream in, OutputStream out, String cmd)
throws IOException, BadHttpRequest
{
if (cmd.startsWith("POST /rmi "))
processRMI(in, out);
else if (cmd.startsWith("POST /lookup "))
lookupName(cmd, in, out);
else
super.doReply(in, out, cmd);
}
private void processRMI(InputStream ins, OutputStream outs)
throws IOException
{
ObjectInputStream in = new ObjectInputStream(ins);
int objectId = in.readInt();
int methodId = in.readInt();
Exception err = null;
Object rvalue = null;
try {
ExportedObject eo
= (ExportedObject)exportedObjects.elementAt(objectId);
Object[] args = readParameters(in);
rvalue = convertRvalue(eo.methods[methodId].invoke(eo.object,
args));
}
catch(Exception e) {
err = e;
logging2(e.toString());
}
outs.write(okHeader);
ObjectOutputStream out = new ObjectOutputStream(outs);
if (err != null) {
out.writeBoolean(false);
out.writeUTF(err.toString());
}
else
try {
out.writeBoolean(true);
out.writeObject(rvalue);
}
catch (NotSerializableException e) {
logging2(e.toString());
}
catch (InvalidClassException e) {
logging2(e.toString());
}
out.flush();
out.close();
in.close();
}
private Object[] readParameters(ObjectInputStream in)
throws IOException, ClassNotFoundException
{
int n = in.readInt();
Object[] args = new Object[n];
for (int i = 0; i < n; ++i) {
Object a = in.readObject();
if (a instanceof RemoteRef) {
RemoteRef ref = (RemoteRef)a;
ExportedObject eo
= (ExportedObject)exportedObjects.elementAt(ref.oid);
a = eo.object;
}
args[i] = a;
}
return args;
}
private Object convertRvalue(Object rvalue)
throws CannotCompileException
{
if (rvalue == null)
return null; // the return type is void.
String classname = rvalue.getClass().getName();
if (stubGen.isProxyClass(classname))
return new RemoteRef(exportObject(null, rvalue), classname);
else
return rvalue;
}
private void lookupName(String cmd, InputStream ins, OutputStream outs)
throws IOException
{
ObjectInputStream in = new ObjectInputStream(ins);
String name = DataInputStream.readUTF(in);
ExportedObject found = (ExportedObject)exportedNames.get(name);
outs.write(okHeader);
ObjectOutputStream out = new ObjectOutputStream(outs);
if (found == null) {
logging2(name + "not found.");
out.writeInt(-1); // error code
out.writeUTF("error");
}
else {
logging2(name);
out.writeInt(found.identifier);
out.writeUTF(found.object.getClass().getName());
}
out.flush();
out.close();
in.close();
}
}
class ExportedObject {
public int identifier;
public Object object;
public Method[] methods;
}
|