jdk/src/share/classes/javax/management/remote/rmi/RMIIIOPServerImpl.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 2 90ce3da70b43
child 4167 76f44f2d5d4d
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management.remote.rmi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.rmi.PortableRemoteObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>An {@link RMIServerImpl} that is exported through IIOP and that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * creates client connections as RMI objects exported through IIOP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * User code does not usually reference this class directly.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see RMIServerImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class RMIIIOPServerImpl extends RMIServerImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * <p>Creates a new {@link RMIServerImpl}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @param env the environment containing attributes for the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * <code>RMIServerImpl</code>.  Can be null, which is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * to an empty Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @exception IOException if the RMI object cannot be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public RMIIIOPServerImpl(Map<String,?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        super(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.env = (env == null) ? Collections.<String, Object>emptyMap() : env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        callerACC = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected void export() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        PortableRemoteObject.exportObject(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected String getProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return "iiop";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <p>Returns an IIOP stub.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * The stub might not yet be connected to the ORB. The stub will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * be serializable only if it is connected to the ORB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @return an IIOP stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @exception IOException if the stub cannot be created - e.g the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *            RMIIIOPServerImpl has not been exported yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public Remote toStub() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // javax.rmi.CORBA.Stub stub =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        //    (javax.rmi.CORBA.Stub) PortableRemoteObject.toStub(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        final Remote stub = PortableRemoteObject.toStub(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // java.lang.System.out.println("NON CONNECTED STUB " + stub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // org.omg.CORBA.ORB orb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        //    org.omg.CORBA.ORB.init((String[])null, (Properties)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // stub.connect(orb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // java.lang.System.out.println("CONNECTED STUB " + stub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return stub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>Creates a new client connection as an RMI object exported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * through IIOP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param connectionId the ID of the new connection.  Every
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * connection opened by this connector server will have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * different ID.  The behavior is unspecified if this parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param subject the authenticated subject.  Can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @return the newly-created <code>RMIConnection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @exception IOException if the new client object cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * created or exported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected RMIConnection makeClient(String connectionId, Subject subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (connectionId == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new NullPointerException("Null connectionId");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        RMIConnection client =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            new RMIConnectionImpl(this, connectionId, getDefaultClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                  subject, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        PortableRemoteObject.exportObject(client);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected void closeClient(RMIConnection client) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        PortableRemoteObject.unexportObject(client);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <p>Called by {@link #close()} to close the connector server by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * unexporting this object.  After returning from this method, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * connector server must not accept any new connections.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @exception IOException if the attempt to close the connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * server failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected void closeServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        PortableRemoteObject.unexportObject(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    RMIConnection doNewClient(final Object credentials) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (callerACC == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new SecurityException("AccessControlContext cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                new PrivilegedExceptionAction<RMIConnection>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    public RMIConnection run() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        return superDoNewClient(credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }, callerACC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw (IOException) pae.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    RMIConnection superDoNewClient(Object credentials) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return super.doNewClient(credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private final Map<String, ?> env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private final AccessControlContext callerACC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}