KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)IORImpl.java 1.32 05/05/27
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.ior;
9
10 import java.util.ListIterator JavaDoc ;
11 import java.util.Iterator JavaDoc ;
12 import java.util.Map JavaDoc ;
13 import java.util.HashMap JavaDoc ;
14
15 import java.io.StringWriter JavaDoc;
16 import java.io.IOException JavaDoc;
17
18 import javax.rmi.CORBA.Util JavaDoc;
19
20 import org.omg.CORBA_2_3.portable.InputStream JavaDoc ;
21 import org.omg.CORBA_2_3.portable.OutputStream JavaDoc ;
22
23 import org.omg.IOP.TAG_INTERNET_IOP JavaDoc ;
24
25 import com.sun.corba.se.spi.ior.ObjectId ;
26 import com.sun.corba.se.spi.ior.TaggedProfileTemplate ;
27 import com.sun.corba.se.spi.ior.TaggedProfile ;
28 import com.sun.corba.se.spi.ior.IOR ;
29 import com.sun.corba.se.spi.ior.IORTemplate ;
30 import com.sun.corba.se.spi.ior.IORTemplateList ;
31 import com.sun.corba.se.spi.ior.IdentifiableFactoryFinder ;
32 import com.sun.corba.se.spi.ior.IdentifiableContainerBase ;
33 import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
34 import com.sun.corba.se.spi.ior.IORFactories ;
35
36 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
37
38 import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry;
39
40 import com.sun.corba.se.spi.orb.ORB;
41
42 import com.sun.corba.se.spi.logging.CORBALogDomains;
43
44 import com.sun.corba.se.impl.encoding.MarshalOutputStream;
45
46 import com.sun.corba.se.impl.encoding.EncapsOutputStream;
47
48 import com.sun.corba.se.impl.orbutil.HexOutputStream;
49 import com.sun.corba.se.impl.orbutil.ORBConstants;
50
51 import com.sun.corba.se.impl.logging.IORSystemException ;
52
53 // XXX remove this once getProfile is gone
54
import com.sun.corba.se.spi.ior.iiop.IIOPProfile ;
55
56 /** An IOR is represented as a list of profiles.
57 * Only objects that extend TaggedProfile should be added to an IOR.
58 * However, enforcing this restriction requires overriding all
59 * of the addXXX methods inherited from List, so no check
60 * is included here.
61 * @author Ken Cavanaugh
62 */

