KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > javax > rmi > CORBA > Util


1 /*
2  * @(#)Util.java 1.45 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  * 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.javax.rmi.CORBA; // Util (sed marker, don't remove!)
17

18 import java.rmi.RemoteException JavaDoc;
19 import java.rmi.UnexpectedException JavaDoc;
20 import java.rmi.MarshalException JavaDoc;
21
22 import java.rmi.server.RMIClassLoader JavaDoc;
23
24 import java.util.Hashtable JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.WeakHashMap JavaDoc;
29
30 import java.io.Serializable JavaDoc;
31 import java.io.NotSerializableException JavaDoc;
32
33 import java.lang.reflect.Constructor JavaDoc;
34
35 import javax.rmi.CORBA.ValueHandler JavaDoc;
36 import javax.rmi.CORBA.Tie JavaDoc;
37
38 import java.security.AccessController JavaDoc;
39 import java.security.PrivilegedAction JavaDoc;
40
41 import java.rmi.MarshalException JavaDoc;
42 import java.rmi.NoSuchObjectException JavaDoc;
43 import java.rmi.AccessException JavaDoc;
44 import java.rmi.Remote JavaDoc;
45 import java.rmi.ServerError JavaDoc;
46 import java.rmi.ServerException JavaDoc;
47 import java.rmi.ServerRuntimeException JavaDoc;
48
49 import javax.transaction.TransactionRequiredException JavaDoc;
50 import javax.transaction.TransactionRolledbackException JavaDoc;
51 import javax.transaction.InvalidTransactionException JavaDoc;
52
53 import org.omg.CORBA.SystemException JavaDoc;
54 import org.omg.CORBA.Any JavaDoc;
55 import org.omg.CORBA.TypeCode JavaDoc;
56 import org.omg.CORBA.COMM_FAILURE JavaDoc;
57 import org.omg.CORBA.BAD_PARAM JavaDoc;
58 import org.omg.CORBA.INV_OBJREF JavaDoc;
59 import org.omg.CORBA.NO_PERMISSION JavaDoc;
60 import org.omg.CORBA.MARSHAL JavaDoc;
61 import org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc;
62 import org.omg.CORBA.TRANSACTION_REQUIRED JavaDoc;
63 import org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc;
64 import org.omg.CORBA.INVALID_TRANSACTION JavaDoc;
65 import org.omg.CORBA.BAD_OPERATION JavaDoc;
66 import org.omg.CORBA.ACTIVITY_REQUIRED JavaDoc;
67 import org.omg.CORBA.ACTIVITY_COMPLETED JavaDoc;
68 import org.omg.CORBA.INVALID_ACTIVITY JavaDoc;
69 import org.omg.CORBA.CompletionStatus JavaDoc;
70 import org.omg.CORBA.TCKind JavaDoc;
71 import org.omg.CORBA.portable.UnknownException JavaDoc;
72 import org.omg.CORBA.portable.InputStream JavaDoc;
73 import org.omg.CORBA.portable.OutputStream JavaDoc;
74
75 // This class must be able to function with non-Sun ORBs.
76
// This means that any of the following com.sun.corba classes
77
// must only occur in contexts that also handle the non-Sun case.
78

79 import com.sun.corba.se.pept.transport.ContactInfoList ;
80 import com.sun.corba.se.spi.orb.ORB;
81 import com.sun.corba.se.spi.orb.ORBVersionFactory;
82 import com.sun.corba.se.spi.protocol.CorbaClientDelegate;
83 import com.sun.corba.se.spi.transport.CorbaContactInfoList ;
84 import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcher ;
85 import com.sun.corba.se.spi.copyobject.ReflectiveCopyException ;
86 import com.sun.corba.se.spi.copyobject.CopierManager ;
87 import com.sun.corba.se.spi.copyobject.ObjectCopierFactory ;
88 import com.sun.corba.se.spi.copyobject.ObjectCopier ;
89 import com.sun.corba.se.impl.io.ValueHandlerImpl;
90 import com.sun.corba.se.impl.orbutil.ORBConstants;
91 import com.sun.corba.se.impl.orbutil.ORBUtility;
92 import com.sun.corba.se.impl.logging.OMGSystemException;
93 import com.sun.corba.se.impl.util.Utility;
94 import com.sun.corba.se.impl.util.IdentityHashtable;
95 import com.sun.corba.se.impl.util.JDKBridge;
96 import com.sun.corba.se.impl.orbutil.ORBClassLoader;
97 import com.sun.corba.se.impl.logging.UtilSystemException;
98 import com.sun.corba.se.spi.logging.CORBALogDomains;
99
100 /**
101  * Provides utility methods that can be used by stubs and ties to
102  * perform common operations.
103  */

