jdk/src/share/classes/sun/rmi/transport/StreamRemoteCall.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 12040 558b0e0d5910
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.rmi.transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.StreamCorruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.rmi.RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.rmi.MarshalException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.rmi.UnmarshalException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.rmi.server.ObjID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.rmi.server.RemoteCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.rmi.server.UnicastRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.rmi.transport.tcp.TCPEndpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Stream-based implementation of the RemoteCall interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class StreamRemoteCall implements RemoteCall {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private ConnectionInputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private ConnectionOutputStream out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private Connection conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean resultStarted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public StreamRemoteCall(Connection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        conn = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public StreamRemoteCall(Connection c, ObjID id, int op, long hash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        throws RemoteException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            conn = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            Transport.transportLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                "write remote call header...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            // write out remote call header info...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // call header, part 1 (read by Transport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            conn.getOutputStream().write(TransportConstants.Call);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            getOutputStream();           // creates a MarshalOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            id.write(out);               // object id (target of call)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // call header, part 2 (read by Dispatcher)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            out.writeInt(op);            // method number (operation index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            out.writeLong(hash);         // stub/skeleton hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new MarshalException("Error marshaling call header", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Return the connection associated with this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public Connection getConnection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Return the output stream the stub/skeleton should put arguments/results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public ObjectOutput getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return getOutputStream(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private ObjectOutput getOutputStream(boolean resultStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            Transport.transportLog.log(Log.VERBOSE, "getting output stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            out = new ConnectionOutputStream(conn, resultStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Release the outputStream  Currently, will not complain if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * output stream is released more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void releaseOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (out != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    out.done();         // always start DGC ack timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            conn.releaseOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Get the InputStream the stub/skeleton should get results/arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public ObjectInput getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Transport.transportLog.log(Log.VERBOSE, "getting input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            in = new ConnectionInputStream(conn.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Release the input stream, this would allow some transports to release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * the channel early.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void releaseInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /* WARNING: Currently, the UnicastRef.java invoke methods rely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * upon this method not throwing an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // execute MarshalInputStream "done" callbacks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    in.done();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                // add saved references to DGC table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                in.registerRefs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                /* WARNING: The connection being passed to done may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                 * already been freed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                in.done(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            conn.releaseInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Returns an output stream (may put out header information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * relating to the success of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param success If true, indicates normal return, else indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * exceptional return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception StreamCorruptedException If result stream previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @exception IOException For any other problem with I/O.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public ObjectOutput getResultStream(boolean success) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        /* make sure result code only marshaled once. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (resultStarted)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new StreamCorruptedException("result already in progress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            resultStarted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // write out return header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // return header, part 1 (read by Transport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        wr.writeByte(TransportConstants.Return);// transport op
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        getOutputStream(true);  // creates a MarshalOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // return header, part 2 (read by client-side RemoteCall)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (success)            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            out.writeByte(TransportConstants.NormalReturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            out.writeByte(TransportConstants.ExceptionalReturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        out.writeID();          // write id for gcAck
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Do whatever it takes to execute the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public void executeCall() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        byte returnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // read result header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        DGCAckHandler ackHandler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (out != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                ackHandler = out.getDGCAckHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            releaseOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            DataInputStream rd = new DataInputStream(conn.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            byte op = rd.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (op != TransportConstants.Return) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                if (Transport.transportLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    Transport.transportLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        "transport return code invalid: " + op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                throw new UnmarshalException("Transport return code invalid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            returnType = in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            in.readID();        // id for DGC acknowledgement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } catch (UnmarshalException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new UnmarshalException("Error unmarshaling return header",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                         e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (ackHandler != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                ackHandler.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        // read return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        switch (returnType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        case TransportConstants.NormalReturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        case TransportConstants.ExceptionalReturn:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            Object ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                ex = in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                throw new UnmarshalException("Error unmarshaling return", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // An exception should have been received,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // if so throw it, else flag error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (ex instanceof Exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                exceptionReceivedFromServer((Exception) ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                throw new UnmarshalException("Return type not Exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (Transport.transportLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                Transport.transportLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    "return code invalid: " + returnType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new UnmarshalException("Return code invalid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Routine that causes the stack traces of remote exceptions to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * filled in with the current stack trace on the client.  Detail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * exceptions are filled in iteratively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    protected void exceptionReceivedFromServer(Exception ex) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        serverException = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        StackTraceElement[] serverTrace = ex.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        StackTraceElement[] clientTrace = (new Throwable()).getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        StackTraceElement[] combinedTrace =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            new StackTraceElement[serverTrace.length + clientTrace.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        System.arraycopy(serverTrace, 0, combinedTrace, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                         serverTrace.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        System.arraycopy(clientTrace, 0, combinedTrace, serverTrace.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                         clientTrace.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        ex.setStackTrace(combinedTrace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * Log the details of a server exception thrown as a result of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         * remote method invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (UnicastRef.clientCallLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            /* log call exception returned from server before it is rethrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            TCPEndpoint ep = (TCPEndpoint) conn.getChannel().getEndpoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            UnicastRef.clientCallLog.log(Log.BRIEF, "outbound call " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                "received exception: [" + ep.getHost() + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                ep.getPort() + "] exception: ", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * method to retrieve possible server side exceptions (which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * be throw from exceptionReceivedFromServer(...) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public Exception getServerException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public void done() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        /* WARNING: Currently, the UnicastRef.java invoke methods rely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * upon this method not throwing an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        releaseInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}