/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas.jmx;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import org.objectweb.jonas.common.Log;
import org.objectweb.jonas.service.AbsServiceImpl;
import org.objectweb.jonas.service.ServiceException;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;


/**
 * JMX Service implementation
 * This singleton class must exist in each jonas server that want to use JMX
 * @author Guillaume Riviere
 * Contributor(s): Michel Bruno
 */

public class JmxServiceImpl extends AbsServiceImpl implements JmxService {

    // Trace logger
     static private Logger logger = null;

    // -------------------------------------------------------------------
    // JOnAS Service Implementation
    // -------------------------------------------------------------------

    private String jonasName = null;

    private MBeanServer jmxServer = null;

    /**
     * Init the Service.
     * Configuration information is passed thru a Context object.
     */
    public void doInit(Context ctx) throws ServiceException {
        logger = Log.getLogger(Log.JONAS_SERVER_PREFIX);
	jmxServer = MBeanServerFactory.createMBeanServer();
	// Gets the configuration
	try {
	    jonasName = (String) ctx.lookup("jonas.name");
	} catch (NamingException ne) {
	    logger.log(BasicLevel.DEBUG, "Cannot initialize JMX service "+ne);
	    throw new ServiceException("Cannot initialize JMX service", ne);
	}
	logger.log(BasicLevel.DEBUG, "JMX Service initialized");
    }

    /**
     * Start the Service
     * Initialization of the service is already done.
     */
    public void doStart() throws ServiceException {       

	try {
	    // JMX RMI Connector creation
	    RMIConnectorImpl rmiConnector = new RMIConnectorImpl(jmxServer);
	    // RMIConnector rebind
	    InitialContext ictx = new InitialContext();
	    ictx.rebind("RMIConnector_"+jonasName, rmiConnector);
	    ictx.close();
	} catch (Exception e) {
	    logger.log(BasicLevel.DEBUG, "Cannot start JMX service "+e);
	    throw new ServiceException("Cannot start JMX service", e);
	}

    }

    /**
     * Stop this service
     */
    public void doStop() {
	try {
	    InitialContext ictx = new InitialContext();
	    ictx.unbind("RMIConnector_"+jonasName);
	    ictx.close();
	} catch (Exception e) {
	    logger.log(BasicLevel.ERROR, "Cannot Unbind Jmx RMI Connector" + e);
	}
	logger.log(BasicLevel.DEBUG, "JMX Service stopped");
    }

    /**
     * @return The reference of the MBean server
     */
    public MBeanServer getJmxServer() {
	return this.jmxServer;
    }
}
