001/*
002 * Copyright 2013 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.atteo.moonshine.jmx;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020import java.util.HashMap;
021
022import javax.inject.Inject;
023import javax.management.MBeanServer;
024import javax.management.remote.JMXConnectorServer;
025import javax.management.remote.JMXConnectorServerFactory;
026import javax.management.remote.JMXServiceURL;
027import javax.xml.bind.annotation.XmlElement;
028import javax.xml.bind.annotation.XmlIDREF;
029import javax.xml.bind.annotation.XmlRootElement;
030
031import org.atteo.moonshine.TopLevelService;
032import org.atteo.moonshine.services.ImportService;
033
034/**
035 * Publishes Jmx MBeanServer through RMI.
036 *
037 * @see <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/management/agent.html">Monitoring and Management Using Jmx Technology</a>
038 */
039@XmlRootElement(name = "jmx-server")
040public class JmxServer extends TopLevelService {
041    /**
042     * Jmx MBeanServer to publish.
043     */
044    @XmlElement
045    @XmlIDREF
046    @ImportService
047    private Jmx jmx;
048
049    /**
050     * {@link RmiRegistry RMI registry} on publish MBeanServer onto.
051     */
052    @XmlElement
053    @XmlIDREF
054    @ImportService
055    private RmiRegistry rmiRegistry;
056
057    @Inject
058    private RmiRegistryPort portProvider;
059
060    @Inject
061    private MBeanServer mbeanServer;
062
063    private JMXConnectorServer connectorServer = null;
064
065    @Override
066    public void start() {
067        JMXServiceURL url;
068        try {
069            url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" + portProvider.getPort() + "/jmxrmi");
070        } catch (MalformedURLException e) {
071            throw new RuntimeException(e);
072        }
073
074        HashMap<String,Object> environment = new HashMap<>();
075        try {
076            connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, environment, mbeanServer);
077            connectorServer.start();
078        } catch (IOException e) {
079            throw new RuntimeException("Cannot start JMX connector server", e);
080        }
081    }
082
083    @Override
084    public void stop() {
085        try {
086            connectorServer.stop();
087        } catch (IOException e) {
088            throw new RuntimeException(e);
089        }
090    }
091}