104 public class Util implements javax.rmi.CORBA.UtilDelegate JavaDoc
105 {
106     // Runs as long as there are exportedServants
107
private static KeepAlive keepAlive = null;
108
109     // Maps targets to ties.
110
private static IdentityHashtable exportedServants = new IdentityHashtable();
111
112     private static ValueHandlerImpl valueHandlerSingleton = new ValueHandlerImpl();
113
114     private UtilSystemException utilWrapper = UtilSystemException.get(
115                                                   CORBALogDomains.RPC_ENCODING);
116       
117     public static Util instance = null;
118
119     public Util() {
120         instance = this;
121     }
122
123     // Used by TOAFactory.shutdown to unexport all targets for this
124
// particular ORB. This happens during ORB shutdown.
125
public void unregisterTargetsForORB(org.omg.CORBA.ORB JavaDoc orb)
126     {
127         for (Enumeration JavaDoc e = exportedServants.keys(); e.hasMoreElements(); )
128     {
129             java.lang.Object JavaDoc key = e.nextElement();
130             Remote JavaDoc target = (Remote JavaDoc)(key instanceof Tie JavaDoc ? ((Tie JavaDoc)key).getTarget() : key);
131
132             // Bug 4476347: BAD_OPERATION is thrown if the ties delegate isn't set.
133
// We can ignore this because it means the tie is not connected to an ORB.
134
try {
135                 if (orb == getTie(target).orb()) {
136                     try {
137                         unexportObject(target);
138                     } catch( java.rmi.NoSuchObjectException JavaDoc ex ) {
139                         // We neglect this exception if at all if it is
140
// raised. It is not harmful.
141
}
142                 }
143             } catch (BAD_OPERATION JavaDoc bad) {
144         /* Ignore */
145         }
146         }
147     }
148
149    /**
150      * Maps a SystemException to a RemoteException.
151      * @param ex the SystemException to map.
152      * @return the mapped exception.
153      */

154     public RemoteException JavaDoc mapSystemException(SystemException JavaDoc ex)
155     {
156         if (ex instanceof UnknownException JavaDoc) {
157             Throwable JavaDoc orig = ((UnknownException JavaDoc)ex).originalEx;
158             if (orig instanceof Error JavaDoc) {
159                 return new ServerError JavaDoc("Error occurred in server thread",(Error JavaDoc)orig);
160             } else if (orig instanceof RemoteException JavaDoc) {
161                 return new ServerException JavaDoc("RemoteException occurred in server thread",
162             (Exception JavaDoc)orig);
163             } else if (orig instanceof RuntimeException JavaDoc) {
164                 throw (RuntimeException JavaDoc) orig;
165             }
166         }
167
168         // Build the message string...
169
String JavaDoc name = ex.getClass().getName();
170         String JavaDoc corbaName = name.substring(name.lastIndexOf('.')+1);
171         String JavaDoc status;
172         switch (ex.completed.value()) {
173         case CompletionStatus._COMPLETED_YES:
174         status = "Yes";
175         break;
176         case CompletionStatus._COMPLETED_NO:
177         status = "No";
178         break;
179         case CompletionStatus._COMPLETED_MAYBE:
180         default:
181         status = "Maybe";
182         break;
183         }
184     
185         String JavaDoc message = "CORBA " + corbaName + " " + ex.minor + " " + status;
186
187         // Now map to the correct RemoteException type...
188
if (ex instanceof COMM_FAILURE JavaDoc) {
189             return new MarshalException JavaDoc(message, ex);
190         } else if (ex instanceof INV_OBJREF JavaDoc) {
191             RemoteException JavaDoc newEx = new NoSuchObjectException JavaDoc(message);
192             newEx.detail = ex;
193             return newEx;
194         } else if (ex instanceof NO_PERMISSION JavaDoc) {
195             return new AccessException JavaDoc(message, ex);
196         } else if (ex instanceof MARSHAL JavaDoc) {
197             return new MarshalException JavaDoc(message, ex);
198         } else if (ex instanceof OBJECT_NOT_EXIST JavaDoc) {
199             RemoteException JavaDoc newEx = new NoSuchObjectException JavaDoc(message);
200             newEx.detail = ex;
201             return newEx;
202         } else if (ex instanceof TRANSACTION_REQUIRED JavaDoc) {
203             RemoteException JavaDoc newEx = new TransactionRequiredException JavaDoc(message);
204             newEx.detail = ex;
205             return newEx;
206         } else if (ex instanceof TRANSACTION_ROLLEDBACK JavaDoc) {
207             RemoteException JavaDoc newEx = new TransactionRolledbackException JavaDoc(message);
208             newEx.detail = ex;
209             return newEx;
210         } else if (ex instanceof INVALID_TRANSACTION JavaDoc) {
211             RemoteException JavaDoc newEx = new InvalidTransactionException JavaDoc(message);
212             newEx.detail = ex;
213             return newEx;
214         } else if (ex instanceof BAD_PARAM JavaDoc) {
215             Exception JavaDoc inner = ex;
216
217             // Pre-Merlin Sun ORBs used the incorrect minor code for
218
// this case. See Java to IDL ptc-00-01-08 1.4.8.
219
if (ex.minor == ORBConstants.LEGACY_SUN_NOT_SERIALIZABLE ||
220                 ex.minor == OMGSystemException.NOT_SERIALIZABLE) {
221
222                 if (ex.getMessage() != null)
223                     inner = new NotSerializableException JavaDoc(ex.getMessage());
224                 else
225                     inner = new NotSerializableException JavaDoc();
226
227         inner.initCause( ex ) ;
228             }
229
230             return new MarshalException JavaDoc(message,inner);
231         } else if (ex instanceof ACTIVITY_REQUIRED JavaDoc) {
232         try {
233         Class JavaDoc cl = ORBClassLoader.loadClass(
234                    "javax.activity.ActivityRequiredException");
235                 Class JavaDoc[] params = new Class JavaDoc[2];
236                 params[0] = java.lang.String JavaDoc.class;
237                 params[1] = java.lang.Throwable JavaDoc.class;
238                 Constructor JavaDoc cr = cl.getConstructor(params);
239                 Object JavaDoc[] args = new Object JavaDoc[2];
240                 args[0] = message;
241                 args[1] = ex;
242                 return (RemoteException JavaDoc) cr.newInstance(args);
243         } catch (Throwable JavaDoc e) {
244                 utilWrapper.classNotFound(
245                               e, "javax.activity.ActivityRequiredException");
246             }
247         } else if (ex instanceof ACTIVITY_COMPLETED JavaDoc) {
248         try {
249         Class JavaDoc cl = ORBClassLoader.loadClass(
250                    "javax.activity.ActivityCompletedException");
251         Class JavaDoc[] params = new Class JavaDoc[2];
252         params[0] = java.lang.String JavaDoc.class;
253         params[1] = java.lang.Throwable JavaDoc.class;
254         Constructor JavaDoc cr = cl.getConstructor(params);
255         Object JavaDoc[] args = new Object JavaDoc[2];
256         args[0] = message;
257         args[1] = ex;
258         return (RemoteException JavaDoc) cr.newInstance(args);
259               } catch (Throwable JavaDoc e) {
260                   utilWrapper.classNotFound(
261                                 e, "javax.activity.ActivityCompletedException");
262               }
263         } else if (ex instanceof INVALID_ACTIVITY JavaDoc) {
264         try {
265         Class JavaDoc cl = ORBClassLoader.loadClass(
266                    "javax.activity.InvalidActivityException");
267         Class JavaDoc[] params = new Class JavaDoc[2];
268         params[0] = java.lang.String JavaDoc.class;
269         params[1] = java.lang.Throwable JavaDoc.class;
270         Constructor JavaDoc cr = cl.getConstructor(params);
271         Object JavaDoc[] args = new Object JavaDoc[2];
272         args[0] = message;
273         args[1] = ex;
274         return (RemoteException JavaDoc) cr.newInstance(args);
275               } catch (Throwable JavaDoc e) {
276                   utilWrapper.classNotFound(
277                                 e, "javax.activity.InvalidActivityException");
278               }
279     }
280
281         // Just map to a generic RemoteException...
282
return new RemoteException JavaDoc(message, ex);
283     }
284
285     /**
286      * Writes any java.lang.Object as a CORBA any.
287      * @param out the stream in which to write the any.
288      * @param obj the object to write as an any.
289      */

290     public void writeAny( org.omg.CORBA.portable.OutputStream JavaDoc out,
291                          java.lang.Object JavaDoc obj)
292     {
293         org.omg.CORBA.ORB JavaDoc orb = out.orb();
294
295         // Create Any
296
Any JavaDoc any = orb.create_any();
297
298         // Make sure we have a connected object...
299
java.lang.Object JavaDoc newObj = Utility.autoConnect(obj,orb,false);
300
301         if (newObj instanceof org.omg.CORBA.Object JavaDoc) {
302             any.insert_Object((org.omg.CORBA.Object JavaDoc)newObj);
303         } else {
304             if (newObj == null) {
305                 // Handle the null case, including backwards
306
// compatibility issues
307
any.insert_Value(null, createTypeCodeForNull(orb));
308             } else {
309                 if (newObj instanceof Serializable JavaDoc) {
310                     // If they're our Any and ORB implementations,
311
// we may want to do type code related versioning.
312
TypeCode JavaDoc tc = createTypeCode((Serializable JavaDoc)newObj, any, orb);
313                     if (tc == null)
314                         any.insert_Value((Serializable JavaDoc)newObj);
315                     else
316                         any.insert_Value((Serializable JavaDoc)newObj, tc);
317                 } else if (newObj instanceof Remote JavaDoc) {
318                     ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName());
319                 } else {
320                     ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName());
321                 }
322             }
323         }
324
325         out.write_any(any);
326     }
327
328     /**
329      * When using our own ORB and Any implementations, we need to get
330      * the ORB version and create the type code appropriately. This is
331      * to overcome a bug in which the JDK 1.3.x ORBs used a tk_char
332      * rather than a tk_wchar to describe a Java char field.
333      *
334      * This only works in RMI-IIOP with Util.writeAny since we actually
335      * know what ORB and stream we're writing with when we insert
336      * the value.
337      *
338      * Returns null if it wasn't possible to create the TypeCode (means
339      * it wasn't our ORB or Any implementation).
340      *
341      * This does not handle null objs.
342      */

