Package org.apache.geronimo.jmxremoting

Source Code of org.apache.geronimo.jmxremoting.JMXConnector

/**
*
* Copyright 2004 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package org.apache.geronimo.jmxremoting;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnectionNotification;
import javax.management.MBeanServer;
import javax.management.NotificationFilterSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;
import org.apache.geronimo.system.jmx.MBeanServerReference;

/**
* A Connector that supports the server sideof JSR 160 JMX Remoting.
*
* @version $Rev: 395155 $ $Date: 2006-04-19 08:44:24 +0200 (Wed, 19 Apr 2006) $
*/
public class JMXConnector implements GBeanLifecycle {
    private final MBeanServer mbeanServer;
    private final Log log;
    private final ClassLoader classLoader;
    private String applicationConfigName;
    private Authenticator authenticator;

    private String protocol;
    private String host;
    private int port = -1;
    private String urlPath;

    private JMXConnectorServer server;
    private JMXServiceURL jmxServiceURL;

    // todo remove this as soon as Geronimo supports factory beans
    public JMXConnector(MBeanServerReference mbeanServerReference, String objectName, ClassLoader classLoader) {
        this(mbeanServerReference.getMBeanServer(), objectName, classLoader);
    }

    /**
     * Constructor for creating the connector. The ClassLoader must be
     * able to load all the LoginModules used in the JAAS login
     *
     * @param mbeanServer the mbean server
     * @param objectName this connector's object name
     * @param classLoader the classLoader used to create this connector
     */
    public JMXConnector(MBeanServer mbeanServer, String objectName, ClassLoader classLoader) {
        this.mbeanServer = mbeanServer;
        this.classLoader = classLoader;
        log = LogFactory.getLog(objectName);
    }

    /**
     * Return the name of the JAAS Application Configuration Entry this
     * connector uses to authenticate users. If null, users are not
     * be authenticated (not recommended).
     *
     * @return the authentication configuration name
     */
    public String getApplicationConfigName() {
        return applicationConfigName;
    }

    /**
     * Set the name of the JAAS Application Configuration Entry this
     * connector should use to authenticate users. If null, users will not
     * be authenticated (not recommended).
     *
     * @param applicationConfigName the authentication configuration name
     */
    public void setApplicationConfigName(String applicationConfigName) {
        this.applicationConfigName = applicationConfigName;
    }

    /**
     * Every connector must specify a property of type InetSocketAddress
     * because we use that to identify the network services to print a list
     * during startup.  However, this can be read-only since the host and port
     * are set in the url attribute.
     */
    public InetSocketAddress getListenAddress() {
        return new InetSocketAddress(getHost(), getPort());
    }

    /**
     * Gets the protocol to use for the connection.
     * @return the protocol to use for the connection
     */
    public String getProtocol() {
        return protocol;
    }

    /**
     * Sets the protocol to use for the connection.
     * @param protocol the protocol to use for the connection
     */
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    /**
     * Gets the JMX host for this connector.
     *
     * @return the JMX host for this connector
     */
    public String getHost() {
        return host;
    }

    /**
     * Sets the JMX host for this connector.
     * @param host the JMX host for this connector
     */
    public void setHost(String host) {
        this.host = host;
    }

    /**
     * Gets the JMX port for this connector.
     *
     * @return the JMX port for this connector
     */
    public int getPort() {
        return port;
    }

    /**
     * Sets the JMX port for this connector.
     * @param port the JMX port for this connector
     */
    public void setPort(int port) {
        this.port = port;
    }

    /**
     * Gets the path within the target server to look for the connection.  This is commonly
     * /jndi/rmi://localhost:1099/JMXConnector
     * @return the path used to loacate the connector on the target server
     */
    public String getUrlPath() {
        return urlPath;
    }

    /**
     * Sets the path within the target server to look for the connection.  This is commonly
     * /jndi/rmi://localhost:1099/JMXConnector
     * @param urlPath the path used to loacate the connector on the target server
     */
    public void setUrlPath(String urlPath) {
        this.urlPath = urlPath;
    }

    public void doStart() throws Exception {
        jmxServiceURL = new JMXServiceURL(protocol, host, port, urlPath);
        Map env = new HashMap();
        if (applicationConfigName != null) {
            authenticator = new Authenticator(applicationConfigName, classLoader);
            env.put(JMXConnectorServer.AUTHENTICATOR, authenticator);
        } else {
            log.warn("Starting unauthenticating JMXConnector for " + jmxServiceURL);
        }
        server = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, env, mbeanServer);
        NotificationFilterSupport filter = new NotificationFilterSupport();
        filter.enableType(JMXConnectionNotification.OPENED);
        filter.enableType(JMXConnectionNotification.CLOSED);
        filter.enableType(JMXConnectionNotification.FAILED);
        server.addNotificationListener(authenticator, filter, null);
        server.start();
        log.debug("Started JMXConnector " + server.getAddress());
    }

    public void doStop() throws Exception {
        try {
              server.stop();
        } catch (java.io.IOException e) {
              // java.io.IOException is expected.
        } catch (Exception e) {
              // Otherwise, something bad happened.  Rethrow the exception.
              throw e;
        }
        finally {
          server = null;
          log.debug("Stopped JMXConnector " + jmxServiceURL);
        }
    }

    public void doFail() {
        try {
            doStop();
            log.warn("Failure in JMXConnector " + jmxServiceURL);
        } catch (Exception e) {
            log.warn("Error stopping JMXConnector after failure", e);
        }
    }

    public static final GBeanInfo GBEAN_INFO;

    static {
        GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("JMX Remoting Connector", JMXConnector.class);
        infoFactory.addReference("MBeanServerReference", MBeanServerReference.class);
        infoFactory.addAttribute("objectName", String.class, false);
        infoFactory.addAttribute("classLoader", ClassLoader.class, false);

        infoFactory.addAttribute("protocol", String.class, true, true);
        infoFactory.addAttribute("host", String.class, true, true);
        infoFactory.addAttribute("port", int.class, true, true);
        infoFactory.addAttribute("urlPath", String.class, true, true);
        infoFactory.addAttribute("applicationConfigName", String.class, true, true);

        infoFactory.setConstructor(new String[]{"MBeanServerReference", "objectName", "classLoader"});
        GBEAN_INFO = infoFactory.getBeanInfo();
    }

    public static GBeanInfo getGBeanInfo() {
        return GBEAN_INFO;
    }
}
TOP

Related Classes of org.apache.geronimo.jmxremoting.JMXConnector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.