/**
 * 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;

// JOnAS Log
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;

import org.objectweb.jonas.common.Log;
import org.objectweb.jonas.management.j2eemanagement.J2EEDomain;
import org.objectweb.jonas.service.ServiceException;
import org.objectweb.jonas.service.ServiceManager;

import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

/**
 * Provides an appropriate management representative.
 * @author Adriana Danes
 */
public class ManagementReprFactory {
    /**
     * Private constructor
     */
    private ManagementReprFactory() {
    }

    /**
     * Logger
     */
    private static Logger logger = Log.getLogger("org.objectweb.jonas.jmx");

    /**
     * Create a ManagementReprImp instance
     * This method is deprecated. ManagementReprImpl objects will no longer be used in rthe future.
     * @return ManagementReprImp instance
     */
    /*public static ManagementRepr getManagementRepr() {
        ManagementRepr representative = null;
        ClassLoader classLoader = ManagementReprFactory.class.getClassLoader();
        try {
            Class managemntRepClass = classLoader.loadClass("org.objectweb.jonas.jmx.ManagementReprImpl");
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "ManagemntReprImp class loaded");
            }
            representative = (ManagementRepr) managemntRepClass.newInstance();
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "ManagemntRepr created");
            }
        } catch (Exception e) {
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "ManagementReprFactory exception : " + e.toString());
            }
        }
        return representative;
    }*/

    /**
     * Create a ManagementReprImpJSR160 instance
     * @param serverName The server to be managed via the created ManagementRepr
     * @return ManagementReprImpJSR160 instance
     */
    public static ManagementRepr getManagementRepr(String serverName) {
        ManagementRepr representative = null;
        ClassLoader classLoader = ManagementReprFactory.class.getClassLoader();
        try {
            Class managemntRepClass = classLoader.loadClass("org.objectweb.jonas.jmx.ManagementReprImplJSR160");
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "ManagemntReprImplJSR160 class loaded");
            }
            representative = (ManagementRepr) managemntRepClass.newInstance();
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "ManagemntRepr created");
            }
            // Get the MBean server connection:
            // - a JSR 160 connection object if the server can be remotelly managed
            // - local MBeanServer reference
            MBeanServerConnection connection = J2EEDomain.getInstance().getConnection(serverName);
            ((ManagementReprImplJSR160) representative).setMBeanServerConnection(connection);
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "ManagementRepr has set connection for server " + serverName);
            }
            if (connection == null) {
                // Couldn't get connection this can arrive only for remote connections
                logger.log(BasicLevel.WARN, "ManagementRepr couldn't get connection for server " + serverName);
            } else {
            	if (logger.isLoggable(BasicLevel.DEBUG)) {
                    logger.log(BasicLevel.DEBUG, "ManagementRepr has set connection for server " + serverName);
                }
            }
        } catch (Exception e) {
            logger.log(BasicLevel.WARN, "Exception when trying to create ManagementRepr: " + e.toString());
        }
        return representative;
    }

    /**
     * Create a ManagementReprImpJSR160 instance
     * @param serverName The server to be managed via the created ManagementRepr
     * @return ManagementReprImpJSR160 instance
     *//*
    public static ManagementRepr getManagementRepr(String serverName, MBeanServerConnection connection) {
        ManagementRepr representative = null;
        ClassLoader classLoader = ManagementReprFactory.class.getClassLoader();
        try {
            Class managemntRepClass = classLoader.loadClass("org.objectweb.jonas.jmx.ManagementReprImplJSR160");
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "ManagemntReprImplJSR160 class loaded");
            }
            representative = (ManagementRepr) managemntRepClass.newInstance();
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "ManagemntRepr created");
            }
            ((ManagementReprImplJSR160) representative).setMBeanServerConnection(connection);
            ((ManagementReprImplJSR160) representative).setServerName(serverName);
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "ManagementRepr has set connection for server " + serverName);
            }
        } catch (Exception e) {
            logger.log(BasicLevel.WARN, "Exception when trying to create ManagementRepr: " + e.toString());
        }
        return representative;
    }*/

    /**
     * @return MBeanServerConnection corresponding to the local MBeanServer
     */
    public static MBeanServerConnection getLocalManagementRepr() {
        MBeanServer myMBeanServer = null;
        JmxService jmxService = null;
        try {
            jmxService = (JmxService) ServiceManager.getInstance().getJmxService();
            myMBeanServer = jmxService.getJmxServer();
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (MBeanServerConnection) myMBeanServer;
    }
}
