Merge Orp RMI patches from Wu Gansha <gansha.wu@intel.com>
[official-gcc.git] / libjava / gnu / java / rmi / server / UnicastRemoteCall.java
blob734002aaa65f4c763b04f63f54560fb7d2afcc49
1 /* UnicastRemoteCall.java
2 Copyright (c) 1996, 1997, 1998, 1999, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package gnu.java.rmi.server;
40 import java.lang.Exception;
41 import java.io.DataInputStream;
42 import java.io.DataOutputStream;
43 import java.io.IOException;
44 import java.io.ObjectOutput;
45 import java.io.ObjectInput;
46 import java.io.StreamCorruptedException;
47 import java.rmi.server.RemoteCall;
48 import java.rmi.RemoteException;
49 import java.rmi.MarshalException;
50 import java.rmi.UnmarshalException;
51 import java.rmi.server.UID;
52 import java.rmi.server.ObjID;
53 import java.rmi.server.RemoteObject;
55 import java.util.Vector;
57 public class UnicastRemoteCall
58 implements RemoteCall, ProtocolConstants
61 private UnicastConnection conn;
62 private Object result;
63 private Object object;
64 private int opnum;
65 private long hash;
66 private Vector vec;
67 private int ptr;
69 private ObjectOutput oout;
70 private ObjectInput oin;
72 /**
73 * Incoming call.
75 UnicastRemoteCall(UnicastConnection conn)
77 this.conn = conn;
80 /**
81 * Outgoing call.
83 UnicastRemoteCall(UnicastConnection conn, ObjID objid, int opnum, long hash)
84 throws RemoteException
86 this.conn = conn;
87 this.opnum = opnum;
88 this.hash = hash;
90 // signal the call when constructing
91 try
93 DataOutputStream dout = conn.getDataOutputStream();
94 dout.write(MESSAGE_CALL);
96 oout = conn.getObjectOutputStream();
97 objid.write(oout);
98 oout.writeInt(opnum);
99 oout.writeLong(hash);
101 catch(IOException ex)
103 throw new MarshalException("Try to write header but failed.", ex);
107 UnicastConnection getConnection()
109 return conn;
112 public ObjectOutput getOutputStream() throws IOException
114 if (conn != null)
116 if(oout == null)
117 return (oout = conn.getObjectOutputStream());
118 else
119 return oout;
121 else
123 vec = new Vector();
124 return (new DummyObjectOutputStream());
128 public void releaseOutputStream() throws IOException
130 if(oout != null)
131 oout.flush();
134 public ObjectInput getInputStream() throws IOException
136 if (conn != null)
138 if(oin == null)
139 return (oin = conn.getObjectInputStream());
140 else
141 return oin;
143 else
145 ptr = 0;
146 return (new DummyObjectInputStream());
150 public void releaseInputStream() throws IOException
152 // Does nothing.
155 public ObjectOutput getResultStream(boolean success)
156 throws IOException, StreamCorruptedException
158 vec = new Vector();
159 return new DummyObjectOutputStream();
162 public void executeCall() throws Exception
164 byte returncode;
165 ObjectInput oin;
168 releaseOutputStream();
169 DataInputStream din = conn.getDataInputStream();
170 if (din.readByte() != MESSAGE_CALL_ACK)
171 throw new RemoteException("Call not acked");
173 oin = getInputStream();
174 returncode = oin.readByte();
175 UID.read(oin);
177 catch(IOException ex)
179 throw new UnmarshalException("Try to read header but failed:", ex);
182 //check return code
183 switch(returncode)
185 case RETURN_ACK: //it's ok
186 return;
187 case RETURN_NACK:
188 Object returnobj;
191 returnobj = oin.readObject();
193 catch(Exception ex2)
195 throw new UnmarshalException
196 ("Try to read exception object but failed", ex2);
199 if(!(returnobj instanceof Exception))
200 throw new UnmarshalException("Should be Exception type here: "
201 + returnobj);
202 throw (Exception)returnobj;
204 default:
205 throw new UnmarshalException("Invalid return code");
209 public void done() throws IOException
211 // conn.disconnect();
214 Object returnValue()
216 return vec.elementAt(0);
219 Object[] getArguments()
221 return vec.toArray();
224 Object getObject()
226 return object;
229 int getOpnum()
231 return opnum;
234 long getHash()
236 return hash;
239 void setReturnValue(Object obj)
241 vec.removeAllElements();
242 vec.addElement(obj);
246 * Dummy object output class.
248 private class DummyObjectOutputStream implements ObjectOutput
251 * Non-private constructor to reduce bytecode emitted.
253 DummyObjectOutputStream()
257 public void writeBoolean(boolean v) throws IOException
259 vec.addElement(new Boolean(v));
262 public void writeByte(int v) throws IOException
264 vec.addElement(new Byte((byte) v));
267 public void writeChar(int v) throws IOException
269 vec.addElement(new Character((char) v));
272 public void writeDouble(double v) throws IOException
274 vec.addElement(new Double(v));
277 public void writeFloat(float v) throws IOException
279 vec.addElement(new Float(v));
282 public void writeInt(int v) throws IOException
284 vec.addElement(new Integer(v));
287 public void writeLong(long v) throws IOException
289 vec.addElement(new Long(v));
292 public void writeShort(int v) throws IOException
294 vec.addElement(new Short((short) v));
297 public void writeObject(Object obj) throws IOException
299 vec.addElement(obj);
302 public void write(byte b[]) throws IOException
304 throw new IOException("not required");
307 public void write(byte b[], int off, int len) throws IOException
309 throw new IOException("not required");
312 public void write(int b) throws IOException
314 throw new IOException("not required");
317 public void writeBytes(String s) throws IOException
319 throw new IOException("not required");
322 public void writeChars(String s) throws IOException
324 throw new IOException("not required");
327 public void writeUTF(String str) throws IOException
329 throw new IOException("not required");
332 public void flush() throws IOException
336 public void close() throws IOException
339 } // class DummyObjectOutputStream
342 * Dummy object input class.
344 private class DummyObjectInputStream implements ObjectInput
347 * Non-private constructor to reduce bytecode emitted.
349 DummyObjectInputStream()
353 public boolean readBoolean() throws IOException
355 Object obj = vec.elementAt(ptr++);
356 return ((Boolean) obj).booleanValue();
359 public byte readByte() throws IOException
361 Object obj = vec.elementAt(ptr++);
362 return ((Byte) obj).byteValue();
365 public char readChar() throws IOException
367 Object obj = vec.elementAt(ptr++);
368 return ((Character) obj).charValue();
371 public double readDouble() throws IOException
373 Object obj = vec.elementAt(ptr++);
374 return ((Double) obj).doubleValue();
377 public float readFloat() throws IOException
379 Object obj = vec.elementAt(ptr++);
380 return ((Float) obj).floatValue();
383 public int readInt() throws IOException
385 Object obj = vec.elementAt(ptr++);
386 return ((Integer) obj).intValue();
389 public long readLong() throws IOException
391 Object obj = vec.elementAt(ptr++);
392 return ((Long) obj).longValue();
395 public short readShort() throws IOException
397 Object obj = vec.elementAt(ptr++);
398 return ((Short) obj).shortValue();
401 public Object readObject() throws IOException
403 return vec.elementAt(ptr++);
406 public int read(byte b[]) throws IOException
408 throw new IOException("not required");
411 public int read(byte b[], int off, int len) throws IOException
413 throw new IOException("not required");
416 public int read() throws IOException
418 throw new IOException("not required");
421 public long skip(long n) throws IOException
423 throw new IOException("not required");
426 public int available() throws IOException
428 throw new IOException("not required");
431 public void readFully(byte b[]) throws IOException
433 throw new IOException("not required");
436 public void readFully(byte b[], int off, int len) throws IOException
438 throw new IOException("not required");
441 public String readLine() throws IOException
443 throw new IOException("not required");
446 public String readUTF() throws IOException
448 throw new IOException("not required");
451 public int readUnsignedByte() throws IOException
453 throw new IOException("not required");
456 public int readUnsignedShort() throws IOException
458 throw new IOException("not required");
461 public int skipBytes(int n) throws IOException
463 throw new IOException("not required");
466 public void close() throws IOException
469 } // class DummyObjectInputStream