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