Merge from mainline.
[official-gcc.git] / libjava / classpath / gnu / java / rmi / server / UnicastRemoteCall.java
blobc5206e76df22ad7290b33118f35b27dc124558ea
1 /* UnicastRemoteCall.java
2 Copyright (c) 1996, 1997, 1998, 1999, 2002, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package gnu.java.rmi.server;
42 import java.io.DataInputStream;
43 import java.io.DataOutputStream;
44 import java.io.IOException;
45 import java.io.ObjectInput;
46 import java.io.ObjectOutput;
47 import java.io.StreamCorruptedException;
48 import java.rmi.MarshalException;
49 import java.rmi.RemoteException;
50 import java.rmi.UnmarshalException;
51 import java.rmi.server.ObjID;
52 import java.rmi.server.RemoteCall;
53 import java.rmi.server.UID;
54 import java.util.Vector;
56 public class UnicastRemoteCall
57 implements RemoteCall, ProtocolConstants
60 private UnicastConnection conn;
61 private Object result;
62 private Object object;
63 private int opnum;
64 private long hash;
65 // These are package-private due to inner class access.
66 Vector vec;
67 int ptr;
68 private ObjID objid;
70 private ObjectOutput oout;
71 private ObjectInput oin;
73 /**
74 * Incoming call.
76 UnicastRemoteCall(UnicastConnection conn)
78 this.conn = conn;
81 /**
82 * Outgoing call.
84 UnicastRemoteCall(UnicastConnection conn, ObjID objid, int opnum, long hash)
85 throws RemoteException
87 this.conn = conn;
88 this.opnum = opnum;
89 this.hash = hash;
90 this.objid = objid;
93 UnicastConnection getConnection()
95 return conn;
98 public ObjectOutput getOutputStream() throws IOException
100 if (vec == null)
101 vec = new Vector();
102 return (new DummyObjectOutputStream());
105 public void releaseOutputStream() throws IOException
107 if (vec != null)
109 oout = conn.getObjectOutputStream();
111 for (int i = 0; i < vec.size(); i += 2)
113 boolean primitive = ((Boolean)vec.elementAt(i)).booleanValue();
114 Object data = vec.elementAt(i+1);
116 // No type, this is
117 if (!primitive)
118 oout.writeObject(data);
119 else
121 if (data instanceof Boolean)
122 oout.writeBoolean(((Boolean)data).booleanValue());
123 else if (data instanceof Character)
124 oout.writeChar(((Character)data).charValue());
125 else if (data instanceof Byte)
126 oout.writeByte(((Byte)data).byteValue());
127 else if (data instanceof Short)
128 oout.writeShort(((Short)data).shortValue());
129 else if (data instanceof Integer)
130 oout.writeInt(((Integer)data).intValue());
131 else if (data instanceof Long)
132 oout.writeLong(((Long)data).longValue());
135 vec = null;
137 if(oout != null)
138 oout.flush();
143 * (re)starts ObjectInputStream
146 public ObjectInput startInputStream() throws IOException
148 if (conn != null) {
149 return (oin = conn.startObjectInputStream());
150 } else {
151 return getInputStream(); // dummy Input Stream
156 public ObjectInput getInputStream() throws IOException
158 if (conn != null)
160 if(oin == null)
161 return (oin = conn.getObjectInputStream());
162 else
163 return oin;
165 else
167 ptr = 0;
168 return (new DummyObjectInputStream());
172 public void releaseInputStream() throws IOException
174 // Does nothing.
177 public ObjectOutput getResultStream(boolean success)
178 throws IOException, StreamCorruptedException
180 vec = new Vector();
181 return new DummyObjectOutputStream();
184 public void executeCall() throws Exception
186 byte returncode;
187 ObjectInput oin;
189 // signal the call when constructing
192 DataOutputStream dout = conn.getDataOutputStream();
193 dout.write(MESSAGE_CALL);
195 oout = conn.startObjectOutputStream(); // (re)start ObjectOutputStream
196 objid.write(oout);
197 oout.writeInt(opnum);
198 oout.writeLong(hash);
200 catch(IOException ex)
202 throw new MarshalException("Try to write header but failed.", ex);
207 releaseOutputStream();
208 DataInputStream din = conn.getDataInputStream();
209 if (din.readByte() != MESSAGE_CALL_ACK)
210 throw new RemoteException("Call not acked");
212 oin = startInputStream();
213 returncode = oin.readByte();
214 UID.read(oin);
216 catch(IOException ex)
218 throw new UnmarshalException("Try to read header but failed:", ex);
221 //check return code
222 switch(returncode)
224 case RETURN_ACK: //it's ok
225 return;
226 case RETURN_NACK:
227 Object returnobj;
230 returnobj = oin.readObject();
232 catch(Exception ex2)
234 throw new UnmarshalException
235 ("Try to read exception object but failed", ex2);
238 if(!(returnobj instanceof Exception))
239 throw new UnmarshalException("Should be Exception type here: "
240 + returnobj);
241 throw (Exception)returnobj;
243 default:
244 throw new UnmarshalException("Invalid return code");
248 public void done() throws IOException
250 // conn.disconnect();
253 boolean isReturnValue()
255 return vec.size() > 0;
258 Object returnValue()
260 // This is not the first one (Boolean) but the second.
261 return vec.elementAt(1);
264 Object[] getArguments()
266 return vec.toArray();
269 Object getObject()
271 return object;
274 int getOpnum()
276 return opnum;
279 long getHash()
281 return hash;
284 void setReturnValue(Object obj)
286 vec.removeAllElements();
287 vec.addElement(obj);
291 * Dummy object output class.
293 private class DummyObjectOutputStream implements ObjectOutput
296 * Non-private constructor to reduce bytecode emitted.
298 DummyObjectOutputStream()
302 public void writeBoolean(boolean v) throws IOException
304 vec.addElement(Boolean.TRUE);
305 vec.addElement(Boolean.valueOf(v));
308 public void writeByte(int v) throws IOException
310 vec.addElement(Boolean.TRUE);
311 vec.addElement(new Byte((byte) v));
314 public void writeChar(int v) throws IOException
316 vec.addElement(Boolean.TRUE);
317 vec.addElement(new Character((char) v));
320 public void writeDouble(double v) throws IOException
322 vec.addElement(Boolean.TRUE);
323 vec.addElement(new Double(v));
326 public void writeFloat(float v) throws IOException
328 vec.addElement(Boolean.TRUE);
329 vec.addElement(new Float(v));
332 public void writeInt(int v) throws IOException
334 vec.addElement(Boolean.TRUE);
335 vec.addElement(new Integer(v));
338 public void writeLong(long v) throws IOException
340 vec.addElement(Boolean.TRUE);
341 vec.addElement(new Long(v));
344 public void writeShort(int v) throws IOException
346 vec.addElement(Boolean.TRUE);
347 vec.addElement(new Short((short) v));
350 public void writeObject(Object obj) throws IOException
352 vec.addElement(Boolean.FALSE);
353 vec.addElement(obj);
356 public void write(byte b[]) throws IOException
358 throw new IOException("not required");
361 public void write(byte b[], int off, int len) throws IOException
363 throw new IOException("not required");
366 public void write(int b) throws IOException
368 throw new IOException("not required");
371 public void writeBytes(String s) throws IOException
373 throw new IOException("not required");
376 public void writeChars(String s) throws IOException
378 throw new IOException("not required");
381 public void writeUTF(String str) throws IOException
383 throw new IOException("not required");
386 public void flush() throws IOException
390 public void close() throws IOException
393 } // class DummyObjectOutputStream
396 * Dummy object input class.
398 private class DummyObjectInputStream implements ObjectInput
401 * Non-private constructor to reduce bytecode emitted.
403 DummyObjectInputStream()
407 public boolean readBoolean() throws IOException
409 Object obj = vec.elementAt(ptr++);
410 return ((Boolean) obj).booleanValue();
413 public byte readByte() throws IOException
415 Object obj = vec.elementAt(ptr++);
416 return ((Byte) obj).byteValue();
419 public char readChar() throws IOException
421 Object obj = vec.elementAt(ptr++);
422 return ((Character) obj).charValue();
425 public double readDouble() throws IOException
427 Object obj = vec.elementAt(ptr++);
428 return ((Double) obj).doubleValue();
431 public float readFloat() throws IOException
433 Object obj = vec.elementAt(ptr++);
434 return ((Float) obj).floatValue();
437 public int readInt() throws IOException
439 Object obj = vec.elementAt(ptr++);
440 return ((Integer) obj).intValue();
443 public long readLong() throws IOException
445 Object obj = vec.elementAt(ptr++);
446 return ((Long) obj).longValue();
449 public short readShort() throws IOException
451 Object obj = vec.elementAt(ptr++);
452 return ((Short) obj).shortValue();
455 public Object readObject() throws IOException
457 return vec.elementAt(ptr++);
460 public int read(byte b[]) throws IOException
462 throw new IOException("not required");
465 public int read(byte b[], int off, int len) throws IOException
467 throw new IOException("not required");
470 public int read() throws IOException
472 throw new IOException("not required");
475 public long skip(long n) throws IOException
477 throw new IOException("not required");
480 public int available() throws IOException
482 throw new IOException("not required");
485 public void readFully(byte b[]) throws IOException
487 throw new IOException("not required");
490 public void readFully(byte b[], int off, int len) throws IOException
492 throw new IOException("not required");
495 public String readLine() throws IOException
497 throw new IOException("not required");
500 public String readUTF() throws IOException
502 throw new IOException("not required");
505 public int readUnsignedByte() throws IOException
507 throw new IOException("not required");
510 public int readUnsignedShort() throws IOException
512 throw new IOException("not required");
515 public int skipBytes(int n) throws IOException
517 throw new IOException("not required");
520 public void close() throws IOException
523 } // class DummyObjectInputStream