343     private TypeCode JavaDoc createTypeCode(Serializable JavaDoc obj,
344                                     org.omg.CORBA.Any JavaDoc any,
345                                     org.omg.CORBA.ORB JavaDoc orb) {
346
347         if (any instanceof com.sun.corba.se.impl.corba.AnyImpl &&
348             orb instanceof ORB) {
349
350             com.sun.corba.se.impl.corba.AnyImpl anyImpl
351                 = (com.sun.corba.se.impl.corba.AnyImpl)any;
352
353             ORB ourORB = (ORB)orb;
354
355             return anyImpl.createTypeCodeForClass(obj.getClass(), ourORB);
356
357         } else
358             return null;
359     }
360
361
362     /**
363      * This is used to create the TypeCode for a null reference.
364      * It also handles backwards compatibility with JDK 1.3.x.
365      *
366      * This method will not return null.
367      */

368     private TypeCode JavaDoc createTypeCodeForNull(org.omg.CORBA.ORB JavaDoc orb)
369     {
370         if (orb instanceof ORB) {
371
372             ORB ourORB = (ORB)orb;
373
374             // Preserve backwards compatibility with Kestrel and Ladybird
375
// by not fully implementing interop issue resolution 3857,
376
// and returning a null TypeCode with a tk_value TCKind.
377
// If we're not talking to Kestrel or Ladybird, fall through
378
// to the abstract interface case (also used for foreign ORBs).
379
if (!ORBVersionFactory.getFOREIGN().equals(ourORB.getORBVersion()) &&
380                 ORBVersionFactory.getNEWER().compareTo(ourORB.getORBVersion()) > 0) {
381
382                 return orb.get_primitive_tc(TCKind.tk_value);
383             }
384         }
385
386         // Use tk_abstract_interface as detailed in the resolution
387

388         // REVISIT: Define this in IDL and get the ID in generated code
389
String JavaDoc abstractBaseID = "IDL:omg.org/CORBA/AbstractBase:1.0";
390
391         return orb.create_abstract_interface_tc(abstractBaseID, "");
392     }
393
394     /**
395      * Reads a java.lang.Object as a CORBA any.
396      * @param in the stream from which to read the any.
397      * @return the object read from the stream.
398      */

