Util.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25/*
26 * Licensed Materials - Property of IBM
27 * RMI-IIOP v1.0
28 * Copyright IBM Corp. 1998 1999  All Rights Reserved
29 *
30 */
31
32package com.sun.corba.se.impl.javax.rmi.CORBA; // Util (sed marker, don't remove!)
33
34import java.rmi.RemoteException;
35import java.rmi.UnexpectedException;
36import java.rmi.MarshalException;
37
38import java.rmi.server.RMIClassLoader;
39
40import java.util.Hashtable;
41import java.util.Enumeration;
42import java.util.Properties;
43import java.util.Map;
44import java.util.WeakHashMap;
45
46import java.io.Serializable;
47import java.io.NotSerializableException;
48
49import java.lang.reflect.Constructor;
50
51import javax.rmi.CORBA.ValueHandler;
52import javax.rmi.CORBA.Tie;
53
54import java.security.AccessController;
55import java.security.PrivilegedAction;
56
57import java.rmi.MarshalException;
58import java.rmi.NoSuchObjectException;
59import java.rmi.AccessException;
60import java.rmi.Remote;
61import java.rmi.ServerError;
62import java.rmi.ServerException;
63import java.rmi.ServerRuntimeException;
64
65import javax.transaction.TransactionRequiredException;
66import javax.transaction.TransactionRolledbackException;
67import javax.transaction.InvalidTransactionException;
68
69import org.omg.CORBA.SystemException;
70import org.omg.CORBA.Any;
71import org.omg.CORBA.TypeCode;
72import org.omg.CORBA.COMM_FAILURE;
73import org.omg.CORBA.BAD_PARAM;
74import org.omg.CORBA.INV_OBJREF;
75import org.omg.CORBA.NO_PERMISSION;
76import org.omg.CORBA.MARSHAL;
77import org.omg.CORBA.OBJECT_NOT_EXIST;
78import org.omg.CORBA.TRANSACTION_REQUIRED;
79import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
80import org.omg.CORBA.INVALID_TRANSACTION;
81import org.omg.CORBA.BAD_OPERATION;
82import org.omg.CORBA.ACTIVITY_REQUIRED;
83import org.omg.CORBA.ACTIVITY_COMPLETED;
84import org.omg.CORBA.INVALID_ACTIVITY;
85import org.omg.CORBA.CompletionStatus;
86import org.omg.CORBA.TCKind;
87import org.omg.CORBA.portable.UnknownException;
88import org.omg.CORBA.portable.InputStream;
89import org.omg.CORBA.portable.OutputStream;
90
91// This class must be able to function with non-Sun ORBs.
92// This means that any of the following com.sun.corba classes
93// must only occur in contexts that also handle the non-Sun case.
94
95import com.sun.corba.se.pept.transport.ContactInfoList ;
96import com.sun.corba.se.spi.orb.ORB;
97import com.sun.corba.se.spi.orb.ORBVersionFactory;
98import com.sun.corba.se.spi.protocol.CorbaClientDelegate;
99import com.sun.corba.se.spi.transport.CorbaContactInfoList ;
100import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcher ;
101import com.sun.corba.se.spi.copyobject.ReflectiveCopyException ;
102import com.sun.corba.se.spi.copyobject.CopierManager ;
103import com.sun.corba.se.spi.copyobject.ObjectCopierFactory ;
104import com.sun.corba.se.spi.copyobject.ObjectCopier ;
105import com.sun.corba.se.impl.io.ValueHandlerImpl;
106import com.sun.corba.se.impl.orbutil.ORBConstants;
107import com.sun.corba.se.impl.orbutil.ORBUtility;
108import com.sun.corba.se.impl.logging.OMGSystemException;
109import com.sun.corba.se.impl.util.Utility;
110import com.sun.corba.se.impl.util.IdentityHashtable;
111import com.sun.corba.se.impl.util.JDKBridge;
112import com.sun.corba.se.impl.logging.UtilSystemException;
113import com.sun.corba.se.spi.logging.CORBALogDomains;
114import sun.corba.SharedSecrets;
115
116/**
117 * Provides utility methods that can be used by stubs and ties to
118 * perform common operations.
119 */
120public class Util implements javax.rmi.CORBA.UtilDelegate
121{
122    // Runs as long as there are exportedServants
123    private static KeepAlive keepAlive = null;
124
125    // Maps targets to ties.
126    private static IdentityHashtable exportedServants = new IdentityHashtable();
127
128    private static final ValueHandlerImpl valueHandlerSingleton =
129        SharedSecrets.getJavaCorbaAccess().newValueHandlerImpl();
130
131    private UtilSystemException utilWrapper = UtilSystemException.get(
132                                                  CORBALogDomains.RPC_ENCODING);
133
134    private static Util instance = null;
135
136    public Util() {
137        setInstance(this);
138    }
139
140    private static void setInstance( Util util ) {
141        assert instance == null : "Instance already defined";
142        instance = util;
143    }
144
145    public static Util getInstance() {
146        return instance;
147    }
148
149    public static boolean isInstanceDefined() {
150        return instance != null;
151    }
152
153    // Used by TOAFactory.shutdown to unexport all targets for this
154    // particular ORB.  This happens during ORB shutdown.
155    public void unregisterTargetsForORB(org.omg.CORBA.ORB orb)
156    {
157        for (Enumeration e = exportedServants.keys(); e.hasMoreElements(); )
158        {
159            java.lang.Object key = e.nextElement();
160            Remote target = (Remote)(key instanceof Tie ? ((Tie)key).getTarget() : key);
161
162            // Bug 4476347: BAD_OPERATION is thrown if the ties delegate isn't set.
163            // We can ignore this because it means the tie is not connected to an ORB.
164            try {
165                if (orb == getTie(target).orb()) {
166                    try {
167                        unexportObject(target);
168                    } catch( java.rmi.NoSuchObjectException ex ) {
169                        // We neglect this exception if at all if it is
170                        // raised. It is not harmful.
171                    }
172                }
173            } catch (BAD_OPERATION bad) {
174                /* Ignore */
175            }
176        }
177    }
178
179   /**
180     * Maps a SystemException to a RemoteException.
181     * @param ex the SystemException to map.
182     * @return the mapped exception.
183     */
184    public RemoteException mapSystemException(SystemException ex)
185    {
186        if (ex instanceof UnknownException) {
187            Throwable orig = ((UnknownException)ex).originalEx;
188            if (orig instanceof Error) {
189                return new ServerError("Error occurred in server thread",(Error)orig);
190            } else if (orig instanceof RemoteException) {
191                return new ServerException("RemoteException occurred in server thread",
192                    (Exception)orig);
193            } else if (orig instanceof RuntimeException) {
194                throw (RuntimeException) orig;
195            }
196        }
197
198        // Build the message string...
199        String name = ex.getClass().getName();
200        String corbaName = name.substring(name.lastIndexOf('.')+1);
201        String status;
202        switch (ex.completed.value()) {
203            case CompletionStatus._COMPLETED_YES:
204                status = "Yes";
205                break;
206            case CompletionStatus._COMPLETED_NO:
207                status = "No";
208                break;
209            case CompletionStatus._COMPLETED_MAYBE:
210            default:
211                status = "Maybe";
212                break;
213        }
214
215        String message = "CORBA " + corbaName + " " + ex.minor + " " + status;
216
217        // Now map to the correct RemoteException type...
218        if (ex instanceof COMM_FAILURE) {
219            return new MarshalException(message, ex);
220        } else if (ex instanceof INV_OBJREF) {
221            RemoteException newEx = new NoSuchObjectException(message);
222            newEx.detail = ex;
223            return newEx;
224        } else if (ex instanceof NO_PERMISSION) {
225            return new AccessException(message, ex);
226        } else if (ex instanceof MARSHAL) {
227            return new MarshalException(message, ex);
228        } else if (ex instanceof OBJECT_NOT_EXIST) {
229            RemoteException newEx = new NoSuchObjectException(message);
230            newEx.detail = ex;
231            return newEx;
232        } else if (ex instanceof TRANSACTION_REQUIRED) {
233            RemoteException newEx = new TransactionRequiredException(message);
234            newEx.detail = ex;
235            return newEx;
236        } else if (ex instanceof TRANSACTION_ROLLEDBACK) {
237            RemoteException newEx = new TransactionRolledbackException(message);
238            newEx.detail = ex;
239            return newEx;
240        } else if (ex instanceof INVALID_TRANSACTION) {
241            RemoteException newEx = new InvalidTransactionException(message);
242            newEx.detail = ex;
243            return newEx;
244        } else if (ex instanceof BAD_PARAM) {
245            Exception inner = ex;
246
247            // Pre-Merlin Sun ORBs used the incorrect minor code for
248            // this case.  See Java to IDL ptc-00-01-08 1.4.8.
249            if (ex.minor == ORBConstants.LEGACY_SUN_NOT_SERIALIZABLE ||
250                ex.minor == OMGSystemException.NOT_SERIALIZABLE) {
251
252                if (ex.getMessage() != null)
253                    inner = new NotSerializableException(ex.getMessage());
254                else
255                    inner = new NotSerializableException();
256
257                inner.initCause( ex ) ;
258            }
259
260            return new MarshalException(message,inner);
261        } else if (ex instanceof ACTIVITY_REQUIRED) {
262            try {
263                Class<?> cl = SharedSecrets.getJavaCorbaAccess().loadClass(
264                               "javax.activity.ActivityRequiredException");
265                Class[] params = new Class[2];
266                params[0] = java.lang.String.class;
267                params[1] = java.lang.Throwable.class;
268                Constructor cr = cl.getConstructor(params);
269                Object[] args = new Object[2];
270                args[0] = message;
271                args[1] = ex;
272                return (RemoteException) cr.newInstance(args);
273            } catch (Throwable e) {
274                utilWrapper.classNotFound(
275                              e, "javax.activity.ActivityRequiredException");
276            }
277        } else if (ex instanceof ACTIVITY_COMPLETED) {
278            try {
279                Class<?> cl = SharedSecrets.getJavaCorbaAccess().loadClass(
280                               "javax.activity.ActivityCompletedException");
281                Class[] params = new Class[2];
282                params[0] = java.lang.String.class;
283                params[1] = java.lang.Throwable.class;
284                Constructor cr = cl.getConstructor(params);
285                Object[] args = new Object[2];
286                args[0] = message;
287                args[1] = ex;
288                return (RemoteException) cr.newInstance(args);
289              } catch (Throwable e) {
290                  utilWrapper.classNotFound(
291                                e, "javax.activity.ActivityCompletedException");
292              }
293        } else if (ex instanceof INVALID_ACTIVITY) {
294            try {
295                Class<?> cl = SharedSecrets.getJavaCorbaAccess().loadClass(
296                               "javax.activity.InvalidActivityException");
297                Class[] params = new Class[2];
298                params[0] = java.lang.String.class;
299                params[1] = java.lang.Throwable.class;
300                Constructor cr = cl.getConstructor(params);
301                Object[] args = new Object[2];
302                args[0] = message;
303                args[1] = ex;
304                return (RemoteException) cr.newInstance(args);
305              } catch (Throwable e) {
306                  utilWrapper.classNotFound(
307                                e, "javax.activity.InvalidActivityException");
308              }
309        }
310
311        // Just map to a generic RemoteException...
312        return new RemoteException(message, ex);
313    }
314
315    /**
316     * Writes any java.lang.Object as a CORBA any.
317     * @param out the stream in which to write the any.
318     * @param obj the object to write as an any.
319     */
320    public void writeAny( org.omg.CORBA.portable.OutputStream out,
321                         java.lang.Object obj)
322    {
323        org.omg.CORBA.ORB orb = out.orb();
324
325        // Create Any
326        Any any = orb.create_any();
327
328        // Make sure we have a connected object...
329        java.lang.Object newObj = Utility.autoConnect(obj,orb,false);
330
331        if (newObj instanceof org.omg.CORBA.Object) {
332            any.insert_Object((org.omg.CORBA.Object)newObj);
333        } else {
334            if (newObj == null) {
335                // Handle the null case, including backwards
336                // compatibility issues
337                any.insert_Value(null, createTypeCodeForNull(orb));
338            } else {
339                if (newObj instanceof Serializable) {
340                    // If they're our Any and ORB implementations,
341                    // we may want to do type code related versioning.
342                    TypeCode tc = createTypeCode((Serializable)newObj, any, orb);
343                    if (tc == null)
344                        any.insert_Value((Serializable)newObj);
345                    else
346                        any.insert_Value((Serializable)newObj, tc);
347                } else if (newObj instanceof Remote) {
348                    ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName());
349                } else {
350                    ORBUtility.throwNotSerializableForCorba(newObj.getClass().getName());
351                }
352            }
353        }
354
355        out.write_any(any);
356    }
357
358    /**
359     * When using our own ORB and Any implementations, we need to get
360     * the ORB version and create the type code appropriately.  This is
361     * to overcome a bug in which the JDK 1.3.x ORBs used a tk_char
362     * rather than a tk_wchar to describe a Java char field.
363     *
364     * This only works in RMI-IIOP with Util.writeAny since we actually
365     * know what ORB and stream we're writing with when we insert
366     * the value.
367     *
368     * Returns null if it wasn't possible to create the TypeCode (means
369     * it wasn't our ORB or Any implementation).
370     *
371     * This does not handle null objs.
372     */
373    private TypeCode createTypeCode(Serializable obj,
374                                    org.omg.CORBA.Any any,
375                                    org.omg.CORBA.ORB orb) {
376
377        if (any instanceof com.sun.corba.se.impl.corba.AnyImpl &&
378            orb instanceof ORB) {
379
380            com.sun.corba.se.impl.corba.AnyImpl anyImpl
381                = (com.sun.corba.se.impl.corba.AnyImpl)any;
382
383            ORB ourORB = (ORB)orb;
384
385            return anyImpl.createTypeCodeForClass(obj.getClass(), ourORB);
386
387        } else
388            return null;
389    }
390
391
392    /**
393     * This is used to create the TypeCode for a null reference.
394     * It also handles backwards compatibility with JDK 1.3.x.
395     *
396     * This method will not return null.
397     */
398    private TypeCode createTypeCodeForNull(org.omg.CORBA.ORB orb)
399    {
400        if (orb instanceof ORB) {
401
402            ORB ourORB = (ORB)orb;
403
404            // Preserve backwards compatibility with Kestrel and Ladybird
405            // by not fully implementing interop issue resolution 3857,
406            // and returning a null TypeCode with a tk_value TCKind.
407            // If we're not talking to Kestrel or Ladybird, fall through
408            // to the abstract interface case (also used for foreign ORBs).
409            if (!ORBVersionFactory.getFOREIGN().equals(ourORB.getORBVersion()) &&
410                ORBVersionFactory.getNEWER().compareTo(ourORB.getORBVersion()) > 0) {
411
412                return orb.get_primitive_tc(TCKind.tk_value);
413            }
414        }
415
416        // Use tk_abstract_interface as detailed in the resolution
417
418        // REVISIT: Define this in IDL and get the ID in generated code
419        String abstractBaseID = "IDL:omg.org/CORBA/AbstractBase:1.0";
420
421        return orb.create_abstract_interface_tc(abstractBaseID, "");
422    }
423
424    /**
425     * Reads a java.lang.Object as a CORBA any.
426     * @param in the stream from which to read the any.
427     * @return the object read from the stream.
428     */
429    public Object readAny(InputStream in)
430    {
431        Any any = in.read_any();
432        if ( any.type().kind().value() == TCKind._tk_objref )
433            return any.extract_Object ();
434        else
435            return any.extract_Value();
436    }
437
438    /**
439     * Writes a java.lang.Object as a CORBA Object. If <code>obj</code> is
440     * an exported RMI-IIOP server object, the tie is found
441     * and wired to <code>obj</code>, then written to <code>out.write_Object(org.omg.CORBA.Object)</code>.
442     * If <code>obj</code> is a CORBA Object, it is written to
443     * <code>out.write_Object(org.omg.CORBA.Object)</code>.
444     * @param out the stream in which to write the object.
445     * @param obj the object to write.
446     */
447    public void writeRemoteObject(OutputStream out, java.lang.Object obj)
448    {
449        // Make sure we have a connected object, then
450        // write it out...
451
452        Object newObj = Utility.autoConnect(obj,out.orb(),false);
453        out.write_Object((org.omg.CORBA.Object)newObj);
454    }
455
456    /**
457     * Writes a java.lang.Object as either a value or a CORBA Object.
458     * If <code>obj</code> is a value object or a stub object, it is written to
459     * <code>out.write_abstract_interface(java.lang.Object)</code>. If <code>obj</code> is an exported
460     * RMI-IIOP server object, the tie is found and wired to <code>obj</code>,
461     * then written to <code>out.write_abstract_interface(java.lang.Object)</code>.
462     * @param out the stream in which to write the object.
463     * @param obj the object to write.
464     */
465    public void writeAbstractObject( OutputStream out, java.lang.Object obj )
466    {
467        // Make sure we have a connected object, then
468        // write it out...
469
470        Object newObj = Utility.autoConnect(obj,out.orb(),false);
471        ((org.omg.CORBA_2_3.portable.OutputStream)out).write_abstract_interface(newObj);
472    }
473
474    /**
475     * Registers a target for a tie. Adds the tie to an internal table and calls
476     * {@link Tie#setTarget} on the tie object.
477     * @param tie the tie to register.
478     * @param target the target for the tie.
479     */
480    public void registerTarget(javax.rmi.CORBA.Tie tie, java.rmi.Remote target)
481    {
482        synchronized (exportedServants) {
483            // Do we already have this target registered?
484            if (lookupTie(target) == null) {
485                // No, so register it and set the target...
486                exportedServants.put(target,tie);
487                tie.setTarget(target);
488
489                // Do we need to instantiate our keep-alive thread?
490                if (keepAlive == null) {
491                    // Yes. Instantiate our keep-alive thread and start
492                    // it up...
493                    keepAlive = (KeepAlive)AccessController.doPrivileged(new PrivilegedAction() {
494                        public java.lang.Object run() {
495                            return new KeepAlive();
496                        }
497                    });
498                    keepAlive.start();
499                }
500            }
501        }
502    }
503
504    /**
505     * Removes the associated tie from an internal table and calls {@link Tie#deactivate}
506     * to deactivate the object.
507     * @param target the object to unexport.
508     */
509    public void unexportObject(java.rmi.Remote target)
510        throws java.rmi.NoSuchObjectException
511    {
512        synchronized (exportedServants) {
513            Tie cachedTie = lookupTie(target);
514            if (cachedTie != null) {
515                exportedServants.remove(target);
516                Utility.purgeStubForTie(cachedTie);
517                Utility.purgeTieAndServant(cachedTie);
518                try {
519                    cleanUpTie(cachedTie);
520                } catch (BAD_OPERATION e) {
521                    // ignore
522                } catch (org.omg.CORBA.OBJ_ADAPTER e) {
523                    // This can happen when the target was never associated with a POA.
524                    // We can safely ignore this case.
525                }
526
527                // Is it time to shut down our keep alive thread?
528                if (exportedServants.isEmpty()) {
529                    keepAlive.quit();
530                    keepAlive = null;
531                }
532            } else {
533                throw new java.rmi.NoSuchObjectException("Tie not found" );
534            }
535        }
536    }
537
538    protected void cleanUpTie(Tie cachedTie)
539        throws java.rmi.NoSuchObjectException
540    {
541        cachedTie.setTarget(null);
542        cachedTie.deactivate();
543    }
544
545    /**
546     * Returns the tie (if any) for a given target object.
547     * @return the tie or null if no tie is registered for the given target.
548     */
549    public Tie getTie (Remote target)
550    {
551        synchronized (exportedServants) {
552            return lookupTie(target);
553        }
554    }
555
556    /**
557     * An unsynchronized version of getTie() for internal use.
558     */
559    private static Tie lookupTie (Remote target)
560    {
561        Tie result = (Tie)exportedServants.get(target);
562        if (result == null && target instanceof Tie) {
563            if (exportedServants.contains(target)) {
564                result = (Tie)target;
565            }
566        }
567        return result;
568    }
569
570    /**
571     * Returns a singleton instance of a class that implements the
572     * {@link ValueHandler} interface.
573     * @return a class which implements the ValueHandler interface.
574     */
575    public ValueHandler createValueHandler()
576    {
577        return valueHandlerSingleton;
578    }
579
580    /**
581     * Returns the codebase, if any, for the given class.
582     * @param clz the class to get a codebase for.
583     * @return a space-separated list of URLs, or null.
584     */
585    public String getCodebase(java.lang.Class clz) {
586        return RMIClassLoader.getClassAnnotation(clz);
587    }
588
589    /**
590     * Returns a class instance for the specified class.
591     * @param className the name of the class.
592     * @param remoteCodebase a space-separated list of URLs at which
593     * the class might be found. May be null.
594     * @param loadingContext a class whose ClassLoader may be used to
595     * load the class if all other methods fail.
596     * @return the <code>Class</code> object representing the loaded class.
597     * @exception ClassNotFoundException if class cannot be loaded.
598     */
599    public Class loadClass( String className, String remoteCodebase,
600        ClassLoader loader) throws ClassNotFoundException
601    {
602        return JDKBridge.loadClass(className,remoteCodebase,loader);
603    }
604
605    /**
606     * The <tt>isLocal</tt> method has the same semantics as the
607     * ObjectImpl._is_local method, except that it can throw a RemoteException.
608     * (no it doesn't but the spec says it should.)
609     *
610     * The <tt>_is_local()</tt> method is provided so that stubs may determine
611     * if a particular object is implemented by a local servant and hence local
612     * invocation APIs may be used.
613     *
614     * @param stub the stub to test.
615     *
616     * @return The <tt>_is_local()</tt> method returns true if
617     * the servant incarnating the object is located in the same process as
618     * the stub and they both share the same ORB instance.  The <tt>_is_local()</tt>
619     * method returns false otherwise. The default behavior of <tt>_is_local()</tt> is
620     * to return false.
621     *
622     * @throws RemoteException The Java to IDL specification does to
623     * specify the conditions that cause a RemoteException to be thrown.
624     */
625    public boolean isLocal(javax.rmi.CORBA.Stub stub) throws RemoteException
626    {
627        boolean result = false ;
628
629        try {
630            org.omg.CORBA.portable.Delegate delegate = stub._get_delegate() ;
631            if (delegate instanceof CorbaClientDelegate) {
632                // For the Sun ORB
633                CorbaClientDelegate cdel = (CorbaClientDelegate)delegate ;
634                ContactInfoList cil = cdel.getContactInfoList() ;
635                if (cil instanceof CorbaContactInfoList) {
636                    CorbaContactInfoList ccil = (CorbaContactInfoList)cil ;
637                    LocalClientRequestDispatcher lcs = ccil.getLocalClientRequestDispatcher() ;
638                    result = lcs.useLocalInvocation( null ) ;
639                }
640            } else {
641                // For a non-Sun ORB
642                result = delegate.is_local( stub ) ;
643            }
644        } catch (SystemException e) {
645            throw javax.rmi.CORBA.Util.mapSystemException(e);
646        }
647
648        return result ;
649    }
650
651    /**
652     * Wraps an exception thrown by an implementation
653     * method.  It returns the corresponding client-side exception.
654     * @param orig the exception to wrap.
655     * @return the wrapped exception.
656     */
657    public RemoteException wrapException(Throwable orig)
658    {
659        if (orig instanceof SystemException) {
660            return mapSystemException((SystemException)orig);
661        }
662
663        if (orig instanceof Error) {
664            return new ServerError("Error occurred in server thread",(Error)orig);
665        } else if (orig instanceof RemoteException) {
666            return new ServerException("RemoteException occurred in server thread",
667                                       (Exception)orig);
668        } else if (orig instanceof RuntimeException) {
669            throw (RuntimeException) orig;
670        }
671
672        if (orig instanceof Exception)
673            return new UnexpectedException( orig.toString(), (Exception)orig );
674        else
675            return new UnexpectedException( orig.toString());
676    }
677
678    /**
679     * Copies or connects an array of objects. Used by local stubs
680     * to copy any number of actual parameters, preserving sharing
681     * across parameters as necessary to support RMI semantics.
682     * @param obj the objects to copy or connect.
683     * @param orb the ORB.
684     * @return the copied or connected objects.
685     * @exception RemoteException if any object could not be copied or connected.
686     */
687    public Object[] copyObjects (Object[] obj, org.omg.CORBA.ORB orb)
688        throws RemoteException
689    {
690        if (obj == null)
691            // Bug fix for 5018613: JCK test expects copyObjects to throw
692            // NPE when obj==null.  This is actually not in the spec, since
693            // obj is not really an RMI-IDL data type, but we follow our
694            // test here, and force this error to be thrown.
695            throw new NullPointerException() ;
696
697        Class compType = obj.getClass().getComponentType() ;
698        if (Remote.class.isAssignableFrom( compType ) && !compType.isInterface()) {
699            // obj is an array of remote impl types.  This
700            // causes problems with stream copier, so we copy
701            // it over to an array of Remotes instead.
702            Remote[] result = new Remote[obj.length] ;
703            System.arraycopy( (Object)obj, 0, (Object)result, 0, obj.length ) ;
704            return (Object[])copyObject( result, orb ) ;
705        } else
706            return (Object[])copyObject( obj, orb ) ;
707    }
708
709    /**
710     * Copies or connects an object. Used by local stubs to copy
711     * an actual parameter, result object, or exception.
712     * @param obj the object to copy.
713     * @param orb the ORB.
714     * @return the copy or connected object.
715     * @exception RemoteException if the object could not be copied or connected.
716     */
717    public Object copyObject (Object obj, org.omg.CORBA.ORB orb)
718        throws RemoteException
719    {
720        if (orb instanceof ORB) {
721            ORB lorb = (ORB)orb ;
722
723            try {
724                try {
725                    // This gets the copier for the current invocation, which was
726                    // previously set by preinvoke.
727                    return lorb.peekInvocationInfo().getCopierFactory().make().copy( obj ) ;
728                } catch (java.util.EmptyStackException exc) {
729                    // copyObject was invoked outside of an invocation, probably by
730                    // a test.  Get the default copier from the ORB.
731                    // XXX should we just make the default copier available directly
732                    // and avoid constructing one on each call?
733                    CopierManager cm = lorb.getCopierManager() ;
734                    ObjectCopier copier = cm.getDefaultObjectCopierFactory().make() ;
735                    return copier.copy( obj ) ;
736                }
737            } catch (ReflectiveCopyException exc) {
738                RemoteException rexc = new RemoteException() ;
739                rexc.initCause( exc ) ;
740                throw rexc ;
741            }
742        } else {
743            org.omg.CORBA_2_3.portable.OutputStream out =
744                (org.omg.CORBA_2_3.portable.OutputStream)orb.create_output_stream();
745            out.write_value((Serializable)obj);
746            org.omg.CORBA_2_3.portable.InputStream in =
747                (org.omg.CORBA_2_3.portable.InputStream)out.create_input_stream();
748            return in.read_value();
749        }
750    }
751}
752
753class KeepAlive extends Thread
754{
755    boolean quit = false;
756
757    public KeepAlive ()
758    {
759        setDaemon(false);
760    }
761
762    public synchronized void run ()
763    {
764        while (!quit) {
765            try {
766                wait();
767            } catch (InterruptedException e) {}
768        }
769    }
770
771    public synchronized void quit ()
772    {
773        quit = true;
774        notifyAll();
775    }
776}
777