J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.management.remote.rmi; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.rmi.Remote; |
| 30 | import java.security.AccessControlContext; |
| 31 | import java.security.AccessController; |
| 32 | import java.security.PrivilegedActionException; |
| 33 | import java.security.PrivilegedExceptionAction; |
| 34 | import java.util.Map; |
| 35 | import java.util.Collections; |
| 36 | import javax.rmi.PortableRemoteObject; |
| 37 | import javax.security.auth.Subject; |
| 38 | |
| 39 | /** |
| 40 | * <p>An {@link RMIServerImpl} that is exported through IIOP and that |
| 41 | * creates client connections as RMI objects exported through IIOP. |
| 42 | * User code does not usually reference this class directly.</p> |
| 43 | * |
| 44 | * @see RMIServerImpl |
| 45 | * |
| 46 | * @since 1.5 |
| 47 | */ |
| 48 | public class RMIIIOPServerImpl extends RMIServerImpl { |
| 49 | /** |
| 50 | * <p>Creates a new {@link RMIServerImpl}.</p> |
| 51 | * |
| 52 | * @param env the environment containing attributes for the new |
| 53 | * <code>RMIServerImpl</code>. Can be null, which is equivalent |
| 54 | * to an empty Map. |
| 55 | * |
| 56 | * @exception IOException if the RMI object cannot be created. |
| 57 | */ |
| 58 | public RMIIIOPServerImpl(Map<String,?> env) |
| 59 | throws IOException { |
| 60 | super(env); |
| 61 | |
| 62 | this.env = (env == null) ? Collections.<String, Object>emptyMap() : env; |
| 63 | |
| 64 | callerACC = AccessController.getContext(); |
| 65 | } |
| 66 | |
| 67 | protected void export() throws IOException { |
| 68 | PortableRemoteObject.exportObject(this); |
| 69 | } |
| 70 | |
| 71 | protected String getProtocol() { |
| 72 | return "iiop"; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * <p>Returns an IIOP stub.</p> |
| 77 | * The stub might not yet be connected to the ORB. The stub will |
| 78 | * be serializable only if it is connected to the ORB. |
| 79 | * @return an IIOP stub. |
| 80 | * @exception IOException if the stub cannot be created - e.g the |
| 81 | * RMIIIOPServerImpl has not been exported yet. |
| 82 | **/ |
| 83 | public Remote toStub() throws IOException { |
| 84 | // javax.rmi.CORBA.Stub stub = |
| 85 | // (javax.rmi.CORBA.Stub) PortableRemoteObject.toStub(this); |
| 86 | final Remote stub = PortableRemoteObject.toStub(this); |
| 87 | // java.lang.System.out.println("NON CONNECTED STUB " + stub); |
| 88 | // org.omg.CORBA.ORB orb = |
| 89 | // org.omg.CORBA.ORB.init((String[])null, (Properties)null); |
| 90 | // stub.connect(orb); |
| 91 | // java.lang.System.out.println("CONNECTED STUB " + stub); |
| 92 | return stub; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * <p>Creates a new client connection as an RMI object exported |
| 97 | * through IIOP. |
| 98 | * |
| 99 | * @param connectionId the ID of the new connection. Every |
| 100 | * connection opened by this connector server will have a |
| 101 | * different ID. The behavior is unspecified if this parameter is |
| 102 | * null. |
| 103 | * |
| 104 | * @param subject the authenticated subject. Can be null. |
| 105 | * |
| 106 | * @return the newly-created <code>RMIConnection</code>. |
| 107 | * |
| 108 | * @exception IOException if the new client object cannot be |
| 109 | * created or exported. |
| 110 | */ |
| 111 | protected RMIConnection makeClient(String connectionId, Subject subject) |
| 112 | throws IOException { |
| 113 | |
| 114 | if (connectionId == null) |
| 115 | throw new NullPointerException("Null connectionId"); |
| 116 | |
| 117 | RMIConnection client = |
| 118 | new RMIConnectionImpl(this, connectionId, getDefaultClassLoader(), |
| 119 | subject, env); |
| 120 | PortableRemoteObject.exportObject(client); |
| 121 | return client; |
| 122 | } |
| 123 | |
| 124 | protected void closeClient(RMIConnection client) throws IOException { |
| 125 | PortableRemoteObject.unexportObject(client); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * <p>Called by {@link #close()} to close the connector server by |
| 130 | * unexporting this object. After returning from this method, the |
| 131 | * connector server must not accept any new connections.</p> |
| 132 | * |
| 133 | * @exception IOException if the attempt to close the connector |
| 134 | * server failed. |
| 135 | */ |
| 136 | protected void closeServer() throws IOException { |
| 137 | PortableRemoteObject.unexportObject(this); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | RMIConnection doNewClient(final Object credentials) throws IOException { |
| 142 | if (callerACC == null) { |
| 143 | throw new SecurityException("AccessControlContext cannot be null"); |
| 144 | } |
| 145 | try { |
| 146 | return AccessController.doPrivileged( |
| 147 | new PrivilegedExceptionAction<RMIConnection>() { |
| 148 | public RMIConnection run() throws IOException { |
| 149 | return superDoNewClient(credentials); |
| 150 | } |
| 151 | }, callerACC); |
| 152 | } catch (PrivilegedActionException pae) { |
| 153 | throw (IOException) pae.getCause(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | RMIConnection superDoNewClient(Object credentials) throws IOException { |
| 158 | return super.doNewClient(credentials); |
| 159 | } |
| 160 | |
| 161 | private final Map<String, ?> env; |
| 162 | private final AccessControlContext callerACC; |
| 163 | } |