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.1 Reply header.
48  *
49  * @author Ram Jeyaraman 05/14/2000
50  */
51 
52 public final class ReplyMessage_1_1 extends Message_1_1
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_1(ORB orb)69     ReplyMessage_1_1(ORB orb) {
70         this.orb = orb;
71         this.wrapper = ORBUtilSystemException.get( orb,
72             CORBALogDomains.RPC_PROTOCOL ) ;
73     }
74 
ReplyMessage_1_1(ORB orb, ServiceContexts _service_contexts, int _request_id, int _reply_status, IOR _ior)75     ReplyMessage_1_1(ORB orb, ServiceContexts _service_contexts,
76             int _request_id, int _reply_status, IOR _ior) {
77         super(Message.GIOPBigMagic, GIOPVersion.V1_1, FLAG_NO_FRAG_BIG_ENDIAN,
78             Message.GIOPReply, 0);
79         this.orb = orb;
80         this.wrapper = ORBUtilSystemException.get( orb,
81             CORBALogDomains.RPC_PROTOCOL ) ;
82         service_contexts = _service_contexts;
83         request_id = _request_id;
84         reply_status = _reply_status;
85         ior = _ior;
86     }
87 
88     // Accessor methods
89 
getRequestId()90     public int getRequestId() {
91         return this.request_id;
92     }
93 
getReplyStatus()94     public int getReplyStatus() {
95         return this.reply_status;
96     }
97 
getAddrDisposition()98     public short getAddrDisposition() {
99         return KeyAddr.value;
100     }
101 
getServiceContexts()102     public ServiceContexts getServiceContexts() {
103         return this.service_contexts;
104     }
105 
setServiceContexts( ServiceContexts sc )106     public void setServiceContexts( ServiceContexts sc ) {
107         this.service_contexts = sc;
108     }
109 
getSystemException(String message)110     public SystemException getSystemException(String message) {
111         return MessageBase.getSystemException(
112             exClassName, minorCode, completionStatus, message, wrapper);
113     }
114 
getIOR()115     public IOR getIOR() {
116         return this.ior;
117     }
118 
setIOR( IOR ior )119     public void setIOR( IOR ior ) {
120         this.ior = ior;
121     }
122 
123     // IO methods
124 
read(org.omg.CORBA.portable.InputStream istream)125     public void read(org.omg.CORBA.portable.InputStream istream) {
126         super.read(istream);
127         this.service_contexts
128             = new ServiceContexts((org.omg.CORBA_2_3.portable.InputStream) istream);
129         this.request_id = istream.read_ulong();
130         this.reply_status = istream.read_long();
131         isValidReplyStatus(this.reply_status); // raises exception on error
132 
133         // The code below reads the reply body in some cases
134         // SYSTEM_EXCEPTION & LOCATION_FORWARD
135         if (this.reply_status == SYSTEM_EXCEPTION) {
136 
137             String reposId = istream.read_string();
138             this.exClassName = ORBUtility.classNameOf(reposId);
139             this.minorCode = istream.read_long();
140             int status = istream.read_long();
141 
142             switch (status) {
143             case CompletionStatus._COMPLETED_YES:
144                 this.completionStatus = CompletionStatus.COMPLETED_YES;
145                 break;
146             case CompletionStatus._COMPLETED_NO:
147                 this.completionStatus = CompletionStatus.COMPLETED_NO;
148                 break;
149             case CompletionStatus._COMPLETED_MAYBE:
150                 this.completionStatus = CompletionStatus.COMPLETED_MAYBE;
151                 break;
152             default:
153                 throw wrapper.badCompletionStatusInReply(
154                     CompletionStatus.COMPLETED_MAYBE, new Integer(status) );
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_1);
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