1 /*
2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.corba.se.impl.protocol.giopmsgheaders;
27 
28 import org.omg.CORBA.INTERNAL;
29 import org.omg.CORBA.CompletionStatus;
30 import org.omg.CORBA.SystemException;
31 
32 import com.sun.corba.se.spi.ior.IOR;
33 import com.sun.corba.se.spi.ior.IORFactories;
34 
35 import com.sun.corba.se.spi.orb.ORB;
36 
37 import com.sun.corba.se.spi.servicecontext.ServiceContexts;
38 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
39 import com.sun.corba.se.impl.orbutil.ORBUtility;
40 import com.sun.corba.se.spi.ior.IOR;
41 import com.sun.corba.se.impl.encoding.CDRInputStream;
42 
43 import com.sun.corba.se.spi.logging.CORBALogDomains ;
44 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
45 
46 /**
47  * This implements the GIOP 1.0 Reply header.
48  *
49  * @author Ram Jeyaraman 05/14/2000
50  */
51 
52 public final class ReplyMessage_1_0 extends Message_1_0
53         implements ReplyMessage {
54 
55     // Instance variables
56 
57     private ORB orb = null;
58     private ORBUtilSystemException wrapper = null ;
59     private ServiceContexts service_contexts = null;
60     private int request_id = (int) 0;
61     private int reply_status = (int) 0;
62     private IOR ior = null;
63     private String exClassName = null;
64     private int minorCode = (int) 0;
65     private CompletionStatus completionStatus = null;
66 
67     // Constructors
68 
ReplyMessage_1_0(ORB orb)69     ReplyMessage_1_0(ORB orb) {
70         this.orb = orb;
71         this.wrapper = ORBUtilSystemException.get( orb,
72             CORBALogDomains.RPC_PROTOCOL ) ;
73     }
74 
ReplyMessage_1_0(ORB orb, ServiceContexts _service_contexts, int _request_id, int _reply_status, IOR _ior)75     ReplyMessage_1_0(ORB orb, ServiceContexts _service_contexts,
76             int _request_id, int _reply_status, IOR _ior) {
77         super(Message.GIOPBigMagic, false, Message.GIOPReply, 0);
78         this.orb = orb;
79         this.wrapper = ORBUtilSystemException.get( orb,
80             CORBALogDomains.RPC_PROTOCOL ) ;
81         service_contexts = _service_contexts;
82         request_id = _request_id;
83         reply_status = _reply_status;
84         ior = _ior;
85     }
86 
87     // Accessor methods
88 
getRequestId()89     public int getRequestId() {
90         return this.request_id;
91     }
92 
getReplyStatus()93     public int getReplyStatus() {
94         return this.reply_status;
95     }
96 
getAddrDisposition()97     public short getAddrDisposition() {
98         return KeyAddr.value;
99     }
100 
getServiceContexts()101     public ServiceContexts getServiceContexts() {
102         return this.service_contexts;
103     }
104 
setServiceContexts( ServiceContexts sc )105     public void setServiceContexts( ServiceContexts sc ) {
106         this.service_contexts = sc;
107     }
108 
getSystemException(String message)109     public SystemException getSystemException(String message) {
110         return MessageBase.getSystemException(
111             exClassName, minorCode, completionStatus, message, wrapper);
112     }
113 
getIOR()114     public IOR getIOR() {
115         return this.ior;
116     }
117 
setIOR( IOR ior )118     public void setIOR( IOR ior ) {
119         this.ior = ior;
120     }
121 
122     // IO methods
123 
read(org.omg.CORBA.portable.InputStream istream)124     public void read(org.omg.CORBA.portable.InputStream istream) {
125         super.read(istream);
126         this.service_contexts
127             = new ServiceContexts((org.omg.CORBA_2_3.portable.InputStream) istream);
128         this.request_id = istream.read_ulong();
129         this.reply_status = istream.read_long();
130         isValidReplyStatus(this.reply_status); // raises exception on error
131 
132         // The code below reads the reply body in some cases
133         // SYSTEM_EXCEPTION & LOCATION_FORWARD
134         if (this.reply_status == SYSTEM_EXCEPTION) {
135 
136             String reposId = istream.read_string();
137             this.exClassName = ORBUtility.classNameOf(reposId);
138             this.minorCode = istream.read_long();
139             int status = istream.read_long();
140 
141             switch (status) {
142             case CompletionStatus._COMPLETED_YES:
143                 this.completionStatus = CompletionStatus.COMPLETED_YES;
144                 break;
145             case CompletionStatus._COMPLETED_NO:
146                 this.completionStatus = CompletionStatus.COMPLETED_NO;
147                 break;
148             case CompletionStatus._COMPLETED_MAYBE:
149                 this.completionStatus = CompletionStatus.COMPLETED_MAYBE;
150                 break;
151             default:
152                 throw wrapper.badCompletionStatusInReply(
153                     CompletionStatus.COMPLETED_MAYBE, new Integer(status) );
154             }
155 
156         } else if (this.reply_status == USER_EXCEPTION) {
157             // do nothing. The client stub will read the exception from body.
158         } else if (this.reply_status == LOCATION_FORWARD) {
159             CDRInputStream cdr = (CDRInputStream) istream;
160             this.ior = IORFactories.makeIOR( cdr ) ;
161         }
162     }
163 
164     // Note, this writes only the header information. SystemException or
165     // IOR may be written afterwards into the reply mesg body.
write(org.omg.CORBA.portable.OutputStream ostream)166     public void write(org.omg.CORBA.portable.OutputStream ostream) {
167         super.write(ostream);
168         if (this.service_contexts != null) {
169                 service_contexts.write(
170                 (org.omg.CORBA_2_3.portable.OutputStream) ostream,
171                 GIOPVersion.V1_0);
172             } else {
173                 ServiceContexts.writeNullServiceContext(
174                 (org.omg.CORBA_2_3.portable.OutputStream) ostream);
175         }
176         ostream.write_ulong(this.request_id);
177         ostream.write_long(this.reply_status);
178     }
179 
180     // Static methods
181 
isValidReplyStatus(int replyStatus)182     public static void isValidReplyStatus(int replyStatus) {
183         switch (replyStatus) {
184         case NO_EXCEPTION :
185         case USER_EXCEPTION :
186         case SYSTEM_EXCEPTION :
187         case LOCATION_FORWARD :
188             break;
189         default :
190             ORBUtilSystemException localWrapper = ORBUtilSystemException.get(
191                 CORBALogDomains.RPC_PROTOCOL ) ;
192             throw localWrapper.illegalReplyStatus( CompletionStatus.COMPLETED_MAYBE);
193         }
194     }
195 
callback(MessageHandler handler)196     public void callback(MessageHandler handler)
197         throws java.io.IOException
198     {
199         handler.handleInput(this);
200     }
201 } //
202