KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > ior > iiop > IIOPProfileImpl


1 /*
2  * @(#)IIOPProfileImpl.java 1.32 04/06/21
3  *
4  * Copyright 2004 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.iiop;
9
10 import java.util.List JavaDoc ;
11 import java.util.Iterator JavaDoc ;
12
13 import org.omg.CORBA.SystemException JavaDoc ;
14
15 import org.omg.CORBA_2_3.portable.OutputStream JavaDoc ;
16 import org.omg.CORBA_2_3.portable.InputStream JavaDoc ;
17
18 import org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS JavaDoc ;
19 import org.omg.IOP.TAG_INTERNET_IOP JavaDoc;
20 import org.omg.IOP.TAG_JAVA_CODEBASE JavaDoc;
21
22 import com.sun.corba.se.spi.protocol.RequestDispatcherRegistry ;
23
24 import com.sun.corba.se.spi.oa.ObjectAdapter ;
25 import com.sun.corba.se.spi.oa.ObjectAdapterFactory ;
26
27 import com.sun.corba.se.spi.ior.ObjectId ;
28 import com.sun.corba.se.spi.ior.ObjectAdapterId ;
29 import com.sun.corba.se.spi.ior.TaggedProfile ;
30 import com.sun.corba.se.spi.ior.TaggedProfileTemplate ;
31 import com.sun.corba.se.spi.ior.ObjectKey ;
32 import com.sun.corba.se.spi.ior.ObjectKeyTemplate ;
33 import com.sun.corba.se.spi.ior.TaggedComponent ;
34 import com.sun.corba.se.spi.ior.IdentifiableBase ;
35 import com.sun.corba.se.spi.ior.IORFactories ;
36 import com.sun.corba.se.spi.ior.ObjectKeyFactory ;
37
38 import com.sun.corba.se.spi.ior.iiop.IIOPAddress ;
39 import com.sun.corba.se.spi.ior.iiop.IIOPProfile ;
40 import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;
41 import com.sun.corba.se.spi.ior.iiop.IIOPFactories ;
42 import com.sun.corba.se.spi.ior.iiop.GIOPVersion ;
43 import com.sun.corba.se.spi.ior.iiop.JavaCodebaseComponent ;
44
45 import com.sun.corba.se.spi.orb.ORB ;
46 import com.sun.corba.se.spi.orb.ORBVersion ;
47
48 import com.sun.corba.se.spi.logging.CORBALogDomains ;
49
50 import com.sun.corba.se.impl.ior.EncapsulationUtility ;
51
52 import com.sun.corba.se.impl.encoding.EncapsInputStream ;
53 import com.sun.corba.se.impl.encoding.EncapsOutputStream ;
54
55 import com.sun.corba.se.impl.util.JDKBridge;
56
57 import com.sun.corba.se.impl.logging.IORSystemException;
58
59 /**
60  * @author
61  */