399     public Object JavaDoc readAny(InputStream JavaDoc in)
400     {
401     Any JavaDoc any = in.read_any();
402         if ( any.type().kind().value() == TCKind._tk_objref )
403         return any.extract_Object ();
404         else
405         return any.extract_Value();
406     }
407
408     /**
409      * Writes a java.lang.Object as a CORBA Object. If <code>obj</code> is
410      * an exported RMI-IIOP server object, the tie is found
411      * and wired to <code>obj</code>, then written to <code>out.write_Object(org.omg.CORBA.Object)</code>.
412      * If <code>obj</code> is a CORBA Object, it is written to
413      * <code>out.write_Object(org.omg.CORBA.Object)</code>.
414      * @param out the stream in which to write the object.
415      * @param obj the object to write.
416      */

417     public void writeRemoteObject(OutputStream JavaDoc out, java.lang.Object JavaDoc obj)
418     {
419         // Make sure we have a connected object, then
420
// write it out...
421

422         Object JavaDoc newObj = Utility.autoConnect(obj,out.orb(),false);
423     out.write_Object((org.omg.CORBA.Object JavaDoc)newObj);
424     }
425     
426     /**
427      * Writes a java.lang.Object as either a value or a CORBA Object.
428      * If <code>obj</code> is a value object or a stub object, it is written to
429      * <code>out.write_abstract_interface(java.lang.Object)</code>. If <code>obj</code> is an exported
430      * RMI-IIOP server object, the tie is found and wired to <code>obj</code>,
431      * then written to <code>out.write_abstract_interface(java.lang.Object)</code>.
432      * @param out the stream in which to write the object.
433      * @param obj the object to write.
434      */

