📄 rmiconnectorserver.java
字号:
/* * @(#)RMIConnectorServer.java 1.61 04/06/21 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.management.remote.rmi;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.net.MalformedURLException;import java.rmi.Remote;import java.rmi.server.RemoteObject;import java.rmi.server.RMIClientSocketFactory;import java.rmi.server.RMIServerSocketFactory;import java.util.Collections;import java.util.Hashtable;import java.util.Map;import java.util.HashMap;import java.util.Set;import java.util.HashSet;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.management.MBeanRegistration;import javax.management.MBeanServer;import javax.management.ObjectName;import javax.management.InstanceNotFoundException;import javax.management.remote.JMXConnectionNotification;import javax.management.remote.JMXConnector;import javax.management.remote.JMXConnectorServer;import javax.management.remote.JMXServiceURL;import javax.management.remote.MBeanServerForwarder;import com.sun.jmx.remote.security.MBeanServerFileAccessController;import com.sun.jmx.remote.util.ClassLogger;import com.sun.jmx.remote.util.EnvHelp;/** * <p>A JMX API connector server that creates RMI-based connections * from remote clients. Usually, such connector servers are made * using {@link javax.management.remote.JMXConnectorServerFactory * JMXConnectorServerFactory}. However, specialized applications can * use this class directly, for example with an {@link RMIServerImpl} * object.</p> * * @since 1.5 * @since.unbundled 1.0 */public class RMIConnectorServer extends JMXConnectorServer { /** * <p>Name of the attribute that specifies whether the {@link * RMIServer} stub that represents an RMI connector server should * override an existing stub at the same address. The value * associated with this attribute, if any, should be a string that * is equal, ignoring case, to <code>"true"</code> or * <code>"false"</code>. The default value is false.</p> */ public static final String JNDI_REBIND_ATTRIBUTE = "jmx.remote.jndi.rebind"; /** * <p>Name of the attribute that specifies the {@link * RMIClientSocketFactory} for the RMI objects created in * conjunction with this connector. The value associated with this * attribute must be of type <code>RMIClientSocketFactory</code> and can * only be specified in the <code>Map</code> argument supplied when * creating a connector server.</p> */ public static final String RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE = "jmx.remote.rmi.client.socket.factory"; /** * <p>Name of the attribute that specifies the {@link * RMIServerSocketFactory} for the RMI objects created in * conjunction with this connector. The value associated with this * attribute must be of type <code>RMIServerSocketFactory</code> and can * only be specified in the <code>Map</code> argument supplied when * creating a connector server.</p> */ public static final String RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE = "jmx.remote.rmi.server.socket.factory"; /** * <p>Makes an <code>RMIConnectorServer</code>. * This is equivalent to calling {@link #RMIConnectorServer( * JMXServiceURL,Map,RMIServerImpl,MBeanServer) * RMIConnectorServer(directoryURL,environment,null,null)}</p> * * @param url the URL defining how to create the connector server. * Cannot be null. * * @param environment attributes governing the creation and * storing of the RMI object. Can be null, which is equivalent to * an empty Map. * * @exception IllegalArgumentException if <code>url</code> is null. * * @exception MalformedURLException if <code>url</code> does not * conform to the syntax for an RMI connector, or if its protocol * is not recognized by this implementation. Only "rmi" and "jrmp" * are valid when this constructor is used. * * @exception IOException if the connector server cannot be created * for some reason or if it is inevitable that its {@link #start() * start} method will fail. */ public RMIConnectorServer(JMXServiceURL url, Map<String,?> environment) throws IOException { this(url, environment, (MBeanServer) null); } /** * <p>Makes an <code>RMIConnectorServer</code> for the given MBean * server. * This is equivalent to calling {@link #RMIConnectorServer( * JMXServiceURL,Map,RMIServerImpl,MBeanServer) * RMIConnectorServer(directoryURL,environment,null,mbeanServer)}</p> * * @param url the URL defining how to create the connector server. * Cannot be null. * * @param environment attributes governing the creation and * storing of the RMI object. Can be null, which is equivalent to * an empty Map. * * @param mbeanServer the MBean server to which the new connector * server is attached, or null if it will be attached by being * registered as an MBean in the MBean server. * * @exception IllegalArgumentException if <code>url</code> is null. * * @exception MalformedURLException if <code>url</code> does not * conform to the syntax for an RMI connector, or if its protocol * is not recognized by this implementation. Only "rmi" and "jrmp" * are valid when this constructor is used. * * @exception IOException if the connector server cannot be created * for some reason or if it is inevitable that its {@link #start() * start} method will fail. */ public RMIConnectorServer(JMXServiceURL url, Map<String,?> environment, MBeanServer mbeanServer) throws IOException { this(url, environment, (RMIServerImpl) null, mbeanServer); } /** * <p>Makes an <code>RMIConnectorServer</code> for the given MBean * server.</p> * * @param url the URL defining how to create the connector server. * Cannot be null. * * @param environment attributes governing the creation and * storing of the RMI object. Can be null, which is equivalent to * an empty Map. * * @param rmiServerImpl An implementation of the RMIServer interface, * consistent with the protocol type specified in <var>url</var>. * If this parameter is non null, the protocol type specified by * <var>url</var> is not constrained, and is assumed to be valid. * Otherwise, only "rmi" and "iiop" will be recognized. * * @param mbeanServer the MBean server to which the new connector * server is attached, or null if it will be attached by being * registered as an MBean in the MBean server. * * @exception IllegalArgumentException if <code>url</code> is null. * * @exception MalformedURLException if <code>url</code> does not * conform to the syntax for an RMI connector, or if its protocol * is not recognized by this implementation. Only "rmi" and "jrmp" * are recognized when <var>rmiServerImpl</var> is null. * * @exception IOException if the connector server cannot be created * for some reason or if it is inevitable that its {@link #start() * start} method will fail. * * @see #start */ public RMIConnectorServer(JMXServiceURL url, Map<String,?> environment, RMIServerImpl rmiServerImpl, MBeanServer mbeanServer) throws IOException { super(mbeanServer); if (url == null) throw new IllegalArgumentException("Null JMXServiceURL"); if (rmiServerImpl == null) { final String prt = url.getProtocol(); if (prt == null || !(prt.equals("rmi") || prt.equals("iiop"))) { final String msg = "Invalid protocol type: " + prt; throw new MalformedURLException(msg); } final String urlPath = url.getURLPath(); if (!urlPath.equals("") && !urlPath.equals("/") && !urlPath.startsWith("/jndi/")) { final String msg = "URL path must be empty or start with " + "/jndi/"; throw new MalformedURLException(msg); } } if (environment == null) this.attributes = Collections.EMPTY_MAP; else { EnvHelp.checkAttributes(environment); this.attributes = Collections.unmodifiableMap(environment); } this.address = url; this.rmiServerImpl = rmiServerImpl; } /** * <p>Returns a client stub for this connector server. A client * stub is a serializable object whose {@link * JMXConnector#connect(Map) connect} method can be used to make * one new connection to this connector server.</p> * * @param env client connection parameters of the same sort that * could be provided to {@link JMXConnector#connect(Map) * JMXConnector.connect(Map)}. Can be null, which is equivalent * to an empty map. * * @return a client stub that can be used to make a new connection * to this connector server. * * @exception UnsupportedOperationException if this connector * server does not support the generation of client stubs. * * @exception IllegalStateException if the JMXConnectorServer is * not started (see {@link #isActive()}). * * @exception IOException if a communications problem means that a * stub cannot be created. **/ public JMXConnector toJMXConnector(Map<String,?> env) throws IOException { // The serialized for of rmiServerImpl is automagically // a RMI server stub. if (!isActive()) throw new IllegalStateException("Connector is not active"); // Merge maps Map usemap = new HashMap((this.attributes==null)?Collections.EMPTY_MAP: this.attributes); if (env != null) { EnvHelp.checkAttributes(env); usemap.putAll(env); } usemap = EnvHelp.filterAttributes(usemap); final RMIServer stub=(RMIServer)rmiServerImpl.toStub(); return new RMIConnector(stub, usemap); } /** * <p>Activates the connector server, that is starts listening for * client connections. Calling this method when the connector * server is already active has no effect. Calling this method * when the connector server has been stopped will generate an * <code>IOException</code>.</p> * * <p>The behaviour of this method when called for the first time * depends on the parameters that were supplied at construction, * as described below.</p> * * <p>First, an object of a subclass of {@link RMIServerImpl} is * required, to export the connector server through RMI:</p> *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -