1 /*
   2  * Copyright (c) 1999, 2004, 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  * Licensed Materials - Property of IBM
  27  * RMI-IIOP v1.0
  28  * Copyright IBM Corp. 1998 1999  All Rights Reserved
  29  *
  30  */
  31 
  32 package com.sun.corba.se.impl.javax.rmi.CORBA;
  33 
  34 import java.io.IOException;
  35 
  36 import java.rmi.RemoteException;
  37 
  38 import javax.rmi.CORBA.Tie;
  39 
  40 import org.omg.CORBA.ORB;
  41 import org.omg.CORBA.SystemException;
  42 import org.omg.CORBA.BAD_OPERATION;
  43 import org.omg.CORBA.BAD_INV_ORDER;
  44 
  45 import org.omg.CORBA.portable.Delegate;
  46 import org.omg.CORBA.portable.OutputStream;
  47 import org.omg.CORBA.portable.InputStream;
  48 
  49 import com.sun.corba.se.spi.presentation.rmi.StubAdapter;
  50 
  51 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  52 
  53 import com.sun.corba.se.impl.util.Utility;
  54 
  55 import com.sun.corba.se.impl.ior.StubIORImpl ;
  56 import com.sun.corba.se.impl.presentation.rmi.StubConnectImpl ;
  57 
  58 import com.sun.corba.se.impl.logging.UtilSystemException ;
  59 
  60 /**
  61  * Base class from which all static RMI-IIOP stubs must inherit.
  62  */
  63 public class StubDelegateImpl implements javax.rmi.CORBA.StubDelegate
  64 {
  65     static UtilSystemException wrapper = UtilSystemException.get(
  66         CORBALogDomains.RMIIIOP ) ;
  67 
  68     private StubIORImpl ior ;
  69 
  70     public StubIORImpl getIOR()
  71     {
  72         return ior ;
  73     }
  74 
  75     public StubDelegateImpl()
  76     {
  77         ior = null ;
  78     }
  79 
  80     /**
  81      * Sets the IOR components if not already set.
  82      */
  83     private void init (javax.rmi.CORBA.Stub self)
  84     {
  85         // If the Stub is not connected to an ORB, BAD_OPERATION exception
  86         // will be raised by the code below.
  87         if (ior == null)
  88             ior = new StubIORImpl( self ) ;
  89     }
  90 
  91     /**
  92      * Returns a hash code value for the object which is the same for all stubs
  93      * that represent the same remote object.
  94      * @return the hash code value.
  95      */
  96     public int hashCode(javax.rmi.CORBA.Stub self)
  97     {
  98         init(self);
  99         return ior.hashCode() ;
 100     }
 101 
 102     /**
 103      * Compares two stubs for equality. Returns <code>true</code> when used to compare stubs
 104      * that represent the same remote object, and <code>false</code> otherwise.
 105      * @param obj the reference object with which to compare.
 106      * @return <code>true</code> if this object is the same as the <code>obj</code>
 107      *          argument; <code>false</code> otherwise.
 108      */
 109     public boolean equals(javax.rmi.CORBA.Stub self, java.lang.Object obj)
 110     {
 111         if (self == obj) {
 112             return true;
 113         }
 114 
 115         if (!(obj instanceof javax.rmi.CORBA.Stub)) {
 116             return false;
 117         }
 118 
 119         // no need to call init() because of calls to hashCode() below
 120 
 121         javax.rmi.CORBA.Stub other = (javax.rmi.CORBA.Stub) obj;
 122         if (other.hashCode() != self.hashCode()) {
 123             return false;
 124         }
 125 
 126         // hashCodes being the same does not mean equality. The stubs still
 127         // could be pointing to different IORs. So, do a literal comparison.
 128         // Apparently the ONLY way to do this (other than using private
 129         // reflection)  toString, because it is not possible to directly
 130         // access the StubDelegateImpl from the Stub.
 131         return self.toString().equals( other.toString() ) ;
 132     }
 133 
 134     public boolean equals( Object obj )
 135     {
 136         if (this == obj)
 137             return true ;
 138 
 139         if (!(obj instanceof StubDelegateImpl))
 140             return false ;
 141 
 142         StubDelegateImpl other = (StubDelegateImpl)obj ;
 143 
 144         if (ior == null)
 145             return ior == other.ior ;
 146         else
 147             return ior.equals( other.ior ) ;
 148     }
 149 
 150     /**
 151      * Returns a string representation of this stub. Returns the same string
 152      * for all stubs that represent the same remote object.
 153      * @return a string representation of this stub.
 154      */
 155     public String toString(javax.rmi.CORBA.Stub self)
 156     {
 157         if (ior == null)
 158             return null ;
 159         else
 160             return ior.toString() ;
 161     }
 162 
 163     /**
 164      * Connects this stub to an ORB. Required after the stub is deserialized
 165      * but not after it is demarshalled by an ORB stream. If an unconnected
 166      * stub is passed to an ORB stream for marshalling, it is implicitly
 167      * connected to that ORB. Application code should not call this method
 168      * directly, but should call the portable wrapper method
 169      * {@link javax.rmi.PortableRemoteObject#connect}.
 170      * @param orb the ORB to connect to.
 171      * @exception RemoteException if the stub is already connected to a different
 172      * ORB, or if the stub does not represent an exported remote or local object.
 173      */
 174     public void connect(javax.rmi.CORBA.Stub self, ORB orb)
 175         throws RemoteException
 176     {
 177         ior = StubConnectImpl.connect( ior, self, self, orb ) ;
 178     }
 179 
 180     /**
 181      * Serialization method to restore the IOR state.
 182      */
 183     public void readObject(javax.rmi.CORBA.Stub self,
 184         java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException
 185     {
 186         if (ior == null)
 187             ior = new StubIORImpl() ;
 188 
 189         ior.doRead( stream ) ;
 190     }
 191 
 192     /**
 193      * Serialization method to save the IOR state.
 194      * @serialData The length of the IOR type ID (int), followed by the IOR type ID
 195      * (byte array encoded using ISO8859-1), followed by the number of IOR profiles
 196      * (int), followed by the IOR profiles.  Each IOR profile is written as a
 197      * profile tag (int), followed by the length of the profile data (int), followed
 198      * by the profile data (byte array).
 199      */
 200     public void writeObject(javax.rmi.CORBA.Stub self,
 201         java.io.ObjectOutputStream stream) throws IOException
 202     {
 203         init(self);
 204         ior.doWrite( stream ) ;
 205     }
 206 }