001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Mikhail A. Markov
021: * @version $Revision: 1.1.2.2 $
022: */package org.apache.harmony.rmi.server;
023:
024: import java.io.DataOutputStream;
025: import java.io.IOException;
026: import java.io.StreamCorruptedException;
027: import java.io.ObjectInput;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutput;
030: import java.rmi.server.RemoteCall;
031:
032: import org.apache.harmony.rmi.internal.nls.Messages;
033: import org.apache.harmony.rmi.transport.RMIObjectInputStream;
034: import org.apache.harmony.rmi.transport.RMIObjectOutputStream;
035: import org.apache.harmony.rmi.transport.RMIProtocolConstants;
036:
037: /**
038: * RemoteCall implementation used by UnicastServerRef on server's side.
039: *
040: * @author Mikhail A. Markov
041: * @version $Revision: 1.1.2.2 $
042: */
043: public class ServerRemoteCall implements RemoteCall,
044: RMIProtocolConstants {
045:
046: // Connection to remote server.
047: private ServerConnection conn;
048:
049: // InputStream for reading objects.
050: private ObjectInputStream oin = null;
051:
052: // OutputStream for sending objects.
053: private RMIObjectOutputStream oout = null;
054:
055: // True if getResultStream has been called.
056: private boolean hasResStream = false;
057:
058: /**
059: * Constructs ServerRemoteCall from existing connection.
060: *
061: * @param conn opened ServerConnection
062: */
063: public ServerRemoteCall(ServerConnection conn) {
064: this .conn = conn;
065: }
066:
067: /**
068: * Constructs ServerRemoteCall from opened connection and already created
069: * ObjectOutputStream.
070: *
071: * @param conn opened ServerConnection
072: * @param oin created ObjectOutputStream
073: */
074: public ServerRemoteCall(ServerConnection conn, ObjectInputStream oin) {
075: this .conn = conn;
076: this .oin = oin;
077: }
078:
079: /**
080: * Constructs ObjectInputStream (if it was not created yet) and returns
081: * this created stream.
082: *
083: * @return ObjectInputStream to read objects from
084: *
085: * @throws IOException if an I/O error occurred during stream construction
086: */
087: public ObjectInput getInputStream() throws IOException {
088: if (oin == null) {
089: oin = new RMIObjectInputStream(conn.getInputStream());
090: }
091: return oin;
092: }
093:
094: /**
095: * Constructs ObjectOutputStream (if it was not created yet) and returns
096: * this created stream.
097: *
098: * @return ObjectOutputStream to write objects to
099: *
100: * @throws IOException if an I/O error occurred during stream construction
101: */
102:
103: public ObjectOutput getOutputStream() throws IOException {
104: if (oout == null) {
105: oout = new RMIObjectOutputStream(conn.getOutputStream());
106: }
107: return oout;
108: }
109:
110: /**
111: * Writes byte meaning normal call return, writes byte identifying call
112: * result (normal return or exception) - depending on success parameter,
113: * writes UID of the object (for DGC) and flushes the output stream.
114: * This method could be called only once.
115: *
116: * @param success if true - means that method call was successful (i.e.
117: * with no exception) - return data description will be written to
118: * the output stream
119: *
120: * @throws IOException if an I/O error occurred while writing to the stream
121: * @throws StreamCorruptedException if this method has already been called
122: */
123: public ObjectOutput getResultStream(boolean success)
124: throws IOException, StreamCorruptedException {
125: if (hasResStream) {
126: // rmi.7A=getResultStream() method has already been called.
127: throw new StreamCorruptedException(Messages
128: .getString("rmi.7A")); //$NON-NLS-1$
129: }
130: (new DataOutputStream(conn.getOutputStream()))
131: .writeByte(CALL_OK);
132:
133: if (oout == null) {
134: oout = new RMIObjectOutputStream(conn.getOutputStream(),
135: true);
136: }
137: oout.writeByte(success ? RETURN_VAL : RETURN_EX);
138: oout.writeUID();
139: oout.flush();
140: hasResStream = true;
141: return oout;
142: }
143:
144: /**
145: * @see RemoteCall.releaseInputStream()
146: */
147: public void releaseInputStream() throws IOException {
148: conn.releaseInputStream();
149: }
150:
151: /**
152: * @see RemoteCall.releaseOutputStream()
153: */
154: public void releaseOutputStream() throws IOException {
155: }
156:
157: /**
158: * @see RemoteCall.done()
159: */
160: public void done() throws IOException {
161: conn.close();
162: }
163:
164: /**
165: * Not used on server side.
166: */
167: public void executeCall() throws Exception {
168: }
169:
170: /**
171: * Returns string representation of this RemoteCall.
172: *
173: * @return string representation of this RemoteCall
174: */
175: public String toString() {
176: return "ServerRemoteCall: connection: " + conn; //$NON-NLS-1$
177: }
178:
179: /**
180: * Returns true if getResultStream was already called before and
181: * false otherwise.
182: *
183: * @return true if getResultStream was already called before and
184: * false otherwise
185: */
186: public boolean hasResultStream() {
187: return hasResStream;
188: }
189: }
|