KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > protocol > CorbaClientDelegateImpl


1 /*
2  * @(#)CorbaClientDelegateImpl.java 1.56 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.protocol;
9
10 import java.util.Iterator JavaDoc;
11 import java.util.HashMap JavaDoc;
12
13 import javax.rmi.CORBA.Tie JavaDoc;
14
15 import org.omg.CORBA.CompletionStatus JavaDoc;
16 import org.omg.CORBA.Context JavaDoc;
17 import org.omg.CORBA.ContextList JavaDoc;
18 import org.omg.CORBA.ExceptionList JavaDoc;
19 import org.omg.CORBA.NamedValue JavaDoc;
20 import org.omg.CORBA.NVList JavaDoc;
21 import org.omg.CORBA.Request JavaDoc;
22 import org.omg.CORBA.TypeCode JavaDoc;
23
24 import org.omg.CORBA.portable.ApplicationException JavaDoc;
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 import org.omg.CORBA.portable.RemarshalException JavaDoc;
29 import org.omg.CORBA.portable.ServantObject JavaDoc;
30
31 import com.sun.corba.se.pept.broker.Broker;
32 import com.sun.corba.se.pept.encoding.InputObject;
33 import com.sun.corba.se.pept.encoding.OutputObject;
34 import com.sun.corba.se.pept.protocol.ClientInvocationInfo;
35 import com.sun.corba.se.pept.protocol.ClientRequestDispatcher;
36 import com.sun.corba.se.pept.transport.ContactInfo;
37 import com.sun.corba.se.pept.transport.ContactInfoList;
38 import com.sun.corba.se.pept.transport.ContactInfoListIterator;
39
40 import com.sun.corba.se.spi.presentation.rmi.StubAdapter;
41 import com.sun.corba.se.spi.ior.IOR;
42 import com.sun.corba.se.spi.logging.CORBALogDomains;
43 import com.sun.corba.se.spi.orb.ORB;
44 import com.sun.corba.se.spi.protocol.CorbaClientDelegate ;
45 import com.sun.corba.se.spi.transport.CorbaContactInfo;
46 import com.sun.corba.se.spi.transport.CorbaContactInfoList;
47 import com.sun.corba.se.spi.transport.CorbaContactInfoListIterator;
48
49 import com.sun.corba.se.impl.corba.RequestImpl;
50 import com.sun.corba.se.impl.protocol.CorbaInvocationInfo;
51 import com.sun.corba.se.impl.transport.CorbaContactInfoListImpl;
52 import com.sun.corba.se.impl.util.JDKBridge;
53 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
54
55 // implements com.sun.corba.se.impl.core.ClientRequestDispatcher
56
// so RMI-IIOP Util.isLocal can call ClientRequestDispatcher.useLocalInvocation.
57

58 /**
59  * @author Harold Carr
60  */