62 public class IIOPProfileImpl extends IdentifiableBase implements IIOPProfile
63 {
64     private ORB orb ;
65     private IORSystemException wrapper ;
66     private ObjectId oid;
67     private IIOPProfileTemplate proftemp;
68     private ObjectKeyTemplate oktemp ;
69
70     // Cached lookups
71
protected String JavaDoc codebase = null ;
72     protected boolean cachedCodebase = false;
73
74     private boolean checkedIsLocal = false ;
75     private boolean cachedIsLocal = false ;
76
77     // initialize-on-demand holder
78
private static class LocalCodeBaseSingletonHolder {
79     public static JavaCodebaseComponent comp ;
80
81     static {
82         String JavaDoc localCodebase = JDKBridge.getLocalCodebase() ;
83         if (localCodebase == null)
84         comp = null ;
85         else
86         comp = IIOPFactories.makeJavaCodebaseComponent(
87             localCodebase ) ;
88     }
89     }
90
91     private GIOPVersion giopVersion = null;
92
93     public boolean equals( Object JavaDoc obj )
94     {
95     if (!(obj instanceof IIOPProfileImpl))
96         return false ;
97
98     IIOPProfileImpl other = (IIOPProfileImpl)obj ;
99
100     return oid.equals( other.oid ) && proftemp.equals( other.proftemp ) &&
101         oktemp.equals( other.oktemp ) ;
102     }
103
104     public int hashCode()
105     {
106     return oid.hashCode() ^ proftemp.hashCode() ^ oktemp.hashCode() ;
107     }
108
109     public ObjectId getObjectId()
110     {
111     return oid ;
112     }
113
114     public TaggedProfileTemplate getTaggedProfileTemplate()
115     {
116     return proftemp ;
117     }
118
119     public ObjectKeyTemplate getObjectKeyTemplate()
120     {
121     return oktemp ;
122     }
123
124     private IIOPProfileImpl( ORB orb )
125     {
126     this.orb = orb ;
127     wrapper = IORSystemException.get( orb,
128         CORBALogDomains.OA_IOR ) ;
129     }
130
131     public IIOPProfileImpl( ORB orb, ObjectKeyTemplate oktemp, ObjectId oid,
132     IIOPProfileTemplate proftemp )
133     {
134     this( orb ) ;
135     this.oktemp = oktemp ;
136     this.oid = oid ;
137     this.proftemp = proftemp ;
138     }
139
140     public IIOPProfileImpl( InputStream JavaDoc is )
141     {
142     this( (ORB)(is.orb()) ) ;
143     init( is ) ;
144     }
145
146     public IIOPProfileImpl( ORB orb, org.omg.IOP.TaggedProfile JavaDoc profile)
147     {
148     this( orb ) ;
149
150         if (profile == null || profile.tag != TAG_INTERNET_IOP.value ||
151         profile.profile_data == null) {
152         throw wrapper.invalidTaggedProfile() ;
153         }
154
155         EncapsInputStream istr = new EncapsInputStream((ORB)orb, profile.profile_data,
156         profile.profile_data.length);
157     istr.consumeEndian();
158     init( istr ) ;
159     }
160
161     private void init( InputStream JavaDoc istr )
162     {
163     // First, read all of the IIOP IOR data
164
GIOPVersion version = new GIOPVersion() ;
165     version.read( istr ) ;
166     IIOPAddress primary = new IIOPAddressImpl( istr ) ;
167     byte[] key = EncapsulationUtility.readOctets( istr ) ;
168
169     ObjectKey okey = orb.getObjectKeyFactory().create( key ) ;
170     oktemp = okey.getTemplate() ;
171     oid = okey.getId() ;
172
173     proftemp = IIOPFactories.makeIIOPProfileTemplate( orb,
174         version, primary ) ;
175
176     // Handle any tagged components (if applicable)
177
if (version.getMinor() > 0)
178         EncapsulationUtility.readIdentifiableSequence( proftemp,
179         orb.getTaggedComponentFactoryFinder(), istr ) ;
180
181     // If there is no codebase in this IOR and there IS a
182
// java.rmi.server.codebase property set, we need to
183
// update the IOR with the local codebase. Note that
184
// there is only one instance of the local codebase, but it
185
// can be safely shared in multiple IORs since it is immutable.
186
if (uncachedGetCodeBase() == null) {
187         JavaCodebaseComponent jcc = LocalCodeBaseSingletonHolder.comp ;
188
189         if (jcc != null) {
190         if (version.getMinor() > 0)
191             proftemp.add( jcc ) ;
192
193         codebase = jcc.getURLs() ;
194         }
195
196         // Whether codebase is null or not, we have it,
197
// and so getCodebase ned never call uncachedGetCodebase.
198
cachedCodebase = true;
199     }
200     }
201
202     public void writeContents(OutputStream JavaDoc os)
203     {
204     proftemp.write( oktemp, oid, os ) ;
205     }
206
207     public int getId()
208     {
209     return proftemp.getId() ;
210     }
211
212     public boolean isEquivalent( TaggedProfile prof )
213     {
214     if (!(prof instanceof IIOPProfile))
215         return false ;
216
217     IIOPProfile other = (IIOPProfile)prof ;
218
219     return oid.equals( other.getObjectId() ) &&
220            proftemp.isEquivalent( other.getTaggedProfileTemplate() ) &&
221            oktemp.equals( other.getObjectKeyTemplate() ) ;
222     }
223
224     public ObjectKey getObjectKey()
225     {
226     ObjectKey result = IORFactories.makeObjectKey( oktemp, oid ) ;
227     return result ;
228     }
229
230     public org.omg.IOP.TaggedProfile JavaDoc getIOPProfile()
231     {
232     EncapsOutputStream os = new EncapsOutputStream( orb ) ;
233     os.write_long( getId() ) ;
234     write( os ) ;
235     InputStream JavaDoc is = (InputStream JavaDoc)(os.create_input_stream()) ;
236     return org.omg.IOP.TaggedProfileHelper.read( is ) ;
237     }
238
239     private String JavaDoc uncachedGetCodeBase() {
240     Iterator JavaDoc iter = proftemp.iteratorById( TAG_JAVA_CODEBASE.value ) ;
241
242     if (iter.hasNext()) {
243         JavaCodebaseComponent jcbc = (JavaCodebaseComponent)(iter.next()) ;
244         return jcbc.getURLs() ;
245     }
246
247     return null ;
248     }
249
250     public synchronized String JavaDoc getCodebase() {
251     if (!cachedCodebase) {
252         cachedCodebase = true ;
253         codebase = uncachedGetCodeBase() ;
254     }
255
256     return codebase ;
257     }
258
259     /**
260      * @return the ORBVersion associated with the object key in the IOR.
261      */

262     public ORBVersion getORBVersion() {
263         return oktemp.getORBVersion();
264     }
265
266     public synchronized boolean isLocal()
267     {
268     if (!checkedIsLocal) {
269         checkedIsLocal = true ;
270         String JavaDoc host = proftemp.getPrimaryAddress().getHost() ;
271
272         cachedIsLocal = orb.isLocalHost(host) &&
273         orb.isLocalServerId(oktemp.getSubcontractId(),
274                        oktemp.getServerId()) &&
275         orb.getLegacyServerSocketManager()
276             .legacyIsLocalServerPort(
277                         proftemp.getPrimaryAddress().getPort());
278         }
279
280     return cachedIsLocal ;
281     }
282
283     /** Return the servant for this IOR, if it is local AND if the OA that
284      * implements this objref supports direct access to servants outside of an
285      * invocation.
286      * XXX revisit: do we want this at all? If we do, it might move to the
287      * ObjectKeyTemplate instead.
288      */

289     public java.lang.Object JavaDoc getServant()
290     {
291     if (!isLocal())
292         return null ;
293
294     RequestDispatcherRegistry scr = orb.getRequestDispatcherRegistry() ;
295     ObjectAdapterFactory oaf = scr.getObjectAdapterFactory(
296         oktemp.getSubcontractId() ) ;
297
298     ObjectAdapterId oaid = oktemp.getObjectAdapterId() ;
299     ObjectAdapter oa = null ;
300
301     try {
302         oa = oaf.find( oaid ) ;
303     } catch (SystemException JavaDoc exc) {
304         // Could not find the OA, so just return null.
305
// This usually happens when POAs are being deleted,
306
// and the POA always return null for getLocalServant anyway.
307
wrapper.getLocalServantFailure( exc, oaid.toString() ) ;
308         return null ;
309     }
310
311     byte[] boid = oid.getId() ;
312     java.lang.Object JavaDoc servant = oa.getLocalServant( boid ) ;
313     return servant ;
314     }
315
316     /**
317      * Return GIOPVersion for this IOR.
318      * Requests created against this IOR will be of the
319      * return Version.
320      */

321     public synchronized GIOPVersion getGIOPVersion()
322     {
323     return proftemp.getGIOPVersion() ;
324     }
325
326     public void makeImmutable()
327     {
328     proftemp.makeImmutable() ;
329     }
330 }
331
Popular Tags