435     public void writeAbstractObject( OutputStream JavaDoc out, java.lang.Object JavaDoc obj )
436     {
437         // Make sure we have a connected object, then
438
// write it out...
439

440         Object JavaDoc newObj = Utility.autoConnect(obj,out.orb(),false);
441         ((org.omg.CORBA_2_3.portable.OutputStream JavaDoc)out).write_abstract_interface(newObj);
442     }
443     
444     /**
445      * Registers a target for a tie. Adds the tie to an internal table and calls
446      * {@link Tie#setTarget} on the tie object.
447      * @param tie the tie to register.
448      * @param target the target for the tie.
449      */

450     public void registerTarget(javax.rmi.CORBA.Tie JavaDoc tie, java.rmi.Remote JavaDoc target)
451     {
452         synchronized (exportedServants) {
453             // Do we already have this target registered?
454
if (lookupTie(target) == null) {
455                 // No, so register it and set the target...
456
exportedServants.put(target,tie);
457                 tie.setTarget(target);
458             
459                 // Do we need to instantiate our keep-alive thread?
460
if (keepAlive == null) {
461                     // Yes. Instantiate our keep-alive thread and start
462
// it up...
463
keepAlive = (KeepAlive)AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
464                         public java.lang.Object JavaDoc run() {
465                             return new KeepAlive();
466                         }
467                     });
468                     keepAlive.start();
469                 }
470             }
471         }
472     }
473     
474     /**
475      * Removes the associated tie from an internal table and calls {@link Tie#deactivate}
476      * to deactivate the object.
477      * @param target the object to unexport.
478      */

