1 /*
   2  * Copyright (c) 1999, 2014, 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 package com.sun.jndi.cosnaming;
  27 
  28 import javax.naming.*;
  29 import javax.naming.spi.StateFactory;
  30 import java.util.Hashtable;
  31 
  32 import org.omg.CORBA.ORB;
  33 
  34 import java.rmi.Remote;
  35 import java.rmi.server.ExportException;
  36 
  37 import com.sun.jndi.toolkit.corba.CorbaUtils;  // for RMI-IIOP
  38 
  39 /**
  40   * StateFactory that turns java.rmi.Remote objects to org.omg.CORBA.Object.
  41   *
  42   * @author Rosanna Lee
  43   */
  44 
  45 public class RemoteToCorba implements StateFactory {
  46     public RemoteToCorba() {
  47     }
  48 
  49     /**
  50      * Returns the CORBA object for a Remote object.
  51      * If input is not a Remote object, or if Remote object uses JRMP, return null.
  52      * If the RMI-IIOP library is not available, throw ConfigurationException.
  53      *
  54      * @param orig The object to turn into a CORBA object. If not Remote,
  55      *             or if is a JRMP stub or impl, return null.
  56      * @param name Ignored
  57      * @param ctx The non-null CNCtx whose ORB to use.
  58      * @param env Ignored
  59      * @return The CORBA object for <tt>orig</tt> or null.
  60      * @exception ConfigurationException If the CORBA object cannot be obtained
  61      *    due to configuration problems, for instance, if RMI-IIOP not available.
  62      * @exception NamingException If some other problem prevented a CORBA
  63      *    object from being obtained from the Remote object.
  64      */
  65     public Object getStateToBind(Object orig, Name name, Context ctx,
  66         Hashtable<?,?> env) throws NamingException {
  67         if (orig instanceof org.omg.CORBA.Object) {
  68             // Already a CORBA object, just use it
  69             return null;
  70         }
  71 
  72         if (orig instanceof Remote) {
  73             // Turn remote object into org.omg.CORBA.Object
  74             // Returns null if JRMP; let next factory try
  75             // CNCtx will eventually throw IllegalArgumentException if
  76             // no CORBA object gotten
  77             return CorbaUtils.remoteToCorba((Remote)orig, ((CNCtx)ctx)._orb);
  78         }
  79         return null; // pass and let next state factory try
  80     }
  81 }