63 public class IORImpl extends IdentifiableContainerBase implements IOR
64 {
65     private String JavaDoc typeId;
66     private ORB factory = null ;
67     private boolean isCachedHashValue = false;
68     private int cachedHashValue;
69     IORSystemException wrapper ;
70
71     public ORB getORB()
72     {
73     return factory ;
74     }
75
76     /* This variable is set directly from the constructors that take
77      * an IORTemplate or IORTemplateList as arguments; otherwise it
78      * is derived from the list of TaggedProfile instances on the first
79      * call to getIORTemplates. Note that we assume that an IOR with
80      * mutiple TaggedProfile instances has the same ObjectId in each
81      * TaggedProfile, as otherwise the IOR could never be created through
82      * an ObjectReferenceFactory.
83      */

84     private IORTemplateList iortemps = null ;
85
86     public boolean equals( Object JavaDoc obj )
87     {
88     if (obj == null)
89         return false ;
90
91     if (!(obj instanceof IOR))
92         return false ;
93
94     IOR other = (IOR)obj ;
95
96     return super.equals( obj ) && typeId.equals( other.getTypeId() ) ;
97     }
98
99     public synchronized int hashCode()
100     {
101         if (! isCachedHashValue) {
102         cachedHashValue = (super.hashCode() ^ typeId.hashCode());
103         isCachedHashValue = true;
104     }
105     return cachedHashValue;
106     }
107
108     /** Construct an empty IOR. This is needed for null object references.
109     */

110     public IORImpl( ORB orb )
111     {
112     this( orb, "" ) ;
113     }
114
115     public IORImpl( ORB orb, String JavaDoc typeid )
116     {
117     factory = orb ;
118     wrapper = IORSystemException.get( orb,
119         CORBALogDomains.OA_IOR ) ;
120     this.typeId = typeid ;
121     }
122
123     /** Construct an IOR from an IORTemplate by applying the same
124     * object id to each TaggedProfileTemplate in the IORTemplate.
125     */

126     public IORImpl( ORB orb, String JavaDoc typeId, IORTemplate iortemp, ObjectId id)
127     {
128     this( orb, typeId ) ;
129
130     this.iortemps = IORFactories.makeIORTemplateList() ;
131     this.iortemps.add( iortemp ) ;
132     
133     addTaggedProfiles( iortemp, id ) ;
134     
135     makeImmutable() ;
136     }
137     
138     private void addTaggedProfiles( IORTemplate iortemp, ObjectId id )
139     {
140     ObjectKeyTemplate oktemp = iortemp.getObjectKeyTemplate() ;
141     Iterator JavaDoc templateIterator = iortemp.iterator() ;
142
143     while (templateIterator.hasNext()) {
144         TaggedProfileTemplate ptemp =
145         (TaggedProfileTemplate)(templateIterator.next()) ;
146
147         TaggedProfile profile = ptemp.create( oktemp, id ) ;
148
149         add( profile ) ;
150     }
151     }
152
153     /** Construct an IOR from an IORTemplate by applying the same
154     * object id to each TaggedProfileTemplate in the IORTemplate.
155     */

156     public IORImpl( ORB orb, String JavaDoc typeId, IORTemplateList iortemps, ObjectId id)
157     {
158     this( orb, typeId ) ;
159
160     this.iortemps = iortemps ;
161
162     Iterator JavaDoc iter = iortemps.iterator() ;
163     while (iter.hasNext()) {
164         IORTemplate iortemp = (IORTemplate)(iter.next()) ;
165         addTaggedProfiles( iortemp, id ) ;
166     }
167     
168     makeImmutable() ;
169     }
170     
171     public IORImpl(InputStream JavaDoc is)
172     {
173     this( (ORB)(is.orb()), is.read_string() ) ;
174
175         IdentifiableFactoryFinder finder =
176         factory.getTaggedProfileFactoryFinder() ;
177
178     EncapsulationUtility.readIdentifiableSequence( this, finder, is ) ;
179
180     makeImmutable() ;
181     }
182     
183     public String JavaDoc getTypeId()
184     {
185     return typeId ;
186     }
187     
188     public void write(OutputStream JavaDoc os)
189     {
190     os.write_string( typeId ) ;
191     EncapsulationUtility.writeIdentifiableSequence( this, os ) ;
192     }
193
194     public String JavaDoc stringify()
195     {
196         StringWriter JavaDoc bs;
197
198         MarshalOutputStream s = new EncapsOutputStream(factory);
199         s.putEndian();
200         write( (OutputStream JavaDoc)s );
201         bs = new StringWriter JavaDoc();
202         try {
203             s.writeTo(new HexOutputStream(bs));
204         } catch (IOException JavaDoc ex) {
205         throw wrapper.stringifyWriteError( ex ) ;
206         }
207
208         return ORBConstants.STRINGIFY_PREFIX + bs;
209     }
210
211     public synchronized void makeImmutable()
212     {
213     makeElementsImmutable() ;
214
215     if (iortemps != null)
216         iortemps.makeImmutable() ;
217
218     super.makeImmutable() ;
219     }
220     
221     public org.omg.IOP.IOR JavaDoc getIOPIOR() {
222     EncapsOutputStream os = new EncapsOutputStream(factory);
223     write(os);
224     InputStream JavaDoc is = (InputStream JavaDoc) (os.create_input_stream());
225     return org.omg.IOP.IORHelper.read(is);
226     }
227
228     public boolean isNil()
229     {
230         //
231
// The check for typeId length of 0 below is commented out
232
// as a workaround for a bug in ORBs which send a
233
// null objref with a non-empty typeId string.
234
//
235
return ((size() == 0) /* && (typeId.length() == 0) */);
236     }
237
238     public boolean isEquivalent(IOR ior)
239     {
240     Iterator JavaDoc myIterator = iterator() ;
241     Iterator JavaDoc otherIterator = ior.iterator() ;
242     while (myIterator.hasNext() && otherIterator.hasNext()) {
243         TaggedProfile myProfile = (TaggedProfile)(myIterator.next()) ;
244         TaggedProfile otherProfile = (TaggedProfile)(otherIterator.next()) ;
245         if (!myProfile.isEquivalent( otherProfile ))
246         return false ;
247     }
248
249     return myIterator.hasNext() == otherIterator.hasNext() ;
250     }
251
252     private void initializeIORTemplateList()
253     {
254     // Maps ObjectKeyTemplate to IORTemplate
255
Map JavaDoc oktempToIORTemplate = new HashMap JavaDoc() ;
256
257     iortemps = IORFactories.makeIORTemplateList() ;
258     Iterator JavaDoc iter = iterator() ;
259     ObjectId oid = null ; // used to check that all profiles have the same oid.
260
while (iter.hasNext()) {
261         TaggedProfile prof = (TaggedProfile)(iter.next()) ;
262         TaggedProfileTemplate ptemp = prof.getTaggedProfileTemplate() ;
263         ObjectKeyTemplate oktemp = prof.getObjectKeyTemplate() ;
264
265         // Check that all oids for all profiles are the same: if they are not,
266
// throw exception.
267
if (oid == null)
268         oid = prof.getObjectId() ;
269         else if (!oid.equals( prof.getObjectId() ))
270         throw wrapper.badOidInIorTemplateList() ;
271
272         // Find or create the IORTemplate for oktemp.
273
IORTemplate iortemp = (IORTemplate)(oktempToIORTemplate.get( oktemp )) ;
274         if (iortemp == null) {
275         iortemp = IORFactories.makeIORTemplate( oktemp ) ;
276         oktempToIORTemplate.put( oktemp, iortemp ) ;
277         iortemps.add( iortemp ) ;
278         }
279
280         iortemp.add( ptemp ) ;
281     }
282
283     iortemps.makeImmutable() ;
284     }
285
286     /** Return the IORTemplateList for this IOR. Will throw
287      * exception if it is not possible to generate an IOR
288      * from the IORTemplateList that is equal to this IOR,
289      * which can only happen if not every TaggedProfile in the
290      * IOR has the same ObjectId.
291      */

292     public synchronized IORTemplateList getIORTemplates()
293     {
294     if (iortemps == null)
295         initializeIORTemplateList() ;
296
297     return iortemps ;
298     }
299
300     /** Return the first IIOPProfile in this IOR.
301      * XXX THIS IS TEMPORARY FOR BACKWARDS COMPATIBILITY AND WILL BE REMOVED
302      * SOON!
303      */

304     public IIOPProfile getProfile()
305     {
306     IIOPProfile iop = null ;
307     Iterator JavaDoc iter = iteratorById( TAG_INTERNET_IOP.value ) ;
308     if (iter.hasNext())
309         iop = (IIOPProfile)(iter.next()) ;
310  
311         if (iop != null)
312             return iop ;
313  
314         // if we come to this point then no IIOP Profile
315
// is present. Therefore, throw an exception.
316
throw wrapper.iorMustHaveIiopProfile() ;
317     }
318 }
319
Popular Tags