/**
 * 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 java.util.Hashtable;

import javax.management.MBeanInfo;
import javax.management.ObjectName;
import javax.naming.Context;

/**
 * Wraper class. Apply the management operations on the ManagementRep instance.
 * @author Adriana Danes
 */
public class JonasManagementRepr {

    /**
     * Using getManagementRepr() without parameters makes creating a ManagemenrReprImpl
     * object (an "old" style representant which uses proprietary RMIConnector object)
     */
    //private static ManagementRepr repr = ManagementReprFactory.getManagementRepr();
    // Let repr null and then use setRepr method, will allow to set/change representative
    // in order to set/change the managed server.
    //private static ManagementRepr repr = null;

    private static Hashtable serverReprs = new Hashtable();


    public static void addServerRepr(String serverName, ManagementRepr repr) {
        serverReprs.put(serverName, repr);
    }

    public static boolean noServerRepr() {
        if (serverReprs.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isServerRepr(String serverName) {
        return serverReprs.containsKey(serverName);
    }

    public static ManagementRepr getServerRepr(String serverName) throws ManagementException {
        if (!isServerRepr(serverName)) {
            // try to get repr
            ManagementReprLoader.loadServerRepr(serverName);
        }
        ManagementRepr repr = (ManagementRepr) serverReprs.get(serverName);
        if (repr != null) {
            return repr;
        } else {
            throw new ManagementException("Can't manage server " + serverName + " as no ManagementRepr set (couldn't get connection)");
        }
    }
    /**
     * @return True if the MBean is already registered in the MBean server, false otherwise or if an exception is catched.
     * @param on ObjectName of the MBean we are looking for
     * @param serverName The server name
     */
    public static boolean isRegistered(ObjectName on, String serverName) throws ManagementException {
        ManagementRepr repr = getServerRepr(serverName);
        return repr.isRegistered(on);
    }

    /**
     * @param on The ObjectName of the MBean from which the attribute is to be retrieved.
     * @param attribute A String specifying the name of the attribute to be retrieve.
     * @param serverName The server name
     * @return The value of the attribute.
     */
    public static Object getAttribute(ObjectName on, String attribute, String serverName)
    throws ManagementException {
        ManagementRepr repr = getServerRepr(serverName);
        return repr.getAttribute(on, attribute);
    }

    /**
     * @param on The ObjectName of the MBean from which the attribute is to be retrieved.
     * @param attribute A String specifying the name of the attribute to be retrieve.
     * @param serverName The server name
     * @return The value of the attribute.
     */
    public static Object getAttributes(ObjectName on, String attribute, String serverName)
        throws ManagementException {
        ManagementRepr repr = getServerRepr(serverName);
        return repr.getAttribute(on, attribute);
    }

    /**
     * @param on The ObjectName of the MBean within which the attribute is to be set.
     * @param serverName The server name
     * @param attribute A String specifying the name of the attribute to be retrieve.
     * @param value The value to set to the attribute.
     */
    public static void setAttribute(ObjectName on, String attribute, Object value, String serverName)
        throws ManagementException
    {
        ManagementRepr repr = getServerRepr(serverName);
        repr.setAttribute(on, attribute, value);
    }

    /**
     * @param on
     * @param param invoke parameters
     * @param signature invoke parameters signature
     * @param serverName The server name
     */
    public static Object invoke(ObjectName on, String operation, Object[] param, String[] signature, String serverName)
        throws ManagementException {
        ManagementRepr repr = getServerRepr(serverName);
        return repr.invoke(on, operation, param, signature);
    }

    /**
     * @return A set containing the ObjectNames for the MBeans selected.
     * @param serverName The server name
     */
    public static java.util.Set queryNames(ObjectName on, String serverName) throws ManagementException
    {
        ManagementRepr repr = getServerRepr(serverName);
        return repr.queryNames(on);
    }


    /**
     * @return An instance of MBeanInfo allowing the retrieval of all
     * attributes and operations of this MBean.
     * @param name MBean's ObjectName
     * @param serverName The server name
     */
    public static MBeanInfo getMBeanInfo(ObjectName name, String serverName) throws ManagementException {
        ManagementRepr repr = getServerRepr(serverName);
        return (MBeanInfo) repr.getMBeanInfo(name);
    }

    /**
     * @return Context the current application context.
     */
    public static Context getContext(String serverName) throws javax.naming.NamingException {
        ManagementRepr repr = getServerRepr(serverName);
        return repr.getContext();
    }
}
