/*
 * 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): Michel Bruno and Guillaume Riviere
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas.jmx;

import org.objectweb.common.RemoteObject;
import java.rmi.RemoteException;
import javax.management.*;

/**
 * a simple remote delegation of JMX Server method
 * Used by JAdmin.
 */
public class RMIConnectorImpl extends RemoteObject implements RMIConnector {
    protected MBeanServer server;

    public RMIConnectorImpl(MBeanServer server) throws RemoteException {
	super();
	this.server = server;
    }

    /**
     * Gets the names of MBeans controlled by the MBean server. 
     * @param name - The object name pattern identifying the MBeans to be retrieved. 
     * @param query - The query expression to be applied for selecting MBeans.
     * @return A set containing the ObjectNames for the MBeans selected. 
     * If no MBean satisfies the query, an empty list is returned.
     */
    public java.util.Set queryNames(ObjectName name, 
				    QueryExp query) 
	throws RemoteException {
	return server.queryNames(name, query);
    }

    /**
     * Checks whether an MBean, identified by its object name, is 
     * already registered with the MBean server.
     * @param name - The object name pattern identifying the MBeans
     */
    public boolean isRegistered(ObjectName name)
	throws RemoteException 
    {
	return server.isRegistered(name);
    }

    /**
     * Gets the value of a specific attribute of a named MBean. 
     * @param name - The name of the MBean
     * @param attribute - the name of the attribute to be retrieved.
     * @return The value of the retrieved attribute.
     */
    public java.lang.Object getAttribute(ObjectName name,
					 java.lang.String attribute)
	throws MBeanException,
	       AttributeNotFoundException,
	       InstanceNotFoundException,
	       ReflectionException,
	       RemoteException 
    {
	return server.getAttribute(name, attribute);
    }

    /**
     * Sets the value of a specific attribute of a named MBean. 
     * @param name - The name of the MBean 
     */
    public void setAttribute(ObjectName name,
			     Attribute attribute)
	throws InstanceNotFoundException,
	       AttributeNotFoundException,
	       InvalidAttributeValueException,
	       MBeanException,
	       ReflectionException,
	       RemoteException 
    {
	server.setAttribute(name, attribute);
    }

    /**
     * Invokes an operation on an MBean.
     * @param name - The name of the MBean 
     * @param operationName - The name of the operation to be invoked.
     * @param params - An array containing the parameters to be set when the operation is invoked
     * @param signature - An array containing the signature of the operation. 
     * The class objects will be loaded using the same class loader as the one 
     * used for loading the MBean on which the operation was invoked.
     * @return The object returned by the operation
     */
    public Object invoke(ObjectName name,
			 java.lang.String operationName,
			 java.lang.Object[] params,
			 java.lang.String[] signature)
	throws InstanceNotFoundException,
	       MBeanException,
	       ReflectionException,
	       RemoteException 
    {
	return server.invoke(name, operationName, params, signature);
    }

    /**
     * This method discovers the attributes and operations that an MBean 
     * exposes for management.
     * @param name - The name of the MBean to analyze
     * @return An instance of MBeanInfo allowing the retrieval of all 
     * attributes and operations of this MBean.
     */
    public MBeanInfo getMBeanInfo(ObjectName name)
	throws InstanceNotFoundException,
	       IntrospectionException,
	       ReflectionException,
	       RemoteException 
    {
	return this.server.getMBeanInfo(name);
    }
}
