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

Wed, 27 Apr 2016 01:21:28 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:21:28 +0800
changeset 0
7ef37b2cdcad
child 748
6845b95cba6b
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/corba/
changeset: 765:f46df0af2ca8
tag: jdk8u25-b17

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

mercurial