61 public class CorbaClientDelegateImpl extends CorbaClientDelegate
62 {
63     private ORB orb;
64     private ORBUtilSystemException wrapper ;
65
66     private CorbaContactInfoList contactInfoList;
67
68     public CorbaClientDelegateImpl(ORB orb,
69                    CorbaContactInfoList contactInfoList)
70     {
71     this.orb = orb;
72     this.wrapper = ORBUtilSystemException.get( orb,
73         CORBALogDomains.RPC_PROTOCOL ) ;
74     this.contactInfoList = contactInfoList;
75     }
76     
77     //
78
// framework.subcontract.Delegate
79
//
80

81     public Broker getBroker()
82     {
83     return orb;
84     }
85
86     public ContactInfoList getContactInfoList()
87     {
88     return contactInfoList;
89     }
90
91     //
92
// CORBA_2_3.portable.Delegate
93
//
94

95     public OutputStream JavaDoc request(org.omg.CORBA.Object JavaDoc self,
96                 String JavaDoc operation,
97                 boolean responseExpected)
98     {
99     ClientInvocationInfo invocationInfo =
100         orb.createOrIncrementInvocationInfo();
101     Iterator JavaDoc contactInfoListIterator =
102         invocationInfo.getContactInfoListIterator();
103     if (contactInfoListIterator == null) {
104         contactInfoListIterator = contactInfoList.iterator();
105         invocationInfo.setContactInfoListIterator(contactInfoListIterator);
106     }
107     if (! contactInfoListIterator.hasNext()) {
108         throw ((CorbaContactInfoListIterator)contactInfoListIterator)
109         .getFailureException();
110     }
111     CorbaContactInfo contactInfo = (CorbaContactInfo) contactInfoListIterator.next();
112     ClientRequestDispatcher subcontract = contactInfo.getClientRequestDispatcher();
113     // Remember chosen subcontract for invoke and releaseReply.
114
// NOTE: This is necessary since a stream is not available in
115
// releaseReply if there is a client marshaling error or an
116
// error in _invoke.
117
invocationInfo.setClientRequestDispatcher(subcontract);
118     return (OutputStream JavaDoc)
119         subcontract.beginRequest(self, operation,
120                      !responseExpected, contactInfo);
121     }
122     
123     public InputStream JavaDoc invoke(org.omg.CORBA.Object JavaDoc self, OutputStream JavaDoc output)
124     throws
125         ApplicationException JavaDoc,
126         RemarshalException JavaDoc
127     {
128     ClientRequestDispatcher subcontract = getClientRequestDispatcher();
129     return (InputStream JavaDoc)
130         subcontract.marshalingComplete((Object JavaDoc)self, (OutputObject)output);
131     }
132     
133     public void releaseReply(org.omg.CORBA.Object JavaDoc self, InputStream JavaDoc input)
134     {
135     // NOTE: InputStream may be null (e.g., exception request from PI).
136
ClientRequestDispatcher subcontract = getClientRequestDispatcher();
137         subcontract.endRequest(orb, self, (InputObject)input);
138     orb.releaseOrDecrementInvocationInfo();
139     }
140
141     private ClientRequestDispatcher getClientRequestDispatcher()
142     {
143         return (ClientRequestDispatcher)
144             ((CorbaInvocationInfo)orb.getInvocationInfo())
145         .getClientRequestDispatcher();
146     }
147
148     public org.omg.CORBA.Object JavaDoc get_interface_def(org.omg.CORBA.Object JavaDoc obj)
149     {
150     InputStream JavaDoc is = null;
151     // instantiate the stub
152
org.omg.CORBA.Object JavaDoc stub = null ;
153
154         try {
155         OutputStream JavaDoc os = request(null, "_interface", true);
156         is = (InputStream JavaDoc) invoke((org.omg.CORBA.Object JavaDoc)null, os);
157
158         org.omg.CORBA.Object JavaDoc objimpl =
159         (org.omg.CORBA.Object JavaDoc) is.read_Object();
160
161         // check if returned object is of correct type
162
if ( !objimpl._is_a("IDL:omg.org/CORBA/InterfaceDef:1.0") )
163         throw wrapper.wrongInterfaceDef(CompletionStatus.COMPLETED_MAYBE);
164
165         try {
166                 stub = (org.omg.CORBA.Object JavaDoc)
167                     JDKBridge.loadClass("org.omg.CORBA._InterfaceDefStub").
168                 newInstance();
169         } catch (Exception JavaDoc ex) {
170         throw wrapper.noInterfaceDefStub( ex ) ;
171         }
172
173         org.omg.CORBA.portable.Delegate JavaDoc del =
174         StubAdapter.getDelegate( objimpl ) ;
175         StubAdapter.setDelegate( stub, del ) ;
176     } catch (ApplicationException JavaDoc e) {
177         // This cannot happen.
178
throw wrapper.applicationExceptionInSpecialMethod( e ) ;
179     } catch (RemarshalException JavaDoc e) {
180         return get_interface_def(obj);
181     } finally {
182         releaseReply((org.omg.CORBA.Object JavaDoc)null, (InputStream JavaDoc)is);
183         }
184
185     return stub;
186     }
187
188     public boolean is_a(org.omg.CORBA.Object JavaDoc obj, String JavaDoc dest)
189     {
190         // dest is the typeId of the interface to compare against.
191
// repositoryIds is the list of typeIds that the stub knows about.
192

193         // First we look for an answer using local information.
194

195         String JavaDoc [] repositoryIds = StubAdapter.getTypeIds( obj ) ;
196         String JavaDoc myid = contactInfoList.getTargetIOR().getTypeId();
197         if ( dest.equals(myid) ) {
198             return true;
199     }
200         for ( int i=0; i<repositoryIds.length; i++ ) {
201             if ( dest.equals(repositoryIds[i]) ) {
202         return true;
203         }
204     }
205
206         // But repositoryIds may not be complete, so it may be necessary to
207
// go to server.
208

209     InputStream JavaDoc is = null;
210         try {
211             OutputStream JavaDoc os = request(null, "_is_a", true);
212             os.write_string(dest);
213             is = (InputStream JavaDoc) invoke((org.omg.CORBA.Object JavaDoc) null, os);
214
215         return is.read_boolean();
216
217         } catch (ApplicationException JavaDoc e) {
218         // This cannot happen.
219
throw wrapper.applicationExceptionInSpecialMethod( e ) ;
220     } catch (RemarshalException JavaDoc e) {
221         return is_a(obj, dest);
222     } finally {
223         releaseReply((org.omg.CORBA.Object JavaDoc)null, (InputStream JavaDoc)is);
224         }
225     }
226     
227     public boolean non_existent(org.omg.CORBA.Object JavaDoc obj)
228     {
229     InputStream JavaDoc is = null;
230         try {
231             OutputStream JavaDoc os = request(null, "_non_existent", true);
232             is = (InputStream JavaDoc) invoke((org.omg.CORBA.Object JavaDoc)null, os);
233
234         return is.read_boolean();
235
236     } catch (ApplicationException JavaDoc e) {
237         // This cannot happen.
238
throw wrapper.applicationExceptionInSpecialMethod( e ) ;
239     } catch (RemarshalException JavaDoc e) {
240         return non_existent(obj);
241     } finally {
242         releaseReply((org.omg.CORBA.Object JavaDoc)null, (InputStream JavaDoc)is);
243         }
244     }
245     
246     public org.omg.CORBA.Object JavaDoc duplicate(org.omg.CORBA.Object JavaDoc obj)
247     {
248     return obj;
249     }
250     
251     public void release(org.omg.CORBA.Object JavaDoc obj)
252     {
253     // DO NOT clear out internal variables to release memory
254
// This delegate may be pointed-to by other objrefs.
255
}
256
257     // obj._get_delegate() == this due to the argument passing conventions in
258
// portable.ObjectImpl, so we just ignore obj here.
259
public boolean is_equivalent(org.omg.CORBA.Object JavaDoc obj,
260                  org.omg.CORBA.Object JavaDoc ref)
261     {
262     if ( ref == null )
263         return false;
264
265     // If ref is a local object, it is not a Stub!
266
if (!StubAdapter.isStub(ref))
267         return false ;
268
269     Delegate JavaDoc del = StubAdapter.getDelegate(ref) ;
270     if (del == null)
271         return false ;
272
273     // Optimize the x.is_equivalent( x ) case
274
if (del == this)
275         return true;
276
277     // If delegate was created by a different ORB, return false
278
if (!(del instanceof CorbaClientDelegateImpl))
279         return false ;
280
281     CorbaClientDelegateImpl corbaDelegate = (CorbaClientDelegateImpl)del ;
282     CorbaContactInfoList ccil =
283         (CorbaContactInfoList)corbaDelegate.getContactInfoList() ;
284     return this.contactInfoList.getTargetIOR().isEquivalent(
285         ccil.getTargetIOR() );
286     }
287
288     /**
289      * This method overrides the org.omg.CORBA.portable.Delegate.equals method,
290      * and does the equality check based on IOR equality.
291      */

292     public boolean equals(org.omg.CORBA.Object JavaDoc self, java.lang.Object JavaDoc other)
293     {
294     if (other == null)
295         return false ;
296
297         if (!StubAdapter.isStub(other)) {
298             return false;
299         }
300         
301     Delegate JavaDoc delegate = StubAdapter.getDelegate( other ) ;
302     if (delegate == null)
303         return false ;
304
305         if (delegate instanceof CorbaClientDelegateImpl) {
306             CorbaClientDelegateImpl otherDel = (CorbaClientDelegateImpl)
307         delegate ;
308             IOR otherIor = otherDel.contactInfoList.getTargetIOR();
309             return this.contactInfoList.getTargetIOR().equals(otherIor);
310         }
311
312     // Come here if other is not implemented by our ORB.
313
return false;
314     }
315
316     public int hashCode(org.omg.CORBA.Object JavaDoc obj)
317     {
318     return this.hashCode() ;
319     }
320
321     public int hash(org.omg.CORBA.Object JavaDoc obj, int maximum)
322     {
323     int h = this.hashCode();
324     if ( h > maximum )
325         return 0;
326     return h;
327     }
328     
329     public Request JavaDoc request(org.omg.CORBA.Object JavaDoc obj, String JavaDoc operation)
330     {
331     return new RequestImpl(orb, obj, null, operation, null, null, null,
332                    null);
333     }
334     
335     public Request JavaDoc create_request(org.omg.CORBA.Object JavaDoc obj,
336                   Context JavaDoc ctx,
337                   String JavaDoc operation,
338                   NVList JavaDoc arg_list,
339                   NamedValue JavaDoc result)
340     {
341     return new RequestImpl(orb, obj, ctx, operation, arg_list,
342                    result, null, null);
343     }
344     
345     public Request JavaDoc create_request(org.omg.CORBA.Object JavaDoc obj,
346                   Context JavaDoc ctx,
347                   String JavaDoc operation,
348                   NVList JavaDoc arg_list,
349                   NamedValue JavaDoc result,
350                   ExceptionList JavaDoc exclist,
351                   ContextList JavaDoc ctxlist)
352     {
353     return new RequestImpl(orb, obj, ctx, operation, arg_list, result,
354                    exclist, ctxlist);
355     }
356     
357     public org.omg.CORBA.ORB JavaDoc orb(org.omg.CORBA.Object JavaDoc obj)
358     {
359     return this.orb;
360     }
361     
362     /**
363      * Returns true if this object is implemented by a local servant.
364      *
365      * REVISIT: locatedIOR should be replaced with a method call that
366      * returns the current IOR for this request (e.g. ContactInfoChooser).
367      *
368      * @param self The object reference which delegated to this delegate.
369      * @return true only if the servant incarnating this object is located in
370      * this ORB.
371      */

372     public boolean is_local(org.omg.CORBA.Object JavaDoc self)
373     {
374     // XXX this need to check isNextCallValid
375
return contactInfoList.getEffectiveTargetIOR().getProfile().
376         isLocal();
377     }
378     
379     public ServantObject JavaDoc servant_preinvoke(org.omg.CORBA.Object JavaDoc self,
380                        String JavaDoc operation,
381                        Class JavaDoc expectedType)
382     {
383     return
384         contactInfoList.getLocalClientRequestDispatcher()
385         .servant_preinvoke(self, operation, expectedType);
386     }
387     
388     public void servant_postinvoke(org.omg.CORBA.Object JavaDoc self,
389                    ServantObject JavaDoc servant)
390     {
391     contactInfoList.getLocalClientRequestDispatcher()
392         .servant_postinvoke(self, servant);
393     }
394     
395     // XXX Should this be public?
396
/* Returns the codebase for object reference provided.
397      * @param self the object reference whose codebase needs to be returned.
398      * @return the codebase as a space delimited list of url strings or
399      * null if none.
400      */

401     public String JavaDoc get_codebase(org.omg.CORBA.Object JavaDoc self)
402     {
403     if (contactInfoList.getTargetIOR() != null) {
404         return contactInfoList.getTargetIOR().getProfile().getCodebase();
405     }
406     return null;
407     }
408
409     public String JavaDoc toString(org.omg.CORBA.Object JavaDoc self)
410     {
411     return contactInfoList.getTargetIOR().stringify();
412     }
413     
414     ////////////////////////////////////////////////////
415
//
416
// java.lang.Object
417
//
418

419     public int hashCode()
420     {
421     return this.contactInfoList.hashCode();
422     }
423 }
424
425 // End of file.
426

427
Popular Tags