2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / rmi / server / UnicastRemoteCall.java
blob2d7d6d4a9ff1287776940b59b49b1157293737e9
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;
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();
141 public ObjectInput getInputStream() throws IOException
143 if (conn != null)
145 if(oin == null)
146 return (oin = conn.getObjectInputStream());
147 else
148 return oin;
150 else
152 ptr = 0;
153 return (new DummyObjectInputStream());
157 public void releaseInputStream() throws IOException
159 // Does nothing.
162 public ObjectOutput getResultStream(boolean success)
163 throws IOException, StreamCorruptedException
165 vec = new Vector();
166 return new DummyObjectOutputStream();
169 public void executeCall() throws Exception
171 byte returncode;
172 ObjectInput oin;
174 // signal the call when constructing
177 DataOutputStream dout = conn.getDataOutputStream();
178 dout.write(MESSAGE_CALL);
180 oout = conn.getObjectOutputStream();
181 objid.write(oout);
182 oout.writeInt(opnum);
183 oout.writeLong(hash);
185 catch(IOException ex)
187 throw new MarshalException("Try to write header but failed.", ex);
192 releaseOutputStream();
193 DataInputStream din = conn.getDataInputStream();
194 if (din.readByte() != MESSAGE_CALL_ACK)
195 throw new RemoteException("Call not acked");
197 oin = getInputStream();
198 returncode = oin.readByte();
199 UID.read(oin);
201 catch(IOException ex)
203 throw new UnmarshalException("Try to read header but failed:", ex);
206 //check return code
207 switch(returncode)
209 case RETURN_ACK: //it's ok
210 return;
211 case RETURN_NACK:
212 Object returnobj;
215 returnobj = oin.readObject();
217 catch(Exception ex2)
219 throw new UnmarshalException
220 ("Try to read exception object but failed", ex2);
223 if(!(returnobj instanceof Exception))
224 throw new UnmarshalException("Should be Exception type here: "
225 + returnobj);
226 throw (Exception)returnobj;
228 default:
229 throw new UnmarshalException("Invalid return code");
233 public void done() throws IOException
235 // conn.disconnect();
238 boolean isReturnValue()
240 return vec.size() > 0;
243 Object returnValue()
245 // This is not the first one (Boolean) but the second.
246 return vec.elementAt(1);
249 Object[] getArguments()
251 return vec.toArray();
254 Object getObject()
256 return object;
259 int getOpnum()
261 return opnum;
264 long getHash()
266 return hash;
269 void setReturnValue(Object obj)
271 vec.removeAllElements();
272 vec.addElement(obj);
276 * Dummy object output class.
278 private class DummyObjectOutputStream implements ObjectOutput
281 * Non-private constructor to reduce bytecode emitted.
283 DummyObjectOutputStream()
287 public void writeBoolean(boolean v) throws IOException
289 vec.addElement(Boolean.TRUE);
290 vec.addElement(Boolean.valueOf(v));
293 public void writeByte(int v) throws IOException
295 vec.addElement(Boolean.TRUE);
296 vec.addElement(new Byte((byte) v));
299 public void writeChar(int v) throws IOException
301 vec.addElement(Boolean.TRUE);
302 vec.addElement(new Character((char) v));
305 public void writeDouble(double v) throws IOException
307 vec.addElement(Boolean.TRUE);
308 vec.addElement(new Double(v));
311 public void writeFloat(float v) throws IOException
313 vec.addElement(Boolean.TRUE);
314 vec.addElement(new Float(v));
317 public void writeInt(int v) throws IOException
319 vec.addElement(Boolean.TRUE);
320 vec.addElement(new Integer(v));
323 public void writeLong(long v) throws IOException
325 vec.addElement(Boolean.TRUE);
326 vec.addElement(new Long(v));
329 public void writeShort(int v) throws IOException
331 vec.addElement(Boolean.TRUE);
332 vec.addElement(new Short((short) v));
335 public void writeObject(Object obj) throws IOException
337 vec.addElement(Boolean.FALSE);
338 vec.addElement(obj);
341 public void write(byte b[]) throws IOException
343 throw new IOException("not required");
346 public void write(byte b[], int off, int len) throws IOException
348 throw new IOException("not required");
351 public void write(int b) throws IOException
353 throw new IOException("not required");
356 public void writeBytes(String s) throws IOException
358 throw new IOException("not required");
361 public void writeChars(String s) throws IOException
363 throw new IOException("not required");
366 public void writeUTF(String str) throws IOException
368 throw new IOException("not required");
371 public void flush() throws IOException
375 public void close() throws IOException
378 } // class DummyObjectOutputStream
381 * Dummy object input class.
383 private class DummyObjectInputStream implements ObjectInput
386 * Non-private constructor to reduce bytecode emitted.
388 DummyObjectInputStream()
392 public boolean readBoolean() throws IOException
394 Object obj = vec.elementAt(ptr++);
395 return ((Boolean) obj).booleanValue();
398 public byte readByte() throws IOException
400 Object obj = vec.elementAt(ptr++);
401 return ((Byte) obj).byteValue();
404 public char readChar() throws IOException
406 Object obj = vec.elementAt(ptr++);
407 return ((Character) obj).charValue();
410 public double readDouble() throws IOException
412 Object obj = vec.elementAt(ptr++);
413 return ((Double) obj).doubleValue();
416 public float readFloat() throws IOException
418 Object obj = vec.elementAt(ptr++);
419 return ((Float) obj).floatValue();
422 public int readInt() throws IOException
424 Object obj = vec.elementAt(ptr++);
425 return ((Integer) obj).intValue();
428 public long readLong() throws IOException
430 Object obj = vec.elementAt(ptr++);
431 return ((Long) obj).longValue();
434 public short readShort() throws IOException
436 Object obj = vec.elementAt(ptr++);
437 return ((Short) obj).shortValue();
440 public Object readObject() throws IOException
442 return vec.elementAt(ptr++);
445 public int read(byte b[]) throws IOException
447 throw new IOException("not required");
450 public int read(byte b[], int off, int len) throws IOException
452 throw new IOException("not required");
455 public int read() throws IOException
457 throw new IOException("not required");
460 public long skip(long n) throws IOException
462 throw new IOException("not required");
465 public int available() throws IOException
467 throw new IOException("not required");
470 public void readFully(byte b[]) throws IOException
472 throw new IOException("not required");
475 public void readFully(byte b[], int off, int len) throws IOException
477 throw new IOException("not required");
480 public String readLine() throws IOException
482 throw new IOException("not required");
485 public String readUTF() throws IOException
487 throw new IOException("not required");
490 public int readUnsignedByte() throws IOException
492 throw new IOException("not required");
495 public int readUnsignedShort() throws IOException
497 throw new IOException("not required");
500 public int skipBytes(int n) throws IOException
502 throw new IOException("not required");
505 public void close() throws IOException
508 } // class DummyObjectInputStream