/*
 * 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
 *
 * Initial developer(s): ____________________________________.
 * Contributor(s): Bruno Michel and Guillaume Riviere
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jmx.vjadmin;

import javax.management.Attribute;
import javax.management.ObjectName;

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

// JOnAS log imports
import org.objectweb.jonas.common.Log;
/**
 * A set of static methods used in all servlets
 */
public class JonasManagementRepr {

    /**
     * @return True if the MBean is already registered in the MBean server, false otherwise or if an exception is catched.
     */
    public static boolean isRegistered(ObjectName on) {
	try {
	    return ConnectorFactory.getRMIConnector().isRegistered(on);
	} catch (Exception e) {
	    return false;
	}
    }

    /**
     * @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.
     * @return The value of the attribute.
     */
    public static Object getAttribute(ObjectName on, String attribute) 
	throws ManagementException 
    {
	try {
	    return ConnectorFactory.getRMIConnector().getAttribute(on, attribute);
	} catch (Exception e) {
	    throw new ManagementException("Error while getting attribute "
					  +attribute+": "
					  +e.getClass().getName(), e);
	}
    }

    /**
     * @param on The ObjectName of the MBean within which the attribute is to be set.
     * @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) 
	throws ManagementException 
    {
	try {
	    ConnectorFactory.getRMIConnector().setAttribute(on, new Attribute(attribute, value));
	} catch (Exception e) {
	    throw new ManagementException("Error while setting attribute "
					  +attribute+": "
					  +e.getClass().getName(), e);
	}
    }

    /**
     * @param on
     */
    public static Object invoke(ObjectName on, String operation, Object[] param, String[] signature) 
	throws ManagementException 
    {
	Logger logger = Log.getLogger("org.objectweb.jonas.jmx.vjadmin");
	try {
	    return ConnectorFactory.getRMIConnector().invoke(on, operation, param, signature);
	} catch (Exception e) {
	    logger.log(BasicLevel.DEBUG, "EXCEPTION when calling invoke. Exception class: " + e.getClass().getName());
	    if (e.getClass().getName().equals("javax.management.MBeanException")) {
		Exception te = ((javax.management.MBeanException)e).getTargetException();
		logger.log(BasicLevel.DEBUG, "TEST target exception : " + e.toString());
		logger.log(BasicLevel.DEBUG, "TEST target exception message : " + e.getMessage());
	    }
	    throw new ManagementException("Error while invoking operation "
					  +operation+": "
					  +e.getClass().getName(), e);
	}
    }

    /**
     * @return A set containing the ObjectNames for the MBeans selected.
     */
    public static java.util.Set queryNames(ObjectName on) throws ManagementException 
    {
	try {
	    return (java.util.Set)ConnectorFactory.getRMIConnector().queryNames(on, null);
	} catch (Exception e) {
	    throw new ManagementException("Error while getting MBeans names: "
					  +e.getClass().getName(), e);
	}
    }
}
