RemoteToCorba.java revision 820:9205e980062a
1226586Sdim/*
2226586Sdim * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
3226586Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4226586Sdim *
5226586Sdim * This code is free software; you can redistribute it and/or modify it
6226586Sdim * under the terms of the GNU General Public License version 2 only, as
7226586Sdim * published by the Free Software Foundation.  Oracle designates this
8226586Sdim * particular file as subject to the "Classpath" exception as provided
9226586Sdim * by Oracle in the LICENSE file that accompanied this code.
10226586Sdim *
11226586Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
12226586Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13226586Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14226586Sdim * version 2 for more details (a copy is included in the LICENSE file that
15226586Sdim * accompanied this code).
16226586Sdim *
17226586Sdim * You should have received a copy of the GNU General Public License version
18249423Sdim * 2 along with this work; if not, write to the Free Software Foundation,
19249423Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20226586Sdim *
21226586Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22249423Sdim * or visit www.oracle.com if you need additional information or have any
23226586Sdim * questions.
24249423Sdim */
25226586Sdim
26226586Sdimpackage com.sun.jndi.cosnaming;
27226586Sdim
28226586Sdimimport javax.naming.*;
29226586Sdimimport javax.naming.spi.StateFactory;
30226586Sdimimport java.util.Hashtable;
31226586Sdim
32226586Sdimimport org.omg.CORBA.ORB;
33226586Sdim
34226586Sdimimport java.rmi.Remote;
35226586Sdimimport java.rmi.server.ExportException;
36226586Sdim
37226586Sdimimport com.sun.jndi.toolkit.corba.CorbaUtils;  // for RMI-IIOP
38226586Sdim
39226586Sdim/**
40226586Sdim  * StateFactory that turns java.rmi.Remote objects to org.omg.CORBA.Object.
41226586Sdim  *
42226586Sdim  * @author Rosanna Lee
43226586Sdim  */
44226586Sdim
45226586Sdimpublic class RemoteToCorba implements StateFactory {
46226586Sdim    public RemoteToCorba() {
47226586Sdim    }
48226586Sdim
49226586Sdim    /**
50226586Sdim     * Returns the CORBA object for a Remote object.
51226586Sdim     * If input is not a Remote object, or if Remote object uses JRMP, return null.
52226586Sdim     * If the RMI-IIOP library is not available, throw ConfigurationException.
53234353Sdim     *
54226586Sdim     * @param orig The object to turn into a CORBA object. If not Remote,
55226586Sdim     *             or if is a JRMP stub or impl, return null.
56226586Sdim     * @param name Ignored
57226586Sdim     * @param ctx The non-null CNCtx whose ORB to use.
58226586Sdim     * @param env Ignored
59226586Sdim     * @return The CORBA object for {@code orig} or null.
60226586Sdim     * @exception ConfigurationException If the CORBA object cannot be obtained
61226586Sdim     *    due to configuration problems, for instance, if RMI-IIOP not available.
62226586Sdim     * @exception NamingException If some other problem prevented a CORBA
63226586Sdim     *    object from being obtained from the Remote object.
64226586Sdim     */
65226586Sdim    public Object getStateToBind(Object orig, Name name, Context ctx,
66226586Sdim        Hashtable<?,?> env) throws NamingException {
67226586Sdim        if (orig instanceof org.omg.CORBA.Object) {
68226586Sdim            // Already a CORBA object, just use it
69226586Sdim            return null;
70226586Sdim        }
71234353Sdim
72226586Sdim        if (orig instanceof Remote) {
73226586Sdim            // Turn remote object into org.omg.CORBA.Object
74226586Sdim            // Returns null if JRMP; let next factory try
75226586Sdim            // CNCtx will eventually throw IllegalArgumentException if
76226586Sdim            // no CORBA object gotten
77226586Sdim            return CorbaUtils.remoteToCorba((Remote)orig, ((CNCtx)ctx)._orb);
78226586Sdim        }
79226586Sdim        return null; // pass and let next state factory try
80226586Sdim    }
81234353Sdim}
82226586Sdim