/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2007 Bull S.A.S.
 * Contact: jonas-team@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.ow2.jonas.services.bootstrap.osgi;

import java.util.List;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;

import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.ow2.jonas.lib.util.Log;
import org.ow2.jonas.properties.ServerProperties;

/**
 * This class represents a JMX Agent. It creates/finds a MBean server then creates a RMI registry and a JMX Connector
 * server.
 * @author Francois Fornaciari
 */
public class JMXAgent {

    /**
     * Management loggers.
     */
    private static Logger logger = Log.getLogger(Log.JONAS_BOOTSTRAP_PREFIX);

    /**
     * MBeanServer.
     */
    private MBeanServer mbeanServer = null;

    /**
     * MBeanServer service registration.
     */
    private ServiceRegistration serverRegistration;

    /**
     * OSGi BundleContext.
     */
    private BundleContext bc = null;

    /**
     * ServerProperties service object.
     */
    private ServerProperties serverProperties = null;

    /**
     * Constructor.
     * @param bc OSGi {@link BundleContext}.
     */
    public JMXAgent(final BundleContext bc) {
        this.bc = bc;
    }

    /**
     * Register the MBeanServer as an OSGi service.
     */
    public void start() {
        logger.log(BasicLevel.DEBUG, "Starting MBean Server");

        initMBeanServer();
        serverRegistration = bc.registerService(MBeanServer.class.getName(), mbeanServer, null);
    }

    /**
     * Unregister the MBeanServer.
     */
    public void stop() {
        logger.log(BasicLevel.DEBUG, "Stopping MBean Server");

        serverRegistration.unregister();
    }

    /**
     * Initialize the MBean Server.
     */
    @SuppressWarnings("unchecked")
    private void initMBeanServer() {
        // Domain name
        String domainName = serverProperties.getDomainName();

        // Look if there is already a MBeanServer for JOnAS.
        List<MBeanServer> mbeanServers = MBeanServerFactory.findMBeanServer(null);
        if (mbeanServers.size() > 0) {
            mbeanServer = mbeanServers.get(0);
        } else {
            // Need to create an MBean server
            mbeanServer = MBeanServerFactory.createMBeanServer(domainName);
        }
    }

    /**
     * @param serverProperties the serverProperties to set
     */
    public void setServerProperties(final ServerProperties serverProperties) {
        this.serverProperties = serverProperties;
    }
}
