| 1 | /*
|
|---|
| 2 | Copyright (c) 1996, 1997, 1998, 1999, 2002 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 | import java.lang.reflect.InvocationTargetException;
|
|---|
| 66 |
|
|---|
| 67 | public class UnicastRef
|
|---|
| 68 | implements RemoteRef, ProtocolConstants {
|
|---|
| 69 |
|
|---|
| 70 | public ObjID objid;
|
|---|
| 71 | UnicastConnectionManager manager;
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Used by serialization, and let subclass capable of having default constructor
|
|---|
| 75 | */
|
|---|
| 76 | //private
|
|---|
| 77 | UnicastRef() {
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | public UnicastRef(ObjID objid, String host, int port, RMIClientSocketFactory csf) {
|
|---|
| 81 | this(objid);
|
|---|
| 82 | manager = UnicastConnectionManager.getInstance(host, port, csf);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | public UnicastRef(ObjID objid) {
|
|---|
| 86 | this.objid = objid;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public Object invoke(Remote obj, Method method, Object[] params, long opnum) throws Exception {
|
|---|
| 90 | // Check if client and server are in the same VM, then local call can be used to
|
|---|
| 91 | // replace remote call, but it's somewhat violating remote semantic.
|
|---|
| 92 | Object svrobj = manager.serverobj;
|
|---|
| 93 | if(svrobj != null){
|
|---|
| 94 | //local call
|
|---|
| 95 | Object ret = null;
|
|---|
| 96 | try{
|
|---|
| 97 | ret = method.invoke(svrobj, params);
|
|---|
| 98 | }catch(InvocationTargetException e){
|
|---|
| 99 | throw (Exception)e.getTargetException();
|
|---|
| 100 | }
|
|---|
| 101 | //System.out.println("\n\n ***** local call: " + method + "\nreturn: " + ret + "\n\n");
|
|---|
| 102 | return ret;
|
|---|
| 103 | }
|
|---|
| 104 | //System.out.println("***************** remote call:" + manager.serverPort);
|
|---|
| 105 | return (invokeCommon(obj, method, params, -1, opnum));
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | private Object invokeCommon(Remote obj, Method method, Object[] params, int opnum, long hash) throws Exception {
|
|---|
| 109 | UnicastConnection conn;
|
|---|
| 110 | try {
|
|---|
| 111 | conn = manager.getConnection();
|
|---|
| 112 | }
|
|---|
| 113 | catch (IOException e1) {
|
|---|
| 114 | throw new RemoteException("connection failed to host: " + manager.serverName, e1);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | ObjectOutputStream out;
|
|---|
| 118 | DataOutputStream dout;
|
|---|
| 119 | try {
|
|---|
| 120 | dout = conn.getDataOutputStream();
|
|---|
| 121 | dout.writeByte(MESSAGE_CALL);
|
|---|
| 122 |
|
|---|
| 123 | out = conn.getObjectOutputStream();
|
|---|
| 124 |
|
|---|
| 125 | objid.write(out);
|
|---|
| 126 | out.writeInt(opnum);
|
|---|
| 127 | out.writeLong(hash);
|
|---|
| 128 |
|
|---|
| 129 | // must handle primitive class and their wrapper classes
|
|---|
| 130 | Class clss[] = method.getParameterTypes();
|
|---|
| 131 | for(int i = 0; i < clss.length; i++)
|
|---|
| 132 | ((RMIObjectOutputStream)out).writeValue(params[i], clss[i]);
|
|---|
| 133 |
|
|---|
| 134 | out.flush();
|
|---|
| 135 | }
|
|---|
| 136 | catch (IOException e2) {
|
|---|
| 137 | throw new RemoteException("call failed: ", e2);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | int returncode;
|
|---|
| 141 | Object returnval;
|
|---|
| 142 | DataInputStream din;
|
|---|
| 143 | ObjectInputStream in;
|
|---|
| 144 | UID ack;
|
|---|
| 145 | try {
|
|---|
| 146 | din = conn.getDataInputStream();
|
|---|
| 147 |
|
|---|
| 148 | if ((returncode = din.readUnsignedByte()) != MESSAGE_CALL_ACK) {
|
|---|
| 149 | conn.disconnect();
|
|---|
| 150 | throw new RemoteException("Call not acked:" + returncode);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | in = conn.getObjectInputStream();
|
|---|
| 154 | returncode = in.readUnsignedByte();
|
|---|
| 155 | ack = UID.read(in);
|
|---|
| 156 |
|
|---|
| 157 | Class cls = method.getReturnType();
|
|---|
| 158 | if(cls == Void.TYPE){
|
|---|
| 159 | returnval = null;
|
|---|
| 160 | in.readObject();
|
|---|
| 161 | }else
|
|---|
| 162 | returnval = ((RMIObjectInputStream)in).readValue(cls);
|
|---|
| 163 |
|
|---|
| 164 | }
|
|---|
| 165 | catch (IOException e3) {
|
|---|
| 166 | //for debug: e3.printStackTrace();
|
|---|
| 167 | throw new RemoteException("call return failed: ", e3);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /* if DGCAck is necessary??
|
|---|
| 171 | //According to RMI wire protocol, send a DGCAck
|
|---|
| 172 | // to indicate receiving return value
|
|---|
| 173 | dout.writeByte(MESSAGE_DGCACK);
|
|---|
| 174 | ack.write(dout);
|
|---|
| 175 | out.flush();
|
|---|
| 176 | */
|
|---|
| 177 |
|
|---|
| 178 | manager.discardConnection(conn);
|
|---|
| 179 |
|
|---|
| 180 | if (returncode != RETURN_ACK && returnval != null) {
|
|---|
| 181 | throw (Exception)returnval;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | return (returnval);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /**
|
|---|
| 188 | * @deprecated
|
|---|
| 189 | */
|
|---|
| 190 | public RemoteCall newCall(RemoteObject obj, Operation[] op, int opnum, long hash) throws RemoteException {
|
|---|
| 191 | UnicastConnection conn;
|
|---|
| 192 |
|
|---|
| 193 | try {
|
|---|
| 194 | conn = manager.getConnection();
|
|---|
| 195 | }
|
|---|
| 196 | catch (IOException e1) {
|
|---|
| 197 | throw new RemoteException("connection failed to host: " + manager.serverName, e1);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | //obj: useless?
|
|---|
| 201 |
|
|---|
| 202 | return (new UnicastRemoteCall(conn, objid, opnum, hash));
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /**
|
|---|
| 206 | * @deprecated
|
|---|
| 207 | */
|
|---|
| 208 | public void invoke(RemoteCall call) throws Exception {
|
|---|
| 209 | UnicastRemoteCall c = (UnicastRemoteCall)call;
|
|---|
| 210 | call.executeCall();
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /**
|
|---|
| 214 | * @deprecated
|
|---|
| 215 | */
|
|---|
| 216 | public void done(RemoteCall call) throws RemoteException {
|
|---|
| 217 | UnicastRemoteCall c = (UnicastRemoteCall)call;
|
|---|
| 218 | try{
|
|---|
| 219 | c.done();
|
|---|
| 220 | } catch(IOException e){}
|
|---|
| 221 | UnicastConnection conn = c.getConnection();
|
|---|
| 222 | manager.discardConnection(conn);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | public void writeExternal(ObjectOutput out) throws IOException {
|
|---|
| 226 | if (manager == null) {
|
|---|
| 227 | throw new IOException("no connection");
|
|---|
| 228 | }
|
|---|
| 229 | manager.write(out);
|
|---|
| 230 | objid.write(out);
|
|---|
| 231 | // This byte is somewhat confusing when interoperating with JDK
|
|---|
| 232 | out.writeByte(0); //RETURN_ACK);
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
|---|
| 236 | manager = UnicastConnectionManager.read(in);
|
|---|
| 237 | objid = ObjID.read(in);
|
|---|
| 238 | byte ack = in.readByte();
|
|---|
| 239 | // This byte is somewhat confusing when interoperating with JDK
|
|---|
| 240 | if (ack != RETURN_ACK && ack != 0/*jdk ack value*/) {
|
|---|
| 241 | throw new IOException("no ack found");
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | public boolean remoteEquals(RemoteRef ref) {
|
|---|
| 246 | throw new Error("Not implemented");
|
|---|
|
|---|