1 /*
   2  * Copyright (c) 2000, 2004, 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.SystemException;
  30 import org.omg.CORBA.CompletionStatus;
  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 
  40 import com.sun.corba.se.impl.encoding.CDRInputStream;
  41 import com.sun.corba.se.impl.encoding.CDROutputStream;
  42 import com.sun.corba.se.impl.encoding.CDRInputStream_1_2;
  43 import com.sun.corba.se.impl.encoding.CDROutputStream_1_2;
  44 
  45 import com.sun.corba.se.impl.orbutil.ORBUtility;
  46 import com.sun.corba.se.impl.orbutil.ORBConstants;
  47 
  48 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  49 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  50 
  51 /**
  52  * This implements the GIOP 1.2 Reply header.
  53  *
  54  * @author Ram Jeyaraman 05/14/2000
  55  */
  56 
  57 public final class ReplyMessage_1_2 extends Message_1_2
  58         implements ReplyMessage {
  59 
  60     // Instance variables
  61 
  62     private ORB orb = null;
  63     private ORBUtilSystemException wrapper = null ;
  64     private int reply_status = (int) 0;
  65     private ServiceContexts service_contexts = null;
  66     private IOR ior = null;
  67     private String exClassName = null;
  68     private int minorCode = (int) 0;
  69     private CompletionStatus completionStatus = null;
  70     private short addrDisposition = KeyAddr.value; // default;
  71 
  72     // Constructors
  73 
  74     ReplyMessage_1_2(ORB orb) {
  75         this.orb = orb;
  76         this.wrapper = ORBUtilSystemException.get( orb,
  77             CORBALogDomains.RPC_PROTOCOL ) ;
  78     }
  79 
  80     ReplyMessage_1_2(ORB orb, int _request_id, int _reply_status,
  81             ServiceContexts _service_contexts, IOR _ior) {
  82         super(Message.GIOPBigMagic, GIOPVersion.V1_2, FLAG_NO_FRAG_BIG_ENDIAN,
  83             Message.GIOPReply, 0);
  84         this.orb = orb;
  85         this.wrapper = ORBUtilSystemException.get( orb,
  86             CORBALogDomains.RPC_PROTOCOL ) ;
  87         request_id = _request_id;
  88         reply_status = _reply_status;
  89         service_contexts = _service_contexts;
  90         ior = _ior;
  91     }
  92 
  93     // Accessor methods
  94 
  95     public int getRequestId() {
  96         return this.request_id;
  97     }
  98 
  99     public int getReplyStatus() {
 100         return this.reply_status;
 101     }
 102 
 103     public short getAddrDisposition() {
 104         return this.addrDisposition;
 105     }
 106 
 107     public ServiceContexts getServiceContexts() {
 108         return this.service_contexts;
 109     }
 110 
 111     public void setServiceContexts( ServiceContexts sc ) {
 112         this.service_contexts = sc;
 113     }
 114 
 115     public SystemException getSystemException(String message) {
 116         return MessageBase.getSystemException(
 117             exClassName, minorCode, completionStatus, message, wrapper);
 118     }
 119 
 120     public IOR getIOR() {
 121         return this.ior;
 122     }
 123 
 124     public void setIOR( IOR ior ) {
 125         this.ior = ior;
 126     }
 127 
 128     // IO methods
 129 
 130     public void read(org.omg.CORBA.portable.InputStream istream) {
 131         super.read(istream);
 132         this.request_id = istream.read_ulong();
 133         this.reply_status = istream.read_long();
 134         isValidReplyStatus(this.reply_status); // raises exception on error
 135         this.service_contexts
 136             = new ServiceContexts((org.omg.CORBA_2_3.portable.InputStream) istream);
 137 
 138         // CORBA formal 00-11-0 15.4.2.2 GIOP 1.2 body must be
 139         // aligned on an 8 octet boundary.
 140         // Ensures that the first read operation called from the stub code,
 141         // during body deconstruction, would skip the header padding, that was
 142         // inserted to ensure that the body was aligned on an 8-octet boundary.
 143         ((CDRInputStream)istream).setHeaderPadding(true);
 144 
 145         // The code below reads the reply body in some cases
 146         // SYSTEM_EXCEPTION & LOCATION_FORWARD & LOCATION_FORWARD_PERM &
 147         // NEEDS_ADDRESSING_MODE
 148         if (this.reply_status == SYSTEM_EXCEPTION) {
 149 
 150             String reposId = istream.read_string();
 151             this.exClassName = ORBUtility.classNameOf(reposId);
 152             this.minorCode = istream.read_long();
 153             int status = istream.read_long();
 154 
 155             switch (status) {
 156             case CompletionStatus._COMPLETED_YES:
 157                 this.completionStatus = CompletionStatus.COMPLETED_YES;
 158                 break;
 159             case CompletionStatus._COMPLETED_NO:
 160                 this.completionStatus = CompletionStatus.COMPLETED_NO;
 161                 break;
 162             case CompletionStatus._COMPLETED_MAYBE:
 163                 this.completionStatus = CompletionStatus.COMPLETED_MAYBE;
 164                 break;
 165             default:
 166                 throw wrapper.badCompletionStatusInReply(
 167                     CompletionStatus.COMPLETED_MAYBE, new Integer(status) );
 168             }
 169 
 170         } else if (this.reply_status == USER_EXCEPTION) {
 171             // do nothing. The client stub will read the exception from body.
 172         } else if ( (this.reply_status == LOCATION_FORWARD) ||
 173                 (this.reply_status == LOCATION_FORWARD_PERM) ){
 174             CDRInputStream cdr = (CDRInputStream) istream;
 175             this.ior = IORFactories.makeIOR( cdr ) ;
 176         }  else if (this.reply_status == NEEDS_ADDRESSING_MODE) {
 177             // read GIOP::AddressingDisposition from body and resend the
 178             // original request using the requested addressing mode. The
 179             // resending is transparent to the client program.
 180             this.addrDisposition = AddressingDispositionHelper.read(istream);
 181         }
 182     }
 183 
 184     // Note, this writes only the header information. SystemException or
 185     // IOR or GIOP::AddressingDisposition may be written afterwards into the
 186     // reply mesg body.
 187     public void write(org.omg.CORBA.portable.OutputStream ostream) {
 188         super.write(ostream);
 189         ostream.write_ulong(this.request_id);
 190         ostream.write_long(this.reply_status);
 191         if (this.service_contexts != null) {
 192                 service_contexts.write(
 193                 (org.omg.CORBA_2_3.portable.OutputStream) ostream,
 194                 GIOPVersion.V1_2);
 195             } else {
 196                 ServiceContexts.writeNullServiceContext(
 197                 (org.omg.CORBA_2_3.portable.OutputStream) ostream);
 198         }
 199 
 200         // CORBA formal 00-11-0 15.4.2.2 GIOP 1.2 body must be
 201         // aligned on an 8 octet boundary.
 202         // Ensures that the first write operation called from the stub code,
 203         // during body construction, would insert a header padding, such that
 204         // the body is aligned on an 8-octet boundary.
 205         ((CDROutputStream)ostream).setHeaderPadding(true);
 206 
 207     }
 208 
 209     // Static methods
 210 
 211     public static void isValidReplyStatus(int replyStatus) {
 212         switch (replyStatus) {
 213         case NO_EXCEPTION :
 214         case USER_EXCEPTION :
 215         case SYSTEM_EXCEPTION :
 216         case LOCATION_FORWARD :
 217         case LOCATION_FORWARD_PERM :
 218         case NEEDS_ADDRESSING_MODE :
 219             break;
 220         default :
 221             ORBUtilSystemException localWrapper = ORBUtilSystemException.get(
 222                 CORBALogDomains.RPC_PROTOCOL ) ;
 223             throw localWrapper.illegalReplyStatus( CompletionStatus.COMPLETED_MAYBE);
 224         }
 225     }
 226 
 227     public void callback(MessageHandler handler)
 228         throws java.io.IOException
 229     {
 230         handler.handleInput(this);
 231     }
 232 } // class ReplyMessage_1_2