]> gcc.gnu.org Git - gcc.git/blob - libjava/gnu/java/rmi/server/UnicastRef.java
ObjectInputStream.java (resolveProxyClass): New method from Classpath.
[gcc.git] / libjava / gnu / java / rmi / server / UnicastRef.java
1 /*
2 Copyright (c) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package gnu.java.rmi.server;
39
40 import java.rmi.Remote;
41 import java.rmi.RemoteException;
42 import java.rmi.server.RemoteRef;
43 import java.rmi.server.RMISocketFactory;
44 import java.rmi.server.RMIClientSocketFactory;
45 import java.rmi.server.RMIServerSocketFactory;
46 import java.rmi.server.RemoteObject;
47 import java.rmi.server.RemoteCall;
48 import java.rmi.server.UnicastRemoteObject;
49 import java.rmi.server.Operation;
50 import java.rmi.server.ObjID;
51 import java.rmi.server.UID;
52 import java.lang.reflect.Method;
53 import java.io.ObjectOutput;
54 import java.io.ObjectInput;
55 import java.io.IOException;
56 import java.net.Socket;
57 import java.net.InetAddress;
58 import java.io.BufferedInputStream;
59 import java.io.BufferedOutputStream;
60 import java.io.ObjectInputStream;
61 import java.io.ObjectOutputStream;
62 import java.io.DataInputStream;
63 import java.io.DataOutputStream;
64
65 public class UnicastRef
66 implements RemoteRef, ProtocolConstants {
67
68 public ObjID objid;
69 UnicastConnectionManager manager;
70
71 /**
72 * Used by serialization.
73 */
74 private UnicastRef() {
75 }
76
77 public UnicastRef(ObjID objid, String host, int port, RMIClientSocketFactory csf) {
78 this(objid);
79 manager = UnicastConnectionManager.getInstance(host, port, csf);
80 }
81
82 public UnicastRef(ObjID objid) {
83 this.objid = objid;
84 }
85
86 public Object invoke(Remote obj, Method method, Object[] params, long opnum) throws Exception {
87 return (invokeCommon(obj, method, params, -1, opnum));
88 }
89
90 private Object invokeCommon(Remote obj, Method method, Object[] params, int opnum, long hash) throws Exception {
91 UnicastConnection conn;
92 try {
93 conn = manager.getConnection();
94 }
95 catch (IOException e1) {
96 throw new RemoteException("connection failed to host: " + manager.serverName, e1);
97 }
98
99 ObjectOutputStream out;
100 DataOutputStream dout;
101 try {
102 dout = conn.getDataOutputStream();
103 dout.writeByte(MESSAGE_CALL);
104
105 out = conn.getObjectOutputStream();
106
107 objid.write(out);
108 out.writeInt(opnum);
109 out.writeLong(hash);
110 /*
111 if (params != null) {
112 for (int i = 0; i < params.length; i++) {
113 if (params[i] instanceof UnicastRemoteObject) {
114 out.writeObject(UnicastRemoteObject.exportObject((UnicastRemoteObject)params[i]));
115 }
116 else {
117 out.writeObject(params[i]);
118 }
119 }
120 }
121 */
122 // must handle primitive class and their wrapper classes
123 Class clss[] = method.getParameterTypes();
124 for(int i = 0; i < clss.length; i++)
125 ((RMIObjectOutputStream)out).writeValue(params[i], clss[i]);
126
127 out.flush();
128 }
129 catch (IOException e2) {
130 throw new RemoteException("call failed: ", e2);
131 }
132
133 int returncode;
134 Object returnval;
135 DataInputStream din;
136 ObjectInputStream in;
137 UID ack;
138 try {
139 din = conn.getDataInputStream();
140 if (din.readUnsignedByte() != MESSAGE_CALL_ACK) {
141 throw new RemoteException("Call not acked");
142 }
143
144 in = conn.getObjectInputStream();
145
146 returncode = in.readUnsignedByte();
147 ack = UID.read(in);
148 //returnval = in.readObject();
149 Class cls = method.getReturnType();
150 if(cls == Void.TYPE){
151 returnval = null;
152 }else
153 returnval = ((RMIObjectInputStream)in).readValue(cls);
154 }
155 catch (IOException e3) {
156 throw new RemoteException("call return failed: ", e3);
157 }
158
159 /* if DGCAck is necessary
160 //According to RMI wire protocol, send a DGCAck
161 // to indicate receiving return value
162 dout.writeByte(MESSAGE_DGCACK);
163 ack.write(dout);
164 out.flush();
165 */
166
167 manager.discardConnection(conn);
168
169 if (returncode != RETURN_ACK) {
170 throw (Exception)returnval;
171 }
172
173 return (returnval);
174 }
175
176 /**
177 * @deprecated
178 */
179 public RemoteCall newCall(RemoteObject obj, Operation[] op, int opnum, long hash) throws RemoteException {
180 return (new UnicastRemoteCall(obj, opnum, hash));
181 }
182
183 /**
184 * @deprecated
185 */
186 public void invoke(RemoteCall call) throws Exception {
187 UnicastRemoteCall c = (UnicastRemoteCall)call;
188 Object ret = invokeCommon((Remote)c.getObject(), (Method)null, c.getArguments(), c.getOpnum(), c.getHash());
189 c.setReturnValue(ret);
190 }
191
192 /**
193 * @deprecated
194 */
195 public void done(RemoteCall call) throws RemoteException {
196 /* Does nothing */
197 }
198
199 public void writeExternal(ObjectOutput out) throws IOException {
200 if (manager == null) {
201 throw new IOException("no connection");
202 }
203 manager.write(out);
204 objid.write(out);
205 // This byte is somewhat confusing when interoperating with JDK
206 out.writeByte(0); //RETURN_ACK);
207 }
208
209 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
210 manager = UnicastConnectionManager.read(in);
211 objid = ObjID.read(in);
212 byte ack = in.readByte();
213 // This byte is somewhat confusing when interoperating with JDK
214 if (ack != RETURN_ACK && ack != 0/*jdk ack value*/) {
215 throw new IOException("no ack found");
216 }
217 }
218
219 public boolean remoteEquals(RemoteRef ref) {
220 throw new Error("Not implemented");
221 }
222
223 public int remoteHashCode() {
224 throw new Error("Not implemented");
225 }
226
227 public String getRefClass(ObjectOutput out) {
228 return ("UnicastRef");
229 }
230
231 public String remoteToString() {
232 throw new Error("Not implemented");
233 }
234
235 public void dump(UnicastConnection conn) {
236 try {
237 DataInputStream din = conn.getDataInputStream();
238 for (;;) {
239 int b = din.readUnsignedByte();
240 System.out.print(Integer.toHexString(b));
241 if (b >= 32 && b < 128) {
242 System.out.print(": " + (char)b);
243 }
244 System.out.println();
245 }
246 }
247 catch (IOException _) {
248 }
249 }
250
251 }
This page took 0.048147 seconds and 5 git commands to generate.