0N/A/*
2362N/A * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.management.remote.rmi;
0N/A
0N/Aimport com.sun.jmx.remote.internal.ArrayNotificationBuffer;
0N/Aimport com.sun.jmx.remote.internal.NotificationBuffer;
0N/Aimport com.sun.jmx.remote.security.JMXPluggableAuthenticator;
0N/Aimport com.sun.jmx.remote.util.ClassLogger;
0N/A
0N/Aimport java.io.Closeable;
0N/Aimport java.io.IOException;
0N/Aimport java.lang.ref.WeakReference;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.server.RemoteServer;
0N/Aimport java.rmi.server.ServerNotActiveException;
0N/Aimport java.security.Principal;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collections;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.Set;
0N/A
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.remote.JMXAuthenticator;
0N/Aimport javax.management.remote.JMXConnectorServer;
0N/Aimport javax.security.auth.Subject;
0N/A
0N/A/**
0N/A * <p>An RMI object representing a connector server. Remote clients
0N/A * can make connections using the {@link #newClient(Object)} method. This
0N/A * method returns an RMI object representing the connection.</p>
0N/A *
0N/A * <p>User code does not usually reference this class directly.
0N/A * RMI connection servers are usually created with the class {@link
0N/A * RMIConnectorServer}. Remote clients usually create connections
0N/A * either with {@link javax.management.remote.JMXConnectorFactory}
0N/A * or by instantiating {@link RMIConnector}.</p>
0N/A *
0N/A * <p>This is an abstract class. Concrete subclasses define the
0N/A * details of the client connection objects, such as whether they use
0N/A * JRMP or IIOP.</p>
0N/A *
0N/A * @since 1.5
0N/A */
0N/Apublic abstract class RMIServerImpl implements Closeable, RMIServer {
0N/A /**
0N/A * <p>Constructs a new <code>RMIServerImpl</code>.</p>
0N/A *
0N/A * @param env the environment containing attributes for the new
0N/A * <code>RMIServerImpl</code>. Can be null, which is equivalent
0N/A * to an empty Map.
0N/A */
0N/A public RMIServerImpl(Map<String,?> env) {
686N/A this.env = (env == null) ? Collections.<String,Object>emptyMap() : env;
0N/A }
0N/A
0N/A void setRMIConnectorServer(RMIConnectorServer connServer)
0N/A throws IOException {
0N/A this.connServer = connServer;
0N/A }
0N/A
0N/A /**
0N/A * <p>Exports this RMI object.</p>
0N/A *
0N/A * @exception IOException if this RMI object cannot be exported.
0N/A */
0N/A protected abstract void export() throws IOException;
0N/A
0N/A /**
0N/A * Returns a remotable stub for this server object.
0N/A * @return a remotable stub.
0N/A * @exception IOException if the stub cannot be obtained - e.g the
0N/A * RMIServerImpl has not been exported yet.
0N/A **/
0N/A public abstract Remote toStub() throws IOException;
0N/A
0N/A /**
0N/A * <p>Sets the default <code>ClassLoader</code> for this connector
0N/A * server. New client connections will use this classloader.
0N/A * Existing client connections are unaffected.</p>
0N/A *
0N/A * @param cl the new <code>ClassLoader</code> to be used by this
0N/A * connector server.
0N/A *
0N/A * @see #getDefaultClassLoader
0N/A */
0N/A public synchronized void setDefaultClassLoader(ClassLoader cl) {
0N/A this.cl = cl;
0N/A }
0N/A
0N/A /**
0N/A * <p>Gets the default <code>ClassLoader</code> used by this connector
0N/A * server.</p>
0N/A *
0N/A * @return the default <code>ClassLoader</code> used by this
0N/A * connector server.</p>
0N/A *
0N/A * @see #setDefaultClassLoader
0N/A */
0N/A public synchronized ClassLoader getDefaultClassLoader() {
0N/A return cl;
0N/A }
0N/A
0N/A /**
0N/A * <p>Sets the <code>MBeanServer</code> to which this connector
0N/A * server is attached. New client connections will interact
0N/A * with this <code>MBeanServer</code>. Existing client connections are
0N/A * unaffected.</p>
0N/A *
0N/A * @param mbs the new <code>MBeanServer</code>. Can be null, but
0N/A * new client connections will be refused as long as it is.
0N/A *
0N/A * @see #getMBeanServer
0N/A */
0N/A public synchronized void setMBeanServer(MBeanServer mbs) {
0N/A this.mbeanServer = mbs;
0N/A }
0N/A
0N/A /**
0N/A * <p>The <code>MBeanServer</code> to which this connector server
0N/A * is attached. This is the last value passed to {@link
0N/A * #setMBeanServer} on this object, or null if that method has
0N/A * never been called.</p>
0N/A *
0N/A * @return the <code>MBeanServer</code> to which this connector
0N/A * is attached.
0N/A *
0N/A * @see #setMBeanServer
0N/A */
0N/A public synchronized MBeanServer getMBeanServer() {
0N/A return mbeanServer;
0N/A }
0N/A
0N/A public String getVersion() {
0N/A // Expected format is: "protocol-version implementation-name"
0N/A try {
0N/A return "1.0 java_runtime_" +
0N/A System.getProperty("java.runtime.version");
0N/A } catch (SecurityException e) {
0N/A return "1.0 ";
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * <p>Creates a new client connection. This method calls {@link
0N/A * #makeClient makeClient} and adds the returned client connection
0N/A * object to an internal list. When this
0N/A * <code>RMIServerImpl</code> is shut down via its {@link
0N/A * #close()} method, the {@link RMIConnection#close() close()}
0N/A * method of each object remaining in the list is called.</p>
0N/A *
0N/A * <p>The fact that a client connection object is in this internal
0N/A * list does not prevent it from being garbage collected.</p>
0N/A *
0N/A * @param credentials this object specifies the user-defined
0N/A * credentials to be passed in to the server in order to
0N/A * authenticate the caller before creating the
0N/A * <code>RMIConnection</code>. Can be null.
0N/A *
0N/A * @return the newly-created <code>RMIConnection</code>. This is
0N/A * usually the object created by <code>makeClient</code>, though
0N/A * an implementation may choose to wrap that object in another
0N/A * object implementing <code>RMIConnection</code>.
0N/A *
0N/A * @exception IOException if the new client object cannot be
0N/A * created or exported.
0N/A *
0N/A * @exception SecurityException if the given credentials do not allow
0N/A * the server to authenticate the user successfully.
0N/A *
0N/A * @exception IllegalStateException if {@link #getMBeanServer()}
0N/A * is null.
0N/A */
0N/A public RMIConnection newClient(Object credentials) throws IOException {
0N/A return doNewClient(credentials);
0N/A }
0N/A
0N/A /**
0N/A * This method could be overridden by subclasses defined in this package
0N/A * to perform additional operations specific to the underlying transport
0N/A * before creating the new client connection.
0N/A */
0N/A RMIConnection doNewClient(Object credentials) throws IOException {
0N/A final boolean tracing = logger.traceOn();
0N/A
0N/A if (tracing) logger.trace("newClient","making new client");
0N/A
0N/A if (getMBeanServer() == null)
0N/A throw new IllegalStateException("Not attached to an MBean server");
0N/A
0N/A Subject subject = null;
0N/A JMXAuthenticator authenticator =
0N/A (JMXAuthenticator) env.get(JMXConnectorServer.AUTHENTICATOR);
0N/A if (authenticator == null) {
0N/A /*
0N/A * Create the JAAS-based authenticator only if authentication
0N/A * has been enabled
0N/A */
0N/A if (env.get("jmx.remote.x.password.file") != null ||
0N/A env.get("jmx.remote.x.login.config") != null) {
0N/A authenticator = new JMXPluggableAuthenticator(env);
0N/A }
0N/A }
0N/A if (authenticator != null) {
0N/A if (tracing) logger.trace("newClient","got authenticator: " +
0N/A authenticator.getClass().getName());
0N/A try {
0N/A subject = authenticator.authenticate(credentials);
0N/A } catch (SecurityException e) {
0N/A logger.trace("newClient", "Authentication failed: " + e);
0N/A throw e;
0N/A }
0N/A }
0N/A
0N/A if (tracing) {
0N/A if (subject != null)
0N/A logger.trace("newClient","subject is not null");
0N/A else logger.trace("newClient","no subject");
0N/A }
0N/A
0N/A final String connectionId = makeConnectionId(getProtocol(), subject);
0N/A
0N/A if (tracing)
0N/A logger.trace("newClient","making new connection: " + connectionId);
0N/A
0N/A RMIConnection client = makeClient(connectionId, subject);
0N/A
0N/A dropDeadReferences();
0N/A WeakReference<RMIConnection> wr = new WeakReference<RMIConnection>(client);
0N/A synchronized (clientList) {
0N/A clientList.add(wr);
0N/A }
0N/A
744N/A connServer.connectionOpened(connectionId, "Connection opened", null);
744N/A
744N/A synchronized (clientList) {
744N/A if (!clientList.contains(wr)) {
744N/A // can be removed only by a JMXConnectionNotification listener
744N/A throw new IOException("The connection is refused.");
744N/A }
744N/A }
744N/A
0N/A if (tracing)
0N/A logger.trace("newClient","new connection done: " + connectionId );
0N/A
0N/A return client;
0N/A }
0N/A
0N/A /**
0N/A * <p>Creates a new client connection. This method is called by
0N/A * the public method {@link #newClient(Object)}.</p>
0N/A *
0N/A * @param connectionId the ID of the new connection. Every
0N/A * connection opened by this connector server will have a
0N/A * different ID. The behavior is unspecified if this parameter is
0N/A * null.
0N/A *
0N/A * @param subject the authenticated subject. Can be null.
0N/A *
0N/A * @return the newly-created <code>RMIConnection</code>.
0N/A *
0N/A * @exception IOException if the new client object cannot be
0N/A * created or exported.
0N/A */
0N/A protected abstract RMIConnection makeClient(String connectionId,
0N/A Subject subject)
0N/A throws IOException;
0N/A
0N/A /**
0N/A * <p>Closes a client connection made by {@link #makeClient makeClient}.
0N/A *
0N/A * @param client a connection previously returned by
0N/A * <code>makeClient</code> on which the <code>closeClient</code>
0N/A * method has not previously been called. The behavior is
0N/A * unspecified if these conditions are violated, including the
0N/A * case where <code>client</code> is null.
0N/A *
0N/A * @exception IOException if the client connection cannot be
0N/A * closed.
0N/A */
0N/A protected abstract void closeClient(RMIConnection client)
0N/A throws IOException;
0N/A
0N/A /**
0N/A * <p>Returns the protocol string for this object. The string is
0N/A * <code>rmi</code> for RMI/JRMP and <code>iiop</code> for RMI/IIOP.
0N/A *
0N/A * @return the protocol string for this object.
0N/A */
0N/A protected abstract String getProtocol();
0N/A
0N/A /**
0N/A * <p>Method called when a client connection created by {@link
0N/A * #makeClient makeClient} is closed. A subclass that defines
0N/A * <code>makeClient</code> must arrange for this method to be
0N/A * called when the resultant object's {@link RMIConnection#close()
0N/A * close} method is called. This enables it to be removed from
0N/A * the <code>RMIServerImpl</code>'s list of connections. It is
0N/A * not an error for <code>client</code> not to be in that
0N/A * list.</p>
0N/A *
0N/A * <p>After removing <code>client</code> from the list of
0N/A * connections, this method calls {@link #closeClient
0N/A * closeClient(client)}.</p>
0N/A *
0N/A * @param client the client connection that has been closed.
0N/A *
0N/A * @exception IOException if {@link #closeClient} throws this
0N/A * exception.
0N/A *
0N/A * @exception NullPointerException if <code>client</code> is null.
0N/A */
0N/A protected void clientClosed(RMIConnection client) throws IOException {
0N/A final boolean debug = logger.debugOn();
0N/A
0N/A if (debug) logger.trace("clientClosed","client="+client);
0N/A
0N/A if (client == null)
0N/A throw new NullPointerException("Null client");
0N/A
0N/A synchronized (clientList) {
0N/A dropDeadReferences();
686N/A for (Iterator<WeakReference<RMIConnection>> it = clientList.iterator();
686N/A it.hasNext(); ) {
686N/A WeakReference<RMIConnection> wr = it.next();
0N/A if (wr.get() == client) {
0N/A it.remove();
0N/A break;
0N/A }
0N/A }
0N/A /* It is not a bug for this loop not to find the client. In
0N/A our close() method, we remove a client from the list before
0N/A calling its close() method. */
0N/A }
0N/A
0N/A if (debug) logger.trace("clientClosed", "closing client.");
0N/A closeClient(client);
0N/A
0N/A if (debug) logger.trace("clientClosed", "sending notif");
0N/A connServer.connectionClosed(client.getConnectionId(),
0N/A "Client connection closed", null);
0N/A
0N/A if (debug) logger.trace("clientClosed","done");
0N/A }
0N/A
0N/A /**
0N/A * <p>Closes this connection server. This method first calls the
0N/A * {@link #closeServer()} method so that no new client connections
0N/A * will be accepted. Then, for each remaining {@link
0N/A * RMIConnection} object returned by {@link #makeClient
0N/A * makeClient}, its {@link RMIConnection#close() close} method is
0N/A * called.</p>
0N/A *
0N/A * <p>The behavior when this method is called more than once is
0N/A * unspecified.</p>
0N/A *
0N/A * <p>If {@link #closeServer()} throws an
0N/A * <code>IOException</code>, the individual connections are
0N/A * nevertheless closed, and then the <code>IOException</code> is
0N/A * thrown from this method.</p>
0N/A *
0N/A * <p>If {@link #closeServer()} returns normally but one or more
0N/A * of the individual connections throws an
0N/A * <code>IOException</code>, then, after closing all the
0N/A * connections, one of those <code>IOException</code>s is thrown
0N/A * from this method. If more than one connection throws an
0N/A * <code>IOException</code>, it is unspecified which one is thrown
0N/A * from this method.</p>
0N/A *
0N/A * @exception IOException if {@link #closeServer()} or one of the
0N/A * {@link RMIConnection#close()} calls threw
0N/A * <code>IOException</code>.
0N/A */
0N/A public synchronized void close() throws IOException {
0N/A final boolean tracing = logger.traceOn();
0N/A final boolean debug = logger.debugOn();
0N/A
0N/A if (tracing) logger.trace("close","closing");
0N/A
0N/A IOException ioException = null;
0N/A try {
0N/A if (debug) logger.debug("close","closing Server");
0N/A closeServer();
0N/A } catch (IOException e) {
0N/A if (tracing) logger.trace("close","Failed to close server: " + e);
0N/A if (debug) logger.debug("close",e);
0N/A ioException = e;
0N/A }
0N/A
0N/A if (debug) logger.debug("close","closing Clients");
0N/A // Loop to close all clients
0N/A while (true) {
0N/A synchronized (clientList) {
0N/A if (debug) logger.debug("close","droping dead references");
0N/A dropDeadReferences();
0N/A
0N/A if (debug) logger.debug("close","client count: "+clientList.size());
0N/A if (clientList.size() == 0)
0N/A break;
0N/A /* Loop until we find a non-null client. Because we called
0N/A dropDeadReferences(), this will usually be the first
0N/A element of the list, but a garbage collection could have
0N/A happened in between. */
686N/A for (Iterator<WeakReference<RMIConnection>> it = clientList.iterator();
686N/A it.hasNext(); ) {
686N/A WeakReference<RMIConnection> wr = it.next();
686N/A RMIConnection client = wr.get();
0N/A it.remove();
0N/A if (client != null) {
0N/A try {
0N/A client.close();
0N/A } catch (IOException e) {
0N/A if (tracing)
0N/A logger.trace("close","Failed to close client: " + e);
0N/A if (debug) logger.debug("close",e);
0N/A if (ioException == null)
0N/A ioException = e;
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A if(notifBuffer != null)
0N/A notifBuffer.dispose();
0N/A
0N/A if (ioException != null) {
0N/A if (tracing) logger.trace("close","close failed.");
0N/A throw ioException;
0N/A }
0N/A
0N/A if (tracing) logger.trace("close","closed.");
0N/A }
0N/A
0N/A /**
0N/A * <p>Called by {@link #close()} to close the connector server.
0N/A * After returning from this method, the connector server must
0N/A * not accept any new connections.</p>
0N/A *
0N/A * @exception IOException if the attempt to close the connector
0N/A * server failed.
0N/A */
0N/A protected abstract void closeServer() throws IOException;
0N/A
0N/A private static synchronized String makeConnectionId(String protocol,
0N/A Subject subject) {
0N/A connectionIdNumber++;
0N/A
0N/A String clientHost = "";
0N/A try {
0N/A clientHost = RemoteServer.getClientHost();
6417N/A /*
6417N/A * According to the rules specified in the javax.management.remote
6417N/A * package description, a numeric IPv6 address (detected by the
6417N/A * presence of otherwise forbidden ":" character) forming a part
6417N/A * of the connection id must be enclosed in square brackets.
6417N/A */
6417N/A if (clientHost.contains(":")) {
6417N/A clientHost = "[" + clientHost + "]";
6417N/A }
0N/A } catch (ServerNotActiveException e) {
0N/A logger.trace("makeConnectionId", "getClientHost", e);
0N/A }
0N/A
0N/A final StringBuilder buf = new StringBuilder();
0N/A buf.append(protocol).append(":");
0N/A if (clientHost.length() > 0)
0N/A buf.append("//").append(clientHost);
0N/A buf.append(" ");
0N/A if (subject != null) {
686N/A Set<Principal> principals = subject.getPrincipals();
0N/A String sep = "";
686N/A for (Iterator<Principal> it = principals.iterator(); it.hasNext(); ) {
686N/A Principal p = it.next();
0N/A String name = p.getName().replace(' ', '_').replace(';', ':');
0N/A buf.append(sep).append(name);
0N/A sep = ";";
0N/A }
0N/A }
0N/A buf.append(" ").append(connectionIdNumber);
0N/A if (logger.traceOn())
0N/A logger.trace("newConnectionId","connectionId="+buf);
0N/A return buf.toString();
0N/A }
0N/A
0N/A private void dropDeadReferences() {
0N/A synchronized (clientList) {
686N/A for (Iterator<WeakReference<RMIConnection>> it = clientList.iterator();
686N/A it.hasNext(); ) {
686N/A WeakReference<RMIConnection> wr = it.next();
0N/A if (wr.get() == null)
0N/A it.remove();
0N/A }
0N/A }
0N/A }
0N/A
0N/A synchronized NotificationBuffer getNotifBuffer() {
0N/A //Notification buffer is lazily created when the first client connects
0N/A if(notifBuffer == null)
0N/A notifBuffer =
0N/A ArrayNotificationBuffer.getNotificationBuffer(mbeanServer,
0N/A env);
0N/A return notifBuffer;
0N/A }
0N/A
0N/A private static final ClassLogger logger =
0N/A new ClassLogger("javax.management.remote.rmi", "RMIServerImpl");
0N/A
0N/A /** List of WeakReference values. Each one references an
0N/A RMIConnection created by this object, or null if the
0N/A RMIConnection has been garbage-collected. */
0N/A private final List<WeakReference<RMIConnection>> clientList =
0N/A new ArrayList<WeakReference<RMIConnection>>();
0N/A
0N/A private ClassLoader cl;
0N/A
0N/A private MBeanServer mbeanServer;
0N/A
686N/A private final Map<String, ?> env;
0N/A
0N/A private RMIConnectorServer connServer;
0N/A
0N/A private static int connectionIdNumber;
0N/A
0N/A private NotificationBuffer notifBuffer;
0N/A}