479     public void unexportObject(java.rmi.Remote JavaDoc target)
480         throws java.rmi.NoSuchObjectException JavaDoc
481     {
482         synchronized (exportedServants) {
483             Tie JavaDoc cachedTie = lookupTie(target);
484             if (cachedTie != null) {
485                 exportedServants.remove(target);
486                 Utility.purgeStubForTie(cachedTie);
487         Utility.purgeTieAndServant(cachedTie);
488                 try {
489                     cleanUpTie(cachedTie);
490                 } catch (BAD_OPERATION JavaDoc e) {
491             // ignore
492
} catch (org.omg.CORBA.OBJ_ADAPTER JavaDoc e) {
493                     // This can happen when the target was never associated with a POA.
494
// We can safely ignore this case.
495
}
496                 
497                 // Is it time to shut down our keep alive thread?
498
if (exportedServants.isEmpty()) {
499                     keepAlive.quit();
500                     keepAlive = null;
501                 }
502             } else {
503                 throw new java.rmi.NoSuchObjectException JavaDoc("Tie not found" );
504             }
505         }
506     }
507
508     protected void cleanUpTie(Tie JavaDoc cachedTie)
509         throws java.rmi.NoSuchObjectException JavaDoc
510     {
511         cachedTie.setTarget(null);
512         cachedTie.deactivate();
513     }
514     
515     /**
516      * Returns the tie (if any) for a given target object.
517      * @return the tie or null if no tie is registered for the given target.
518      */

519     public Tie JavaDoc getTie (Remote JavaDoc target)
520     {
521         synchronized (exportedServants) {
522             return lookupTie(target);
523         }
524     }
525
526     /**
527      * An unsynchronized version of getTie() for internal use.
528      */

529     private static Tie JavaDoc lookupTie (Remote JavaDoc target)
530     {
531         Tie JavaDoc result = (Tie JavaDoc)exportedServants.get(target);
532         if (result == null && target instanceof Tie JavaDoc) {
533             if (exportedServants.contains(target)) {
534                 result = (Tie JavaDoc)target;
535             }
536         }
537         return result;
538     }
539
540     /**
541      * Returns a singleton instance of a class that implements the
542      * {@link ValueHandler} interface.
543      * @return a class which implements the ValueHandler interface.
544      */

545     public ValueHandler JavaDoc createValueHandler()
546     {
547         return valueHandlerSingleton;
548     }
549
550     /**
551      * Returns the codebase, if any, for the given class.
552      * @param clz the class to get a codebase for.
553      * @return a space-separated list of URLs, or null.
554      */

