src/share/classes/com/sun/corba/se/impl/ior/StubIORImpl.java

Wed, 07 Feb 2018 00:10:57 -0800

author
rpatil
date
Wed, 07 Feb 2018 00:10:57 -0800
changeset 1692
b90a8fc589af
parent 158
91006f157c46
child 1699
b3563151fe42
permissions
-rw-r--r--

8192757: Improve stub classes implementation
Reviewed-by: rriggs, dfuchs, erikj

duke@1 1 /*
rpatil@1692 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@158 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@158 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@158 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@158 22 * or visit www.oracle.com if you need additional information or have any
ohair@158 23 * questions.
duke@1 24 */
duke@1 25 /*
duke@1 26 * Licensed Materials - Property of IBM
duke@1 27 * RMI-IIOP v1.0
duke@1 28 * Copyright IBM Corp. 1998 1999 All Rights Reserved
duke@1 29 *
duke@1 30 */
duke@1 31
duke@1 32 package com.sun.corba.se.impl.ior;
duke@1 33
duke@1 34 import java.io.ObjectInputStream ;
duke@1 35 import java.io.ObjectOutputStream ;
duke@1 36 import java.io.IOException ;
duke@1 37
duke@1 38 import org.omg.CORBA.ORB ;
duke@1 39
duke@1 40 import org.omg.CORBA.portable.Delegate ;
duke@1 41 import org.omg.CORBA.portable.InputStream ;
duke@1 42 import org.omg.CORBA.portable.OutputStream ;
duke@1 43
duke@1 44 // Be very careful: com.sun.corba imports must not depend on
duke@1 45 // PEORB internal classes in ways that prevent portability to
duke@1 46 // other vendor's ORBs.
duke@1 47 import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
duke@1 48 import com.sun.corba.se.impl.orbutil.HexOutputStream ;
rpatil@1692 49 import sun.corba.SharedSecrets;
duke@1 50
duke@1 51 /**
duke@1 52 * This class implements a very simply IOR representation
duke@1 53 * which must be completely ORBImpl free so that this class
duke@1 54 * can be used in the implementation of a portable StubDelegateImpl.
duke@1 55 */
duke@1 56 public class StubIORImpl
duke@1 57 {
duke@1 58 // cached hash code
duke@1 59 private int hashCode;
duke@1 60
duke@1 61 // IOR components
duke@1 62 private byte[] typeData;
duke@1 63 private int[] profileTags;
duke@1 64 private byte[][] profileData;
duke@1 65
duke@1 66 public StubIORImpl()
duke@1 67 {
duke@1 68 hashCode = 0 ;
duke@1 69 typeData = null ;
duke@1 70 profileTags = null ;
duke@1 71 profileData = null ;
duke@1 72 }
duke@1 73
duke@1 74 public String getRepositoryId()
duke@1 75 {
duke@1 76 if (typeData == null)
duke@1 77 return null ;
duke@1 78
duke@1 79 return new String( typeData ) ;
duke@1 80 }
duke@1 81
duke@1 82 public StubIORImpl( org.omg.CORBA.Object obj )
duke@1 83 {
duke@1 84 // write the IOR to an OutputStream and get an InputStream
duke@1 85 OutputStream ostr = StubAdapter.getORB( obj ).create_output_stream();
duke@1 86 ostr.write_Object(obj);
duke@1 87 InputStream istr = ostr.create_input_stream();
duke@1 88
duke@1 89 // read the IOR components back from the stream
duke@1 90 int typeLength = istr.read_long();
duke@1 91 typeData = new byte[typeLength];
duke@1 92 istr.read_octet_array(typeData, 0, typeLength);
duke@1 93 int numProfiles = istr.read_long();
duke@1 94 profileTags = new int[numProfiles];
duke@1 95 profileData = new byte[numProfiles][];
duke@1 96 for (int i = 0; i < numProfiles; i++) {
duke@1 97 profileTags[i] = istr.read_long();
duke@1 98 profileData[i] = new byte[istr.read_long()];
duke@1 99 istr.read_octet_array(profileData[i], 0, profileData[i].length);
duke@1 100 }
duke@1 101 }
duke@1 102
duke@1 103 public Delegate getDelegate( ORB orb )
duke@1 104 {
duke@1 105 // write the IOR components to an org.omg.CORBA.portable.OutputStream
duke@1 106 OutputStream ostr = orb.create_output_stream();
duke@1 107 ostr.write_long(typeData.length);
duke@1 108 ostr.write_octet_array(typeData, 0, typeData.length);
duke@1 109 ostr.write_long(profileTags.length);
duke@1 110 for (int i = 0; i < profileTags.length; i++) {
duke@1 111 ostr.write_long(profileTags[i]);
duke@1 112 ostr.write_long(profileData[i].length);
duke@1 113 ostr.write_octet_array(profileData[i], 0, profileData[i].length);
duke@1 114 }
duke@1 115
duke@1 116 InputStream istr = ostr.create_input_stream() ;
duke@1 117
duke@1 118 // read the IOR back from the stream
duke@1 119 org.omg.CORBA.Object obj = (org.omg.CORBA.Object)istr.read_Object();
duke@1 120 return StubAdapter.getDelegate( obj ) ;
duke@1 121 }
duke@1 122
duke@1 123 public void doRead( java.io.ObjectInputStream stream )
duke@1 124 throws IOException, ClassNotFoundException
duke@1 125 {
duke@1 126 // read the IOR from the ObjectInputStream
duke@1 127 int typeLength = stream.readInt();
rpatil@1692 128 SharedSecrets.getJavaOISAccess().checkArray(stream, byte[].class, typeLength);
duke@1 129 typeData = new byte[typeLength];
duke@1 130 stream.readFully(typeData);
rpatil@1692 131
duke@1 132 int numProfiles = stream.readInt();
rpatil@1692 133 SharedSecrets.getJavaOISAccess().checkArray(stream, int[].class, numProfiles);
rpatil@1692 134 SharedSecrets.getJavaOISAccess().checkArray(stream, byte[].class, numProfiles);
duke@1 135 profileTags = new int[numProfiles];
duke@1 136 profileData = new byte[numProfiles][];
duke@1 137 for (int i = 0; i < numProfiles; i++) {
duke@1 138 profileTags[i] = stream.readInt();
rpatil@1692 139 int dataSize = stream.readInt();
rpatil@1692 140 SharedSecrets.getJavaOISAccess().checkArray(stream, byte[].class, dataSize);
rpatil@1692 141 profileData[i] = new byte[dataSize];
duke@1 142 stream.readFully(profileData[i]);
duke@1 143 }
duke@1 144 }
duke@1 145
duke@1 146 public void doWrite( ObjectOutputStream stream )
duke@1 147 throws IOException
duke@1 148 {
duke@1 149 // write the IOR to the ObjectOutputStream
duke@1 150 stream.writeInt(typeData.length);
duke@1 151 stream.write(typeData);
duke@1 152 stream.writeInt(profileTags.length);
duke@1 153 for (int i = 0; i < profileTags.length; i++) {
duke@1 154 stream.writeInt(profileTags[i]);
duke@1 155 stream.writeInt(profileData[i].length);
duke@1 156 stream.write(profileData[i]);
duke@1 157 }
duke@1 158 }
duke@1 159
duke@1 160 /**
duke@1 161 * Returns a hash code value for the object which is the same for all stubs
duke@1 162 * that represent the same remote object.
duke@1 163 * @return the hash code value.
duke@1 164 */
duke@1 165 public synchronized int hashCode()
duke@1 166 {
duke@1 167 if (hashCode == 0) {
duke@1 168
duke@1 169 // compute the hash code
duke@1 170 for (int i = 0; i < typeData.length; i++) {
duke@1 171 hashCode = hashCode * 37 + typeData[i];
duke@1 172 }
duke@1 173
duke@1 174 for (int i = 0; i < profileTags.length; i++) {
duke@1 175 hashCode = hashCode * 37 + profileTags[i];
duke@1 176 for (int j = 0; j < profileData[i].length; j++) {
duke@1 177 hashCode = hashCode * 37 + profileData[i][j];
duke@1 178 }
duke@1 179 }
duke@1 180 }
duke@1 181
duke@1 182 return hashCode;
duke@1 183 }
duke@1 184
duke@1 185 private boolean equalArrays( int[] data1, int[] data2 )
duke@1 186 {
duke@1 187 if (data1.length != data2.length)
duke@1 188 return false ;
duke@1 189
duke@1 190 for (int ctr=0; ctr<data1.length; ctr++) {
duke@1 191 if (data1[ctr] != data2[ctr])
duke@1 192 return false ;
duke@1 193 }
duke@1 194
duke@1 195 return true ;
duke@1 196 }
duke@1 197
duke@1 198 private boolean equalArrays( byte[] data1, byte[] data2 )
duke@1 199 {
duke@1 200 if (data1.length != data2.length)
duke@1 201 return false ;
duke@1 202
duke@1 203 for (int ctr=0; ctr<data1.length; ctr++) {
duke@1 204 if (data1[ctr] != data2[ctr])
duke@1 205 return false ;
duke@1 206 }
duke@1 207
duke@1 208 return true ;
duke@1 209 }
duke@1 210
duke@1 211 private boolean equalArrays( byte[][] data1, byte[][] data2 )
duke@1 212 {
duke@1 213 if (data1.length != data2.length)
duke@1 214 return false ;
duke@1 215
duke@1 216 for (int ctr=0; ctr<data1.length; ctr++) {
duke@1 217 if (!equalArrays( data1[ctr], data2[ctr] ))
duke@1 218 return false ;
duke@1 219 }
duke@1 220
duke@1 221 return true ;
duke@1 222 }
duke@1 223
duke@1 224 public boolean equals(java.lang.Object obj)
duke@1 225 {
duke@1 226 if (this == obj) {
duke@1 227 return true;
duke@1 228 }
duke@1 229
duke@1 230 if (!(obj instanceof StubIORImpl)) {
duke@1 231 return false;
duke@1 232 }
duke@1 233
duke@1 234 StubIORImpl other = (StubIORImpl) obj;
duke@1 235 if (other.hashCode() != this.hashCode()) {
duke@1 236 return false;
duke@1 237 }
duke@1 238
duke@1 239 return equalArrays( typeData, other.typeData ) &&
duke@1 240 equalArrays( profileTags, other.profileTags ) &&
duke@1 241 equalArrays( profileData, other.profileData ) ;
duke@1 242 }
duke@1 243
duke@1 244 private void appendByteArray( StringBuffer result, byte[] data )
duke@1 245 {
duke@1 246 for ( int ctr=0; ctr<data.length; ctr++ ) {
duke@1 247 result.append( Integer.toHexString( data[ctr] ) ) ;
duke@1 248 }
duke@1 249 }
duke@1 250
duke@1 251 /**
duke@1 252 * Returns a string representation of this stub. Returns the same string
duke@1 253 * for all stubs that represent the same remote object.
duke@1 254 * "SimpleIORImpl[<typeName>,[<profileID>]data, ...]"
duke@1 255 * @return a string representation of this stub.
duke@1 256 */
duke@1 257 public String toString()
duke@1 258 {
duke@1 259 StringBuffer result = new StringBuffer() ;
duke@1 260 result.append( "SimpleIORImpl[" ) ;
duke@1 261 String repositoryId = new String( typeData ) ;
duke@1 262 result.append( repositoryId ) ;
duke@1 263 for (int ctr=0; ctr<profileTags.length; ctr++) {
duke@1 264 result.append( ",(" ) ;
duke@1 265 result.append( profileTags[ctr] ) ;
duke@1 266 result.append( ")" ) ;
duke@1 267 appendByteArray( result, profileData[ctr] ) ;
duke@1 268 }
duke@1 269
duke@1 270 result.append( "]" ) ;
duke@1 271 return result.toString() ;
duke@1 272 }
duke@1 273 }

mercurial