1 /*
   2  * Copyright (c) 2003, 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.transport;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.nio.ByteBuffer;
  31 
  32 import org.omg.CORBA.INTERNAL;
  33 
  34 import com.sun.corba.se.pept.broker.Broker;
  35 import com.sun.corba.se.pept.encoding.InputObject;
  36 import com.sun.corba.se.pept.encoding.OutputObject;
  37 import com.sun.corba.se.pept.protocol.ClientRequestDispatcher;
  38 import com.sun.corba.se.pept.protocol.MessageMediator;
  39 import com.sun.corba.se.pept.transport.ByteBufferPool;
  40 import com.sun.corba.se.pept.transport.ContactInfo;
  41 import com.sun.corba.se.pept.transport.ContactInfoList;
  42 import com.sun.corba.se.pept.transport.Connection;
  43 import com.sun.corba.se.pept.transport.EventHandler;
  44 import com.sun.corba.se.pept.transport.OutboundConnectionCache;
  45 
  46 import com.sun.corba.se.spi.orb.ORB;
  47 import com.sun.corba.se.spi.ior.IOR;
  48 import com.sun.corba.se.spi.ior.ObjectKeyTemplate;
  49 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
  50 import com.sun.corba.se.spi.ior.iiop.IIOPAddress ;
  51 import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate;
  52 import com.sun.corba.se.spi.ior.iiop.IIOPProfile;
  53 import com.sun.corba.se.spi.protocol.CorbaMessageMediator;
  54 import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry;
  55 import com.sun.corba.se.spi.transport.CorbaConnection;
  56 import com.sun.corba.se.spi.transport.CorbaContactInfoList;
  57 import com.sun.corba.se.spi.transport.CorbaContactInfo;
  58 
  59 import com.sun.corba.se.impl.encoding.ByteBufferWithInfo;
  60 import com.sun.corba.se.impl.encoding.CDRInputObject;
  61 import com.sun.corba.se.impl.encoding.CDRInputStream_1_0;
  62 import com.sun.corba.se.impl.encoding.CDROutputObject;
  63 import com.sun.corba.se.impl.protocol.CorbaMessageMediatorImpl;
  64 import com.sun.corba.se.impl.protocol.giopmsgheaders.KeyAddr;
  65 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
  66 import com.sun.corba.se.impl.protocol.giopmsgheaders.MessageBase;
  67 import com.sun.corba.se.impl.orbutil.ORBUtility;
  68 
  69 /**
  70  * @author Harold Carr
  71  */
  72 public abstract class CorbaContactInfoBase
  73     implements
  74         CorbaContactInfo
  75 {
  76     protected ORB orb;
  77     protected CorbaContactInfoList contactInfoList;
  78     // NOTE: This may be different from same named one in CorbaContactInfoList.
  79     protected IOR effectiveTargetIOR;
  80     protected short addressingDisposition;
  81     protected OutboundConnectionCache connectionCache;
  82 
  83     ////////////////////////////////////////////////////
  84     //
  85     // pept.transport.ContactInfo
  86     //
  87 
  88     public Broker getBroker()
  89     {
  90         return orb;
  91     }
  92 
  93     public ContactInfoList getContactInfoList()
  94     {
  95         return contactInfoList;
  96     }
  97 
  98     public ClientRequestDispatcher getClientRequestDispatcher()
  99     {
 100         int scid =
 101             getEffectiveProfile().getObjectKeyTemplate().getSubcontractId() ;
 102         RequestDispatcherRegistry scr = orb.getRequestDispatcherRegistry() ;
 103         return scr.getClientRequestDispatcher( scid ) ;
 104     }
 105 
 106     // Note: not all derived classes will use a connection cache.
 107     // These are convenience methods that may not be used.
 108     public void setConnectionCache(OutboundConnectionCache connectionCache)
 109     {
 110         this.connectionCache = connectionCache;
 111     }
 112 
 113     public OutboundConnectionCache getConnectionCache()
 114     {
 115         return connectionCache;
 116     }
 117 
 118     // Called when client making an invocation.
 119     public MessageMediator createMessageMediator(Broker broker,
 120                                                  ContactInfo contactInfo,
 121                                                  Connection connection,
 122                                                  String methodName,
 123                                                  boolean isOneWay)
 124     {
 125         // REVISIT: Would like version, ior, requestid, etc., decisions
 126         // to be in client subcontract.  Cannot pass these to this
 127         // factory method because it breaks generic abstraction.
 128         // Maybe set methods on mediator called from subcontract
 129         // after creation?
 130         CorbaMessageMediator messageMediator =
 131             new CorbaMessageMediatorImpl(
 132                 (ORB) broker,
 133                 contactInfo,
 134                 connection,
 135                 GIOPVersion.chooseRequestVersion( (ORB)broker,
 136                      effectiveTargetIOR),
 137                 effectiveTargetIOR,
 138                 ((CorbaConnection)connection).getNextRequestId(),
 139                 getAddressingDisposition(),
 140                 methodName,
 141                 isOneWay);
 142 
 143         return messageMediator;
 144     }
 145 
 146     // Called when connection handling a read event.
 147     public MessageMediator createMessageMediator(Broker broker,Connection conn)
 148     {
 149         ORB orb = (ORB) broker;
 150         CorbaConnection connection = (CorbaConnection) conn;
 151 
 152         if (orb.transportDebugFlag) {
 153             if (connection.shouldReadGiopHeaderOnly()) {
 154                 dprint(
 155                 ".createMessageMediator: waiting for message header on connection: "
 156                 + connection);
 157             } else {
 158                 dprint(
 159                 ".createMessageMediator: waiting for message on connection: "
 160                 + connection);
 161             }
 162         }
 163 
 164         Message msg = null;
 165 
 166         if (connection.shouldReadGiopHeaderOnly()) {
 167             // read giop header only
 168             msg = MessageBase.readGIOPHeader(orb, connection);
 169         } else {
 170             // read entire giop message
 171             msg = MessageBase.readGIOPMessage(orb, connection);
 172         }
 173 
 174         ByteBuffer byteBuffer = msg.getByteBuffer();
 175         msg.setByteBuffer(null);
 176         CorbaMessageMediator messageMediator =
 177             new CorbaMessageMediatorImpl(orb, connection, msg, byteBuffer);
 178 
 179         return messageMediator;
 180     }
 181 
 182     // Called when connection reading message body
 183     public MessageMediator finishCreatingMessageMediator(Broker broker,
 184                                Connection conn, MessageMediator messageMediator)
 185     {
 186         ORB orb = (ORB) broker;
 187         CorbaConnection connection = (CorbaConnection) conn;
 188         CorbaMessageMediator corbaMessageMediator =
 189                       (CorbaMessageMediator)messageMediator;
 190 
 191         if (orb.transportDebugFlag) {
 192             dprint(
 193             ".finishCreatingMessageMediator: waiting for message body on connection: "
 194                 + connection);
 195         }
 196 
 197         Message msg = corbaMessageMediator.getDispatchHeader();
 198         msg.setByteBuffer(corbaMessageMediator.getDispatchBuffer());
 199 
 200         // read giop body only
 201         msg = MessageBase.readGIOPBody(orb, connection, msg);
 202 
 203         ByteBuffer byteBuffer = msg.getByteBuffer();
 204         msg.setByteBuffer(null);
 205         corbaMessageMediator.setDispatchHeader(msg);
 206         corbaMessageMediator.setDispatchBuffer(byteBuffer);
 207 
 208         return corbaMessageMediator;
 209     }
 210 
 211     public OutputObject createOutputObject(MessageMediator messageMediator)
 212     {
 213         CorbaMessageMediator corbaMessageMediator = (CorbaMessageMediator)
 214             messageMediator;
 215 
 216         OutputObject outputObject =
 217             sun.corba.OutputStreamFactory.newCDROutputObject(orb, messageMediator,
 218                                 corbaMessageMediator.getRequestHeader(),
 219                                 corbaMessageMediator.getStreamFormatVersion());
 220 
 221         messageMediator.setOutputObject(outputObject);
 222         return outputObject;
 223     }
 224 
 225     public InputObject createInputObject(Broker broker,
 226                                          MessageMediator messageMediator)
 227     {
 228         // REVISIT: Duplicate of acceptor code.
 229         CorbaMessageMediator corbaMessageMediator = (CorbaMessageMediator)
 230             messageMediator;
 231         return new CDRInputObject((ORB)broker,
 232                                   (CorbaConnection)messageMediator.getConnection(),
 233                                   corbaMessageMediator.getDispatchBuffer(),
 234                                   corbaMessageMediator.getDispatchHeader());
 235     }
 236 
 237     ////////////////////////////////////////////////////
 238     //
 239     // spi.transport.CorbaContactInfo
 240     //
 241 
 242     public short getAddressingDisposition()
 243     {
 244         return addressingDisposition;
 245     }
 246 
 247     public void setAddressingDisposition(short addressingDisposition)
 248     {
 249         this.addressingDisposition = addressingDisposition;
 250     }
 251 
 252     // REVISIT - remove this.
 253     public IOR getTargetIOR()
 254     {
 255         return  contactInfoList.getTargetIOR();
 256     }
 257 
 258     public IOR getEffectiveTargetIOR()
 259     {
 260         return effectiveTargetIOR ;
 261     }
 262 
 263     public IIOPProfile getEffectiveProfile()
 264     {
 265         return effectiveTargetIOR.getProfile();
 266     }
 267 
 268     ////////////////////////////////////////////////////
 269     //
 270     // java.lang.Object
 271     //
 272 
 273     public String toString()
 274     {
 275         return
 276             "CorbaContactInfoBase["
 277             + "]";
 278     }
 279 
 280 
 281     ////////////////////////////////////////////////////
 282     //
 283     // Implementation
 284     //
 285 
 286     protected void dprint(String msg)
 287     {
 288         ORBUtility.dprint("CorbaContactInfoBase", msg);
 289     }
 290 }
 291 
 292 // End of file.