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