555     public String JavaDoc getCodebase(java.lang.Class JavaDoc clz) {
556         return RMIClassLoader.getClassAnnotation(clz);
557     }
558
559     /**
560      * Returns a class instance for the specified class.
561      * @param className the name of the class.
562      * @param remoteCodebase a space-separated list of URLs at which
563      * the class might be found. May be null.
564      * @param loadingContext a class whose ClassLoader may be used to
565      * load the class if all other methods fail.
566      * @return the <code>Class</code> object representing the loaded class.
567      * @exception ClassNotFoundException if class cannot be loaded.
568      */

569     public Class JavaDoc loadClass( String JavaDoc className, String JavaDoc remoteCodebase,
570     ClassLoader JavaDoc loader) throws ClassNotFoundException JavaDoc
571     {
572         return JDKBridge.loadClass(className,remoteCodebase,loader);
573     }
574
575     /**
576      * The <tt>isLocal</tt> method has the same semantics as the
577      * ObjectImpl._is_local method, except that it can throw a RemoteException.
578      * (no it doesn't but the spec says it should.)
579      *
580      * The <tt>_is_local()</tt> method is provided so that stubs may determine
581      * if a particular object is implemented by a local servant and hence local
582      * invocation APIs may be used.
583      *
584      * @param stub the stub to test.
585      *
586      * @return The <tt>_is_local()</tt> method returns true if
587      * the servant incarnating the object is located in the same process as
588      * the stub and they both share the same ORB instance. The <tt>_is_local()</tt>
589      * method returns false otherwise. The default behavior of <tt>_is_local()</tt> is
590      * to return false.
591      *
592      * @throws RemoteException The Java to IDL specification does to
593      * specify the conditions that cause a RemoteException to be thrown.
594      */

595     public boolean isLocal(javax.rmi.CORBA.Stub JavaDoc stub) throws RemoteException JavaDoc
596     {
597     boolean result = false ;
598
599         try {
600         org.omg.CORBA.portable.Delegate JavaDoc delegate = stub._get_delegate() ;
601         if (delegate instanceof CorbaClientDelegate) {
602         // For the Sun ORB
603
CorbaClientDelegate cdel = (CorbaClientDelegate)delegate ;
604         ContactInfoList cil = cdel.getContactInfoList() ;
605         if (cil instanceof CorbaContactInfoList) {
606             CorbaContactInfoList ccil = (CorbaContactInfoList)cil ;
607             LocalClientRequestDispatcher lcs = ccil.getLocalClientRequestDispatcher() ;
608             result = lcs.useLocalInvocation( null ) ;
609         }
610         } else {
611         // For a non-Sun ORB
612
result = delegate.is_local( stub ) ;
613         }
614         } catch (SystemException JavaDoc e) {
615             throw javax.rmi.CORBA.Util.mapSystemException(e);
616         }
617
618     return result ;
619     }
620     
621     /**
622      * Wraps an exception thrown by an implementation
623      * method. It returns the corresponding client-side exception.
624      * @param orig the exception to wrap.
625      * @return the wrapped exception.
626      */

627     public RemoteException JavaDoc wrapException(Throwable JavaDoc orig)
628     {
629         if (orig instanceof SystemException JavaDoc) {
630             return mapSystemException((SystemException JavaDoc)orig);
631         }
632         
633         if (orig instanceof Error JavaDoc) {
634             return new ServerError JavaDoc("Error occurred in server thread",(Error JavaDoc)orig);
635         } else if (orig instanceof RemoteException JavaDoc) {
636         return new ServerException JavaDoc("RemoteException occurred in server thread",
637                        (Exception JavaDoc)orig);
638         } else if (orig instanceof RuntimeException JavaDoc) {
639             throw (RuntimeException JavaDoc) orig;
640         }
641         
642     if (orig instanceof Exception JavaDoc)
643         return new UnexpectedException JavaDoc( orig.toString(), (Exception JavaDoc)orig );
644     else
645         return new UnexpectedException JavaDoc( orig.toString());
646     }
647
648     /**
649      * Copies or connects an array of objects. Used by local stubs
650      * to copy any number of actual parameters, preserving sharing
651      * across parameters as necessary to support RMI semantics.
652      * @param obj the objects to copy or connect.
653      * @param orb the ORB.
654      * @return the copied or connected objects.
655      * @exception RemoteException if any object could not be copied or connected.
656      */

