KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > ior > StubIORImpl


1 /*
2  * @(#)StubIORImpl.java 1.3 04/07/27
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package com.sun.corba.se.impl.ior;
17
18 import java.io.ObjectInputStream JavaDoc ;
19 import java.io.ObjectOutputStream JavaDoc ;
20 import java.io.IOException JavaDoc ;
21 import java.io.StringWriter JavaDoc ;
22
23 import org.omg.CORBA.ORB JavaDoc ;
24
25 import org.omg.CORBA.portable.Delegate JavaDoc ;
26 import org.omg.CORBA.portable.InputStream JavaDoc ;
27 import org.omg.CORBA.portable.OutputStream JavaDoc ;
28
29 // Be very careful: com.sun.corba imports must not depend on
30
// PEORB internal classes in ways that prevent portability to
31
// other vendor's ORBs.
32
import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
33 import com.sun.corba.se.impl.orbutil.HexOutputStream ;
34
35 /**
36  * This class implements a very simply IOR representation
37  * which must be completely ORBImpl free so that this class
38  * can be used in the implementation of a portable StubDelegateImpl.
39  */

40 public class StubIORImpl
41 {
42     // cached hash code
43
private int hashCode;
44     
45     // IOR components
46
private byte[] typeData;
47     private int[] profileTags;
48     private byte[][] profileData;
49
50     public StubIORImpl()
51     {
52     hashCode = 0 ;
53     typeData = null ;
54     profileTags = null ;
55     profileData = null ;
56     }
57
58     public String JavaDoc getRepositoryId()
59     {
60     if (typeData == null)
61         return null ;
62
63     return new String JavaDoc( typeData ) ;
64     }
65
66     public StubIORImpl( org.omg.CORBA.Object JavaDoc obj )
67     {
68     // write the IOR to an OutputStream and get an InputStream
69
OutputStream JavaDoc ostr = StubAdapter.getORB( obj ).create_output_stream();
70     ostr.write_Object(obj);
71     InputStream JavaDoc istr = ostr.create_input_stream();
72
73     // read the IOR components back from the stream
74
int typeLength = istr.read_long();
75     typeData = new byte[typeLength];
76     istr.read_octet_array(typeData, 0, typeLength);
77     int numProfiles = istr.read_long();
78     profileTags = new int[numProfiles];
79     profileData = new byte[numProfiles][];
80     for (int i = 0; i < numProfiles; i++) {
81         profileTags[i] = istr.read_long();
82         profileData[i] = new byte[istr.read_long()];
83         istr.read_octet_array(profileData[i], 0, profileData[i].length);
84     }
85     }
86    
87     public Delegate JavaDoc getDelegate( ORB JavaDoc orb )
88     {
89     // write the IOR components to an org.omg.CORBA.portable.OutputStream
90
OutputStream JavaDoc ostr = orb.create_output_stream();
91     ostr.write_long(typeData.length);
92     ostr.write_octet_array(typeData, 0, typeData.length);
93     ostr.write_long(profileTags.length);
94     for (int i = 0; i < profileTags.length; i++) {
95         ostr.write_long(profileTags[i]);
96         ostr.write_long(profileData[i].length);
97         ostr.write_octet_array(profileData[i], 0, profileData[i].length);
98     }
99
100     InputStream JavaDoc istr = ostr.create_input_stream() ;
101
102     // read the IOR back from the stream
103
org.omg.CORBA.Object JavaDoc obj = (org.omg.CORBA.Object JavaDoc)istr.read_Object();
104     return StubAdapter.getDelegate( obj ) ;
105     }
106
107     public void doRead( java.io.ObjectInputStream JavaDoc stream )
108         throws IOException JavaDoc, ClassNotFoundException JavaDoc
109     {
110     // read the IOR from the ObjectInputStream
111
int typeLength = stream.readInt();
112     typeData = new byte[typeLength];
113     stream.readFully(typeData);
114     int numProfiles = stream.readInt();
115     profileTags = new int[numProfiles];
116     profileData = new byte[numProfiles][];
117     for (int i = 0; i < numProfiles; i++) {
118         profileTags[i] = stream.readInt();
119         profileData[i] = new byte[stream.readInt()];
120         stream.readFully(profileData[i]);
121         }
122     }
123
124     public void doWrite( ObjectOutputStream JavaDoc stream )
125         throws IOException JavaDoc
126     {
127     // write the IOR to the ObjectOutputStream
128
stream.writeInt(typeData.length);
129     stream.write(typeData);
130     stream.writeInt(profileTags.length);
131     for (int i = 0; i < profileTags.length; i++) {
132         stream.writeInt(profileTags[i]);
133         stream.writeInt(profileData[i].length);
134         stream.write(profileData[i]);
135         }
136     }
137
138     /**
139      * Returns a hash code value for the object which is the same for all stubs
140      * that represent the same remote object.
141      * @return the hash code value.
142      */

143     public synchronized int hashCode()
144     {
145     if (hashCode == 0) {
146
147         // compute the hash code
148
for (int i = 0; i < typeData.length; i++) {
149         hashCode = hashCode * 37 + typeData[i];
150         }
151
152         for (int i = 0; i < profileTags.length; i++) {
153         hashCode = hashCode * 37 + profileTags[i];
154         for (int j = 0; j < profileData[i].length; j++) {
155             hashCode = hashCode * 37 + profileData[i][j];
156         }
157             }
158     }
159
160         return hashCode;
161     }
162
163     private boolean equalArrays( int[] data1, int[] data2 )
164     {
165     if (data1.length != data2.length)
166         return false ;
167
168     for (int ctr=0; ctr<data1.length; ctr++) {
169         if (data1[ctr] != data2[ctr])
170         return false ;
171     }
172
173     return true ;
174     }
175
176     private boolean equalArrays( byte[] data1, byte[] data2 )
177     {
178     if (data1.length != data2.length)
179         return false ;
180
181     for (int ctr=0; ctr<data1.length; ctr++) {
182         if (data1[ctr] != data2[ctr])
183         return false ;
184     }
185
186     return true ;
187     }
188
189     private boolean equalArrays( byte[][] data1, byte[][] data2 )
190     {
191     if (data1.length != data2.length)
192         return false ;
193
194     for (int ctr=0; ctr<data1.length; ctr++) {
195         if (!equalArrays( data1[ctr], data2[ctr] ))
196         return false ;
197     }
198
199     return true ;
200     }
201
202     public boolean equals(java.lang.Object JavaDoc obj)
203     {
204         if (this == obj) {
205             return true;
206         }
207         
208         if (!(obj instanceof StubIORImpl)) {
209             return false;
210         }
211         
212         StubIORImpl other = (StubIORImpl) obj;
213         if (other.hashCode() != this.hashCode()) {
214             return false;
215         }
216
217     return equalArrays( typeData, other.typeData ) &&
218         equalArrays( profileTags, other.profileTags ) &&
219         equalArrays( profileData, other.profileData ) ;
220     }
221
222     private void appendByteArray( StringBuffer JavaDoc result, byte[] data )
223     {
224     for ( int ctr=0; ctr<data.length; ctr++ ) {
225         result.append( Integer.toHexString( data[ctr] ) ) ;
226     }
227     }
228
229     /**
230      * Returns a string representation of this stub. Returns the same string
231      * for all stubs that represent the same remote object.
232      * "SimpleIORImpl[<typeName>,[<profileID>]data, ...]"
233      * @return a string representation of this stub.
234      */

235     public String JavaDoc toString()
236     {
237     StringBuffer JavaDoc result = new StringBuffer JavaDoc() ;
238     result.append( "SimpleIORImpl[" ) ;
239     String JavaDoc repositoryId = new String JavaDoc( typeData ) ;
240     result.append( repositoryId ) ;
241     for (int ctr=0; ctr<profileTags.length; ctr++) {
242         result.append( ",(" ) ;
243         result.append( profileTags[ctr] ) ;
244         result.append( ")" ) ;
245         appendByteArray( result, profileData[ctr] ) ;
246     }
247
248     result.append( "]" ) ;
249     return result.toString() ;
250     }
251 }
252
Popular Tags