corba/src/share/classes/com/sun/corba/se/impl/presentation/rmi/JNDIStateFactoryImpl.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
package com.sun.corba.se.impl.presentation.rmi ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Field ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import javax.naming.*;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
import javax.naming.spi.StateFactory;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
import javax.rmi.PortableRemoteObject ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
import com.sun.corba.se.spi.orb.ORB;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
import java.rmi.Remote;
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
import java.rmi.server.ExportException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
// XXX This creates a dependendcy on the implementation
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
// of the CosNaming service provider.
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
import com.sun.jndi.cosnaming.CNCtx ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
  * StateFactory that turns java.rmi.Remote objects to org.omg.CORBA.Object.
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
  * This version works either with standard RMI-IIOP or Dynamic RMI-IIOP.
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
  * Based on the original com.sun.jndi.cosnaming.RemoteToCorba and
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
  * com.sun.jndi.toolkit.corba.CorbaUtils.
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
  *
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
  * @author Ken Cavanaugh
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
  */
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
public class JNDIStateFactoryImpl implements StateFactory
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
    private static final Field orbField ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
    static {
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
        orbField = (Field) AccessController.doPrivileged(
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
            new PrivilegedAction() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
                public Object run() {
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
                    Field fld = null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
                    try {
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
                        Class cls = CNCtx.class ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
                        fld = cls.getDeclaredField( "_orb" ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
                        fld.setAccessible( true ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
                    } catch (Exception exc) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
                        // XXX log exception at FINE
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
                    return fld ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
        ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
    public JNDIStateFactoryImpl()
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
     * Returns the CORBA object for a Remote object.
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
     * If input is not a Remote object, or if Remote object uses JRMP, return null.
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
     * If the RMI-IIOP library is not available, throw ConfigurationException.
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
     * @param orig The object to turn into a CORBA object. If not Remote,
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
     *             or if is a JRMP stub or impl, return null.
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
     * @param name Ignored
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
     * @param ctx The non-null CNCtx whose ORB to use.
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
     * @param env Ignored
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
     * @return The CORBA object for <tt>orig</tt> or null.
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
     * @exception ConfigurationException If the CORBA object cannot be obtained
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
     *    due to configuration problems
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
     * @exception NamingException If some other problem prevented a CORBA
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
     *    object from being obtained from the Remote object.
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
    public Object getStateToBind(Object orig, Name name, Context ctx,
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
        Hashtable<?,?> env) throws NamingException
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
        if (orig instanceof org.omg.CORBA.Object)
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
            return orig ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
        if (!(orig instanceof Remote))
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
            // Not for this StateFactory
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
            return null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
        ORB orb = getORB( ctx ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
        if (orb == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
            // Wrong kind of context, so just give up and let another StateFactory
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
            // try to satisfy getStateToBind.
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
            return null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
        Remote stub = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
            stub = PortableRemoteObject.toStub( (Remote)orig ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
        } catch (Exception exc) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
            // XXX log at FINE level?
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
            // Wrong sort of object: just return null to allow another StateFactory
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
            // to handle this.  This can happen easily because this StateFactory
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
            // is specified for the application, not the service context provider.
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
            return null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
        if (StubAdapter.isStub( stub )) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
                StubAdapter.connect( stub, orb ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
            } catch (Exception exc) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
                if (!(exc instanceof java.rmi.RemoteException)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
                    // XXX log at FINE level?
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
                    // Wrong sort of object: just return null to allow another StateFactory
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
                    // to handle this call.
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
                    return null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
                // ignore RemoteException because stub might have already
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
                // been connected
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
        return stub ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
    // This is necessary because the _orb field is package private in
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
    // com.sun.jndi.cosnaming.CNCtx.  This is not an ideal solution.
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
    // The best solution for our ORB is to change the CosNaming provider
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
    // to use the StubAdapter.  But this has problems as well, because
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
    // other vendors may use the CosNaming provider with a different ORB
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
    // entirely.
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
    private ORB getORB( Context ctx )
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
        ORB orb = null ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
            orb = (ORB)orbField.get( ctx ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
        } catch (Exception exc) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
            // XXX log this exception at FINE level
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
            // ignore the exception and return null.
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
            // Note that the exception may be because ctx
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
            // is not a CosNaming context.
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
        return orb ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
}