657     public Object JavaDoc[] copyObjects (Object JavaDoc[] obj, org.omg.CORBA.ORB JavaDoc orb)
658     throws RemoteException JavaDoc
659     {
660     if (obj == null)
661         // Bug fix for 5018613: JCK test expects copyObjects to throw
662
// NPE when obj==null. This is actually not in the spec, since
663
// obj is not really an RMI-IDL data type, but we follow our
664
// test here, and force this error to be thrown.
665
throw new NullPointerException JavaDoc() ;
666
667     Class JavaDoc compType = obj.getClass().getComponentType() ;
668     if (Remote JavaDoc.class.isAssignableFrom( compType ) && !compType.isInterface()) {
669         // obj is an array of remote impl types. This
670
// causes problems with stream copier, so we copy
671
// it over to an array of Remotes instead.
672
Remote JavaDoc[] result = new Remote JavaDoc[obj.length] ;
673         System.arraycopy( (Object JavaDoc)obj, 0, (Object JavaDoc)result, 0, obj.length ) ;
674         return (Object JavaDoc[])copyObject( result, orb ) ;
675     } else
676         return (Object JavaDoc[])copyObject( obj, orb ) ;
677     }
678
679     /**
680      * Copies or connects an object. Used by local stubs to copy
681      * an actual parameter, result object, or exception.
682      * @param obj the object to copy.
683      * @param orb the ORB.
684      * @return the copy or connected object.
685      * @exception RemoteException if the object could not be copied or connected.
686      */

687     public Object JavaDoc copyObject (Object JavaDoc obj, org.omg.CORBA.ORB JavaDoc orb)
688     throws RemoteException JavaDoc
689     {
690     if (orb instanceof ORB) {
691         ORB lorb = (ORB)orb ;
692
693         try {
694         try {
695             // This gets the copier for the current invocation, which was
696
// previously set by preinvoke.
697
return lorb.peekInvocationInfo().getCopierFactory().make().copy( obj ) ;
698         } catch (java.util.EmptyStackException JavaDoc exc) {
699             // copyObject was invoked outside of an invocation, probably by
700
// a test. Get the default copier from the ORB.
701
// XXX should we just make the default copier available directly
702
// and avoid constructing one on each call?
703
CopierManager cm = lorb.getCopierManager() ;
704             ObjectCopier copier = cm.getDefaultObjectCopierFactory().make() ;
705             return copier.copy( obj ) ;
706         }
707         } catch (ReflectiveCopyException exc) {
708         RemoteException JavaDoc rexc = new RemoteException JavaDoc() ;
709         rexc.initCause( exc ) ;
710         throw rexc ;
711         }
712     } else {
713         org.omg.CORBA_2_3.portable.OutputStream JavaDoc out =
714         (org.omg.CORBA_2_3.portable.OutputStream JavaDoc)orb.create_output_stream();
715         out.write_value((Serializable JavaDoc)obj);
716         org.omg.CORBA_2_3.portable.InputStream JavaDoc in =
717         (org.omg.CORBA_2_3.portable.InputStream JavaDoc)out.create_input_stream();
718         return in.read_value();
719     }
720     }
721 }
722
723 class KeepAlive extends Thread JavaDoc
724 {
725     boolean quit = false;
726     
727     public KeepAlive ()
728     {
729         setDaemon(false);
730     }
731     
732     public synchronized void run ()
733     {
734         while (!quit) {
735             try {
736                 wait();
737             } catch (InterruptedException JavaDoc e) {}
738         }
739     }
740     
741     public synchronized void quit ()
742     {
743         quit = true;
744         notifyAll();
745     }
746 }
747
Popular Tags