1 /*
   2  * Copyright (c) 2000, 2003, 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;
  27 
  28 import java.util.Iterator ;
  29 
  30 import org.omg.CORBA_2_3.portable.InputStream ;
  31 import org.omg.CORBA_2_3.portable.OutputStream ;
  32 
  33 import com.sun.corba.se.spi.protocol.CorbaServerRequestDispatcher ;
  34 
  35 import com.sun.corba.se.spi.ior.ObjectId ;
  36 import com.sun.corba.se.spi.ior.ObjectAdapterId ;
  37 import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
  38 
  39 import com.sun.corba.se.spi.orb.ORB ;
  40 import com.sun.corba.se.spi.orb.ORBVersion ;
  41 
  42 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  43 
  44 
  45 import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
  46 
  47 import com.sun.corba.se.impl.logging.IORSystemException ;
  48 
  49 
  50 /**
  51  * @author
  52  */
  53 public abstract class ObjectKeyTemplateBase implements ObjectKeyTemplate
  54 {
  55     // Fixed constants for Java IDL object key template forms
  56     public static final String JIDL_ORB_ID = "" ;
  57     private static final String[] JIDL_OAID_STRINGS = { "TransientObjectAdapter" } ;
  58     public static final ObjectAdapterId JIDL_OAID = new ObjectAdapterIdArray( JIDL_OAID_STRINGS ) ;
  59 
  60     private ORB orb ;
  61     protected IORSystemException wrapper ;
  62     private ORBVersion version ;
  63     private int magic ;
  64     private int scid ;
  65     private int serverid ;
  66     private String orbid ;
  67     private ObjectAdapterId oaid ;
  68 
  69     private byte[] adapterId ;
  70 
  71     public byte[] getAdapterId()
  72     {
  73         return (byte[])(adapterId.clone()) ;
  74     }
  75 
  76     private byte[] computeAdapterId()
  77     {
  78         // write out serverid, orbid, oaid
  79         ByteBuffer buff = new ByteBuffer() ;
  80 
  81         buff.append( getServerId() ) ;
  82         buff.append( orbid ) ;
  83 
  84         buff.append( oaid.getNumLevels() ) ;
  85         Iterator iter = oaid.iterator() ;
  86         while (iter.hasNext()) {
  87             String comp = (String)(iter.next()) ;
  88             buff.append( comp ) ;
  89         }
  90 
  91         buff.trimToSize() ;
  92 
  93         return buff.toArray() ;
  94     }
  95 
  96     public ObjectKeyTemplateBase( ORB orb, int magic, int scid, int serverid,
  97         String orbid, ObjectAdapterId oaid )
  98     {
  99         this.orb = orb ;
 100         this.wrapper = IORSystemException.get( orb,
 101             CORBALogDomains.OA_IOR ) ;
 102         this.magic = magic ;
 103         this.scid = scid ;
 104         this.serverid = serverid ;
 105         this.orbid = orbid ;
 106         this.oaid = oaid ;
 107 
 108         adapterId = computeAdapterId() ;
 109     }
 110 
 111     public boolean equals( Object obj )
 112     {
 113         if (!(obj instanceof ObjectKeyTemplateBase))
 114             return false ;
 115 
 116         ObjectKeyTemplateBase other = (ObjectKeyTemplateBase)obj ;
 117 
 118         return (magic == other.magic) && (scid == other.scid) &&
 119             (serverid == other.serverid) && (version.equals( other.version ) &&
 120             orbid.equals( other.orbid ) && oaid.equals( other.oaid )) ;
 121     }
 122 
 123     public int hashCode()
 124     {
 125         int result = 17 ;
 126         result = 37*result + magic ;
 127         result = 37*result + scid ;
 128         result = 37*result + serverid ;
 129         result = 37*result + version.hashCode() ;
 130         result = 37*result + orbid.hashCode() ;
 131         result = 37*result + oaid.hashCode() ;
 132         return result ;
 133     }
 134 
 135     public int getSubcontractId()
 136     {
 137         return scid ;
 138     }
 139 
 140     public int getServerId()
 141     {
 142         return serverid ;
 143     }
 144 
 145     public String getORBId()
 146     {
 147         return orbid ;
 148     }
 149 
 150     public ObjectAdapterId getObjectAdapterId()
 151     {
 152         return oaid ;
 153     }
 154 
 155     public void write(ObjectId objectId, OutputStream os)
 156     {
 157         writeTemplate( os ) ;
 158         objectId.write( os ) ;
 159     }
 160 
 161     public void write( OutputStream os )
 162     {
 163         writeTemplate( os ) ;
 164     }
 165 
 166     abstract protected void writeTemplate( OutputStream os ) ;
 167 
 168     protected int getMagic()
 169     {
 170         return magic ;
 171     }
 172 
 173     // All subclasses should set the version in their constructors.
 174     // Public so it can be used in a white-box test.
 175     public void setORBVersion( ORBVersion version )
 176     {
 177         this.version = version ;
 178     }
 179 
 180     public ORBVersion getORBVersion()
 181     {
 182         return version ;
 183     }
 184 
 185     protected byte[] readObjectKey( InputStream is )
 186     {
 187         int len = is.read_long() ;
 188         byte[] result = new byte[len] ;
 189         is.read_octet_array( result, 0, len ) ;
 190         return result ;
 191     }
 192 
 193     public CorbaServerRequestDispatcher getServerRequestDispatcher( ORB orb, ObjectId id )
 194     {
 195         return orb.getRequestDispatcherRegistry().getServerRequestDispatcher( scid ) ;
 196     }
 197 }