OSDN Git Service

837e6e4e975b3a4e711f7aa5773fb17767efe83a
[pf3gnuchains/gcc-fork.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 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License.
26  */
27
28 package gnu.java.rmi.server;
29
30 import java.rmi.Remote;
31 import java.rmi.RemoteException;
32 import java.rmi.server.RemoteRef;
33 import java.rmi.server.RMISocketFactory;
34 import java.rmi.server.RMIClientSocketFactory;
35 import java.rmi.server.RMIServerSocketFactory;
36 import java.rmi.server.RemoteObject;
37 import java.rmi.server.RemoteCall;
38 import java.rmi.server.UnicastRemoteObject;
39 import java.rmi.server.Operation;
40 import java.rmi.server.ObjID;
41 import java.rmi.server.UID;
42 import java.lang.reflect.Method;
43 import java.io.ObjectOutput;
44 import java.io.ObjectInput;
45 import java.io.IOException;
46 import java.net.Socket;
47 import java.net.InetAddress;
48 import java.io.BufferedInputStream;
49 import java.io.BufferedOutputStream;
50 import java.io.ObjectInputStream;
51 import java.io.ObjectOutputStream;
52 import java.io.DataInputStream;
53 import java.io.DataOutputStream;
54
55 public class UnicastRef
56         implements RemoteRef, ProtocolConstants {
57
58 public ObjID objid;
59 UnicastConnectionManager manager;
60
61 /**
62  * Used by serialization.
63  */
64 private UnicastRef() {
65 }
66
67 public UnicastRef(ObjID objid, String host, int port, RMIClientSocketFactory csf) {
68         this(objid);
69         manager = UnicastConnectionManager.getInstance(host, port, csf);
70 }
71
72 public UnicastRef(ObjID objid) {
73         this.objid = objid;
74 }
75
76 public Object invoke(Remote obj, Method method, Object[] params, long opnum) throws Exception {
77         return (invokeCommon(obj, method, params, -1, opnum));
78 }
79
80 private Object invokeCommon(Remote obj, Method method, Object[] params, int opnum, long hash) throws Exception {
81         UnicastConnection conn;
82         try {
83                 conn = manager.getConnection();
84         }
85         catch (IOException e1) {
86                 throw new RemoteException("connection failed to host: " + manager.serverName, e1);
87         }
88
89         ObjectOutputStream out;
90         DataOutputStream dout;
91         try {
92                 dout = conn.getDataOutputStream();
93                 dout.writeByte(MESSAGE_CALL);
94
95                 out = conn.getObjectOutputStream();
96                 
97                 objid.write(out);
98                 out.writeInt(opnum);
99                 out.writeLong(hash);
100                 if (params != null) {
101                         for (int i = 0; i < params.length; i++) {
102                                 if (params[i] instanceof UnicastRemoteObject) {
103                                         out.writeObject(UnicastRemoteObject.exportObject((UnicastRemoteObject)params[i]));
104                                 }
105                                 else {
106                                         out.writeObject(params[i]);
107                                 }
108                         }
109                 }
110
111                 out.flush();
112         }
113         catch (IOException e2) {
114                 throw new RemoteException("call failed: ", e2);
115         }
116
117         int returncode;
118         Object returnval;
119         DataInputStream din;
120         ObjectInputStream in;
121         UID ack;
122         try {
123                 din = conn.getDataInputStream();
124                 if (din.readUnsignedByte() != MESSAGE_CALL_ACK) {
125                         throw new RemoteException("Call not acked");
126                 }
127
128                 in = conn.getObjectInputStream();
129
130                 returncode = in.readUnsignedByte();
131                 ack = UID.read(in);
132                 returnval = in.readObject();
133         }
134         catch (IOException e3) {
135                 throw new RemoteException("call return failed: ", e3);
136         }
137
138         manager.discardConnection(conn);
139
140         if (returncode != RETURN_ACK) {
141                 throw (Exception)returnval;
142         }
143
144         return (returnval);
145 }
146
147 /**
148  * @deprecated
149  */
150 public RemoteCall newCall(RemoteObject obj, Operation[] op, int opnum, long hash) throws RemoteException {
151         return (new UnicastRemoteCall(obj, opnum, hash));
152 }
153
154 /**
155  * @deprecated
156  */
157 public void invoke(RemoteCall call) throws Exception {
158         UnicastRemoteCall c = (UnicastRemoteCall)call;
159         Object ret = invokeCommon((Remote)c.getObject(), (Method)null, c.getArguments(), c.getOpnum(), c.getHash());
160         c.setReturnValue(ret);
161 }
162
163 /**
164  * @deprecated
165  */
166 public void done(RemoteCall call) throws RemoteException {
167         /* Does nothing */
168 }
169
170 public void writeExternal(ObjectOutput out) throws IOException {
171         if (manager == null) {
172                 throw new IOException("no connection");
173         }
174         manager.write(out);
175         objid.write(out);
176         out.writeByte(RETURN_ACK);
177 }
178
179 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
180         manager = UnicastConnectionManager.read(in);
181         objid = ObjID.read(in);
182         if (in.readByte() != RETURN_ACK) {
183                 throw new IOException("no ack found");
184         }
185 }
186
187 public boolean remoteEquals(RemoteRef ref) {
188         throw new Error("Not implemented");
189 }
190
191 public int remoteHashCode() {
192         throw new Error("Not implemented");
193 }
194
195 public String getRefClass(ObjectOutput out) {
196         return ("UnicastRef");
197 }
198
199 public String remoteToString() {
200         throw new Error("Not implemented");
201 }
202
203 public void dump(UnicastConnection conn) {
204         try {
205                 DataInputStream din = conn.getDataInputStream();
206                 for (;;) {
207                         int b = din.readUnsignedByte();
208                         System.out.print(Integer.toHexString(b));
209                         if (b >= 32 && b < 128) {
210                                 System.out.print(": " + (char)b);
211                         }
212                         System.out.println();
213                 }
214         }
215         catch (IOException _) {
216         }
217 }
218
219 }