Source for gnu.CORBA.GIOP.v1_2.RequestHeader

   1: /* RequestHeader.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.CORBA.GIOP.v1_2;
  40: 
  41: import gnu.CORBA.Minor;
  42: import gnu.CORBA.CDR.AbstractCdrInput;
  43: import gnu.CORBA.CDR.AbstractCdrOutput;
  44: import gnu.CORBA.GIOP.ServiceContext;
  45: import gnu.CORBA.GIOP.CodeSetServiceContext;
  46: 
  47: import java.io.IOException;
  48: 
  49: import org.omg.CORBA.MARSHAL;
  50: import org.omg.CORBA.NO_IMPLEMENT;
  51: 
  52: /**
  53:  * The GIOP 1.2 request header. The GIOP 1.1 request header
  54:  * is the same as GIOP 1.0 request header, if taking the
  55:  * alignment into consideration.
  56:  *
  57:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  58:  */
  59: public class RequestHeader
  60:   extends gnu.CORBA.GIOP.v1_0.RequestHeader
  61: {
  62:   /**
  63:    * Use serialVersionUID for interoperability.
  64:    */
  65:   private static final long serialVersionUID = 1;
  66: 
  67:   /**
  68:    * Indicates that the object is addressed by the object key.
  69:    */
  70:   public static final short KeyAddr = 0;
  71: 
  72:   /**
  73:    * Indicates that the object is addressed by the IOP tagged profile.
  74:    */
  75:   public static final short ProfileAddr = 1;
  76: 
  77:   /**
  78:    * Indicates that the objec is addressed by IOR addressing info.
  79:    */
  80:   public static final short ReferenceAddr = 2;
  81: 
  82:   /**
  83:    * The response flags of the header. By default, the flags are initialised
  84:    * by value 0x3 (response expected).
  85:    */
  86:   public byte response_flags = 3;
  87: 
  88:   /**
  89:    * The used addressing method.
  90:    */
  91:   public short AddressingDisposition;
  92: 
  93:   /**
  94:    * Adds the standard encoding context.
  95:    */
  96:   public RequestHeader()
  97:   {
  98:     service_context = new ServiceContext[] { CodeSetServiceContext.STANDARD };
  99:   }
 100: 
 101:   /**
 102:    * Set if the sender expects any response to this message.
 103:    * Clears or sets the 2 lower bits of flags
 104:    * (0 - not expected, 0x3 - expected).
 105:    */
 106:   public void setResponseExpected(boolean expected)
 107:   {
 108:     response_expected = expected;
 109: 
 110:     if (expected)
 111:       response_flags = (byte) (response_flags | 0x3);
 112:     else
 113:       response_flags = (byte) (response_flags & (~0x3));
 114:   }
 115: 
 116:   /**
 117:    * Return true if response is expected.
 118:    *
 119:    * @return true if the two lowest bits of the flags are set or
 120:    * the response expected is explicitly set to true.
 121:    */
 122:   public boolean isResponseExpected()
 123:   {
 124:     return response_expected || ((response_flags & 0x3) == 0x3);
 125:   }
 126: 
 127:   /**
 128:    * Read the header from the given stream.
 129:    *
 130:    * @param in a stream to read from.
 131:    */
 132:   public void read(AbstractCdrInput in)
 133:   {
 134:     try
 135:       {
 136:         request_id = in.read_ulong();
 137:         response_flags = (byte) in.read();
 138: 
 139:         // Skip 3 reserved octets:
 140:         in.skip(3);
 141: 
 142:         // Read target address.
 143:         AddressingDisposition = in.read_ushort();
 144: 
 145:         switch (AddressingDisposition)
 146:           {
 147:             case KeyAddr :
 148:               object_key = in.read_sequence();
 149:               break;
 150: 
 151:             // TODO FIXME add other addressing methods.
 152:             case ProfileAddr :
 153:               throw new NO_IMPLEMENT("Object addressing by IOP tagged profile");
 154: 
 155:             case ReferenceAddr :
 156:               throw new NO_IMPLEMENT("Object addressing by IOR addressing info");
 157: 
 158:             default :
 159:               MARSHAL m = new MARSHAL("Unknow addressing method in request, " +
 160:                                 AddressingDisposition
 161:                                );
 162:               m.minor = Minor.UnsupportedAddressing;
 163:               throw m;
 164:           }
 165: 
 166:         operation = in.read_string();
 167:         service_context = gnu.CORBA.GIOP.ServiceContext.readSequence(in);
 168: 
 169:         // No requesting principal in this new format.
 170:         in.setCodeSet(CodeSetServiceContext.find(service_context));
 171:       }
 172:     catch (IOException ex)
 173:       {
 174:         MARSHAL t = new MARSHAL();
 175:         t.minor = Minor.Header;
 176:         t.initCause(ex);
 177:         throw t;
 178:       }
 179:   }
 180: 
 181:   /**
 182:    * Return a string representation.
 183:    */
 184:   public String toString()
 185:   {
 186:     return "Request " + request_id + ", call '" + operation + "' on " +
 187:            bytes(object_key) + ", " +
 188:            (response_expected ? "wait response" : "one way") +
 189:            " addressed by " + " method " + AddressingDisposition + "." +
 190:            contexts();
 191:   }
 192: 
 193:   /**
 194:    * Write the header to the given stream.
 195:    *
 196:    * @param out a stream to write into.
 197:    */
 198:   public void write(AbstractCdrOutput out)
 199:   {
 200:     out.write_ulong(request_id);
 201: 
 202:     out.write(response_flags);
 203: 
 204:     // Skip 3 reserved octets:
 205:     out.write(0);
 206:     out.write(0);
 207:     out.write(0);
 208: 
 209:     // Write addressing disposition from IOR.
 210:     // TODO FIXME add other addressing methods.
 211:     out.write_ushort(KeyAddr);
 212: 
 213:     out.write_sequence(object_key);
 214: 
 215:     out.write_string(operation);
 216: 
 217:     ServiceContext.writeSequence(out, service_context);
 218: 
 219:     // No requesting principal in this new format.
 220:     out.setCodeSet(CodeSetServiceContext.find(service_context));
 221:   }
 222: }