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