Source for javax.rmi.CORBA.Util

   1: /* Util.java --
   2:    Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.rmi.CORBA;
  40: 
  41: import org.omg.CORBA.Any;
  42: import org.omg.CORBA.BAD_PARAM;
  43: import org.omg.CORBA.COMM_FAILURE;
  44: import org.omg.CORBA.CompletionStatus;
  45: import org.omg.CORBA.INVALID_TRANSACTION;
  46: import org.omg.CORBA.INV_OBJREF;
  47: import org.omg.CORBA.MARSHAL;
  48: import org.omg.CORBA.NO_PERMISSION;
  49: import org.omg.CORBA.OBJECT_NOT_EXIST;
  50: import org.omg.CORBA.OMGVMCID;
  51: import org.omg.CORBA.ORB;
  52: import org.omg.CORBA.SystemException;
  53: import org.omg.CORBA.TRANSACTION_REQUIRED;
  54: import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
  55: import org.omg.CORBA.TypeCode;
  56: import org.omg.CORBA.portable.InputStream;
  57: import org.omg.CORBA.portable.OutputStream;
  58: 
  59: import gnu.javax.rmi.CORBA.DelegateFactory;
  60: 
  61: import java.rmi.AccessException;
  62: import java.rmi.MarshalException;
  63: import java.rmi.NoSuchObjectException;
  64: import java.rmi.Remote;
  65: import java.rmi.RemoteException;
  66: import java.rmi.ServerError;
  67: import java.rmi.ServerException;
  68: import java.rmi.UnexpectedException;
  69: import java.rmi.server.RMIClassLoader;
  70: 
  71: import javax.transaction.InvalidTransactionException;
  72: import javax.transaction.TransactionRequiredException;
  73: import javax.transaction.TransactionRolledbackException;
  74: 
  75: /**
  76:  * Provides utility methods used by stubs and ties to perform common operations.
  77:  * The functionality is forwarded to the enclosed UtilDelegate. This delegate
  78:  * can be altered by setting the system property "javax.rmi.CORBA.UtilClass" to
  79:  * the name of the alternative class that must implement {@link UtilDelegate}.
  80:  *
  81:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  82:  */
  83: public class Util
  84: {
  85:   /**
  86:    * The delegate, responsible for all functionality.
  87:    */
  88:   static UtilDelegate delegate = (UtilDelegate) DelegateFactory.getInstance(DelegateFactory.UTIL);
  89: 
  90:   /**
  91:    * Prevents this class from being instantiated.
  92:    */
  93:   private Util()
  94:   {
  95:   }
  96: 
  97:   /**
  98:    * Used by local stubs to create a copy of the object. The object must be
  99:    * Serializable for this operation to succeed. Strings are not copied and
 100:    * 1D-3D string arrays are only cloned.
 101:    */
 102:   public static java.lang.Object copyObject(java.lang.Object object, ORB orb)
 103:     throws RemoteException
 104:   {
 105:     return delegate.copyObject(object, orb);
 106:   }
 107: 
 108:   /**
 109:    * Used by local stubs to create a multiple copies of the object, preserving
 110:    * sharing accross the parameters if necessary.
 111:    */
 112:   public static java.lang.Object[] copyObjects(java.lang.Object[] object,
 113:     ORB orb)
 114:     throws RemoteException
 115:   {
 116:     return delegate.copyObjects(object, orb);
 117:   }
 118: 
 119:   /**
 120:    * Get the value handler that Serializes Java objects to and from CDR (GIOP)
 121:    * streams.
 122:    *
 123:    * When using the default Util implementation, the class of the returned
 124:    * handler can be altered by setting by setting the system property
 125:    * "javax.rmi.CORBA.ValueHandlerClass" to the name of the alternative class
 126:    * that must implement {@link ValueHandler}.
 127:    */
 128:   public static ValueHandler createValueHandler()
 129:   {
 130:     return delegate.createValueHandler();
 131:   }
 132: 
 133:   /**
 134:    * This call is finally delegated to {@link RMIClassLoader#getClassAnnotation};
 135:    */
 136:   public static String getCodebase(Class clz)
 137:   {
 138:     return delegate.getCodebase(clz);
 139:   }
 140: 
 141:   /**
 142:    * Get the Tie that handles invocations on the given target. If the target/Tie
 143:    * pair has not been previously registered using {@link #registerTarget},
 144:    * this method tries to locate a tie class by the name pattern. If this
 145:    * succeeds, the tie-target pair is also registered.
 146:    *
 147:    * @return the Tie.
 148:    */
 149:   public static Tie getTie(Remote target)
 150:   {
 151:     return delegate.getTie(target);
 152:   }
 153: 
 154:   /**
 155:    * Checks if the given stub is local. The implementation it delegates call to
 156:    * {@link ObjectImpl#_is_local().
 157:    *
 158:    * @param stub a stub to check.
 159:    * @return true if the stub is local, false otherwise.
 160:    *
 161:    * @throws RemoteException if the {@link ObjectImpl#_is_local()} throws a
 162:    * {@link org.omg.CORBA.SystemException}.
 163:    */
 164:   public static boolean isLocal(Stub stub)
 165:     throws RemoteException
 166:   {
 167:     return delegate.isLocal(stub);
 168:   }
 169: 
 170:   /**
 171:    * Load the class. The method uses class loaders from the call stact first. If
 172:    * this fails, the further behaviour depends on the System Property
 173:    * "java.rmi.server.useCodebaseOnly" with default value "false".
 174:    *
 175:    * <ul>
 176:    * <li>If remoteCodebase is non-null and useCodebaseOnly is "false" then call
 177:    * java.rmi.server.RMIClassLoader.loadClass (remoteCodebase, className)</li>
 178:    * <li> If remoteCodebase is null or useCodebaseOnly is true then call
 179:    * java.rmi.server.RMIClassLoader.loadClass(className)</li>
 180:    * <li>If a class is still not successfully loaded and the loader != null
 181:    * then try Class.forName(className, false, loader). </li>
 182:    * </ul>
 183:    *
 184:    * @param className the name of the class.
 185:    * @param remoteCodebase the codebase.
 186:    * @param loader the class loader.
 187:    * @return the loaded class.
 188:    *
 189:    * @throws ClassNotFoundException of the class cannot be loaded.
 190:    */
 191:   public static Class loadClass(String className, String remoteCodebase,
 192:     ClassLoader loader)
 193:     throws ClassNotFoundException
 194:   {
 195:     return delegate.loadClass(className, remoteCodebase, loader);
 196:   }
 197: 
 198:   /**
 199:    * Converts CORBA {@link SystemException} into RMI {@link RemoteException}.
 200:    * The exception is converted as defined in the following table:
 201:    * <p>
 202:    * <table border = "1">
 203:    * <tr>
 204:    * <th>CORBA Exception</th>
 205:    * <th>RMI Exception</th>
 206:    * </tr>
 207:    * <tr>
 208:    * <td>{@link COMM_FAILURE}</td>
 209:    * <td>{@link MarshalException}</td>
 210:    * </tr>
 211:    * <tr>
 212:    * <td>{@link INV_OBJREF}</td>
 213:    * <td>{@link  NoSuchObjectException}</td>
 214:    * </tr>
 215:    * <tr>
 216:    * <td>{@link NO_PERMISSION}</td>
 217:    * <td>{@link  AccessException}</td>
 218:    * </tr>
 219:    * <tr>
 220:    * <td>{@link MARSHAL}</td>
 221:    * <td>{@link  MarshalException}</td>
 222:    * </tr>
 223:    * <tr>
 224:    * <td>{@link BAD_PARAM} (all other cases)</td>
 225:    * <td>{@link  MarshalException}</td>
 226:    * </tr>
 227:    * <tr>
 228:    * <td>{@link OBJECT_NOT_EXIST}</td>
 229:    * <td>{@link  NoSuchObjectException}</td>
 230:    * </tr>
 231:    * <tr>
 232:    * <td>{@link TRANSACTION_REQUIRED}</td>
 233:    * <td>{@link  TransactionRequiredException}</td>
 234:    * </tr>
 235:    * <tr>
 236:    * <td>{@link TRANSACTION_ROLLEDBACK}</td>
 237:    * <td>{@link  TransactionRolledbackException}</td>
 238:    * </tr>
 239:    * <tr>
 240:    * <td>{@link INVALID_TRANSACTION}</td>
 241:    * <td>{@link  InvalidTransactionException}</td>
 242:    * </tr>
 243:    * <tr>
 244:    * <td bgcolor="lightgray">Any other {@link SystemException}</td>
 245:    * <td bgcolor="lightgray">{@link RemoteException}</td>
 246:    * </tr>
 247:    * </table>
 248:    * </p>
 249:    * <p>
 250:    * The exception detailed message always consists of
 251:    * <ol>
 252:    * <li>the string "CORBA "</li>
 253:    * <li>the CORBA name of the system exception</li>
 254:    * <li>single space</li>
 255:    * <li>the hexadecimal value of the system exception's minor code, preceeded
 256:    * by 0x (higher bits contain {@link OMGVMCID}).</li>
 257:    * <li>single space</li>
 258:    * <li>the {@link CompletionStatus} of the exception: "Yes", "No" or "Maybe".</li>
 259:    * </ol>
 260:    * The subsequent content is not part of the official RMI-IIOP standart and is
 261:    * added for compatibility with Sun's implementation:
 262:    * <ol>
 263:    * <li>the phrase "<code>; nested exception is: <i>(line feed)(tab)</i></code>"</li>
 264:    * <li>the full name of the mapped SystemException, as returned by
 265:    * Class.getName().</li>
 266:    * <li>the ": ".
 267:    * <li>the value, returned by .getMessage() of the passed parameter.</li>
 268:    * </ol>
 269:    * <p>
 270:    * For instance, if the Internet connection was refused:
 271:    * </p><p>
 272:    * <code>CORBA COMM_FAILURE 0x535500C9 No</code>
 273:    * </p><p>
 274:    * The original CORBA exception is set as the cause of the RemoteException
 275:    * being created.
 276:    * </p>
 277:    */
 278:   public static RemoteException mapSystemException(SystemException ex)
 279:   {
 280:     return delegate.mapSystemException(ex);
 281:   }
 282: 
 283:   /**
 284:    * Register the Tie-target pair. As the Tie is a Servant, it can potentially
 285:    * be connected to several objects and hence may be registered with several
 286:    * targets.
 287:    */
 288:   public static void registerTarget(Tie tie, Remote target)
 289:   {
 290:     delegate.registerTarget(tie, target);
 291:   }
 292: 
 293:   /**
 294:    * Deactivate the associated Tie, if it is found and is not connected to other
 295:    * registered targets. Independing from the POA policies, the transparent
 296:    * reactivation will not be possible.
 297:    */
 298:   public static void unexportObject(Remote target)
 299:     throws NoSuchObjectException
 300:   {
 301:     delegate.unexportObject(target);
 302:   }
 303: 
 304:   /**
 305:    * Converts the exception that was thrown by the implementation method on a
 306:    * server side into RemoteException that can be transferred and re-thrown on a
 307:    * client side. The method converts exceptions as defined in the following
 308:    * table: <table border = "1">
 309:    * <tr>
 310:    * <th>Exception to map (or subclass)</th>
 311:    * <th>Maps into</th>
 312:    * </tr>
 313:    * <tr>
 314:    * <td>{@link Error}</td>
 315:    * <td>{@link ServerError}</td>
 316:    * </tr>
 317:    * <tr>
 318:    * <td>{@link RemoteException}</td>
 319:    * <td>{@link ServerException}</td>
 320:    * </tr>
 321:    * <tr>
 322:    * <td>{@link SystemException}</td>
 323:    * <td>wrapException({@link #mapSystemException})</td>
 324:    * </tr>
 325:    * <tr>
 326:    * <td>{@link RuntimeException}</td>
 327:    * <td><b>rethrows</b></td>
 328:    * </tr>
 329:    * <tr>
 330:    * <td>Any other exception</td>
 331:    * <td>{@link UnexpectedException}</td>
 332:    * </tr>
 333:    * </table>
 334:    *
 335:    * @param exception an exception that was thrown on a server side implementation.
 336:    *
 337:    * @return the corresponding RemoteException unless it is a RuntimeException.
 338:    *
 339:    * @throws RuntimeException the passed exception if it is an instance of
 340:    * RuntimeException.
 341:    *
 342:    * @specnote It is the same behavior, as in Suns implementations 1.4.0-1.5.0.
 343:    */
 344:   public static RemoteException wrapException(Throwable exception)
 345:   {
 346:     return delegate.wrapException(exception);
 347:   }
 348: 
 349:   /**
 350:    * Write abstract interface to the CORBA output stream. The write format is
 351:    * matching CORBA abstract interface. Remotes and CORBA objects are written as
 352:    * objects, other classes are supposed to be value types and are written as
 353:    * such. {@link Remote}s are processed as defined in
 354:    * {@link #writeRemoteObject}. The written data contains discriminator,
 355:    * defining, that was written. Another method that writes the same content is
 356:    * {@link org.omg.CORBA_2_3.portable.OutputStream#write_abstract_interface(java.lang.Object)}.
 357:    *
 358:    * @param output a stream to write to, must be
 359:    * {@link org.omg.CORBA_2_3.portable.OutputStream}.
 360:    *
 361:    * @param object an object to write, must be CORBA object, Remote
 362:    */
 363:   public static void writeAbstractObject(OutputStream output,
 364:     java.lang.Object object)
 365:   {
 366:     delegate.writeAbstractObject(output, object);
 367:   }
 368: 
 369:   /**
 370:    * Write the passed java object to the output stream in the form of the CORBA
 371:    * {@link Any}. This includes creating an writing the object {@link TypeCode}
 372:    * first. Such Any can be later read by a non-RMI-IIOP CORBA implementation
 373:    * and manipulated, for instance, by means, provided in
 374:    * {@link org.omg.DynamicAny.DynAny}. Depending from the passed value, this
 375:    * method writes CORBA object, value type or value box. For value types Null
 376:    * is written with the abstract interface, its typecode having repository id
 377:    * "IDL:omg.org/CORBA/AbstractBase:1.0" and the empty string name.
 378:    *
 379:    * @param output the object to write.
 380:    * @param object the java object that must be written in the form of the CORBA
 381:    * {@link Any}.
 382:    */
 383:   public static void writeAny(OutputStream output, java.lang.Object object)
 384:   {
 385:     delegate.writeAny(output, object);
 386:   }
 387: 
 388:   /**
 389:    * Read Any from the input stream.
 390:    */
 391:   public static java.lang.Object readAny(InputStream input)
 392:   {
 393:     return delegate.readAny(input);
 394:   }
 395: 
 396:   /**
 397:    * Write the passed parameter to the output stream as CORBA object. If the
 398:    * parameter is an instance of Remote and not an instance of Stub, the method
 399:    * instantiates a suitable Tie, connects the parameter to this Tie and then
 400:    * connects that Tie to the ORB that is requested from the output stream. Then
 401:    * the object reference is written to the stream, making remote invocations
 402:    * possible (the ORB is started and activated, if required). This method is
 403:    * used in write_value(..) method group in
 404:    * {@link org.omg.CORBA_2_3.portable.OutputStream} and also may be called
 405:    * directly from generated Stubs and Ties.
 406:    *
 407:    * @param output a stream to write to, must be
 408:    * org.omg.CORBA_2_3.portable.OutputStream
 409:    * @param object an object to write.
 410:    */
 411:   public static void writeRemoteObject(OutputStream output,
 412:     java.lang.Object object)
 413:   {
 414:     delegate.writeRemoteObject(output, object);
 415:   }
 416: }