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.ior.iiop;
  27 
  28 import java.util.Iterator ;
  29 
  30 import org.omg.IOP.TAG_INTERNET_IOP ;
  31 
  32 import org.omg.CORBA_2_3.portable.InputStream ;
  33 import org.omg.CORBA_2_3.portable.OutputStream ;
  34 
  35 import com.sun.corba.se.spi.ior.TaggedComponent ;
  36 import com.sun.corba.se.spi.ior.TaggedProfile ;
  37 import com.sun.corba.se.spi.ior.TaggedProfileTemplate ;
  38 import com.sun.corba.se.spi.ior.TaggedProfileTemplateBase ;
  39 import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
  40 import com.sun.corba.se.spi.ior.ObjectId ;
  41 import com.sun.corba.se.spi.ior.IdentifiableContainerBase ;
  42 import com.sun.corba.se.spi.ior.IdentifiableBase ;
  43 
  44 import com.sun.corba.se.impl.ior.EncapsulationUtility ;
  45 
  46 import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;
  47 import com.sun.corba.se.spi.ior.iiop.IIOPAddress ;
  48 import com.sun.corba.se.spi.ior.iiop.IIOPFactories ;
  49 
  50 import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
  51 
  52 import com.sun.corba.se.impl.encoding.CDROutputStream ;
  53 
  54 import com.sun.corba.se.spi.ior.iiop.GIOPVersion ;
  55 import com.sun.corba.se.spi.orb.ORB ;
  56 
  57 /**
  58  * @author
  59  * If getMinorVersion==0, this does not contain any tagged components
  60  */
  61 public class IIOPProfileTemplateImpl extends TaggedProfileTemplateBase
  62     implements IIOPProfileTemplate
  63 {
  64     private ORB orb ;
  65     private GIOPVersion giopVersion ;
  66     private IIOPAddress primary ;
  67 
  68     public boolean equals( Object obj )
  69     {
  70         if (!(obj instanceof IIOPProfileTemplateImpl))
  71             return false ;
  72 
  73         IIOPProfileTemplateImpl other = (IIOPProfileTemplateImpl)obj ;
  74 
  75         return super.equals( obj ) && giopVersion.equals( other.giopVersion ) &&
  76             primary.equals( other.primary ) ;
  77     }
  78 
  79     public int hashCode()
  80     {
  81         return super.hashCode() ^ giopVersion.hashCode() ^ primary.hashCode() ;
  82     }
  83 
  84     public TaggedProfile create( ObjectKeyTemplate oktemp, ObjectId id )
  85     {
  86         return IIOPFactories.makeIIOPProfile( orb, oktemp, id, this ) ;
  87     }
  88 
  89     public GIOPVersion getGIOPVersion()
  90     {
  91         return giopVersion ;
  92     }
  93 
  94     public IIOPAddress getPrimaryAddress()
  95     {
  96         return primary ;
  97     }
  98 
  99     public IIOPProfileTemplateImpl( ORB orb, GIOPVersion version, IIOPAddress primary )
 100     {
 101         this.orb = orb ;
 102         this.giopVersion = version ;
 103         this.primary = primary ;
 104         if (giopVersion.getMinor() == 0)
 105             // Adding tagged components is not allowed for IIOP 1.0,
 106             // so this template is complete and should be made immutable.
 107             makeImmutable() ;
 108     }
 109 
 110     public IIOPProfileTemplateImpl( InputStream istr )
 111     {
 112         byte major = istr.read_octet() ;
 113         byte minor = istr.read_octet() ;
 114         giopVersion = GIOPVersion.getInstance( major, minor ) ;
 115         primary = new IIOPAddressImpl( istr ) ;
 116         orb = (ORB)(istr.orb()) ;
 117         // Handle any tagged components (if applicable)
 118         if (minor > 0)
 119             EncapsulationUtility.readIdentifiableSequence(
 120                 this, orb.getTaggedComponentFactoryFinder(), istr ) ;
 121 
 122         makeImmutable() ;
 123     }
 124 
 125     public void write( ObjectKeyTemplate okeyTemplate, ObjectId id, OutputStream os)
 126     {
 127         giopVersion.write( os ) ;
 128         primary.write( os ) ;
 129 
 130         // Note that this is NOT an encapsulation: do not marshal
 131         // the endianness flag.  However, the length is required.
 132         // Note that this cannot be accomplished with a codec!
 133 
 134         // Use the byte order of the given stream
 135         OutputStream encapsulatedOS =
 136             sun.corba.OutputStreamFactory.newEncapsOutputStream(
 137                 (ORB)os.orb(), ((CDROutputStream)os).isLittleEndian() ) ;
 138 
 139         okeyTemplate.write( id, encapsulatedOS ) ;
 140         EncapsulationUtility.writeOutputStream( encapsulatedOS, os ) ;
 141 
 142         if (giopVersion.getMinor() > 0)
 143             EncapsulationUtility.writeIdentifiableSequence( this, os ) ;
 144     }
 145 
 146     /** Write out this IIOPProfileTemplateImpl only.
 147     */
 148     public void writeContents( OutputStream os)
 149     {
 150         giopVersion.write( os ) ;
 151         primary.write( os ) ;
 152 
 153         if (giopVersion.getMinor() > 0)
 154             EncapsulationUtility.writeIdentifiableSequence( this, os ) ;
 155     }
 156 
 157     public int getId()
 158     {
 159         return TAG_INTERNET_IOP.value ;
 160     }
 161 
 162     public boolean isEquivalent( TaggedProfileTemplate temp )
 163     {
 164         if (!(temp instanceof IIOPProfileTemplateImpl))
 165             return false ;
 166 
 167         IIOPProfileTemplateImpl tempimp = (IIOPProfileTemplateImpl)temp ;
 168 
 169         return primary.equals( tempimp.primary )  ;
 170     }
 171 
 172 }