package net.hyperic.hq.product.mx4j;

import net.hyperic.hq.product.ControlPlugin;
import net.hyperic.hq.product.ControlPluginManager;

import net.hyperic.hq.product.PluginException;
import net.hyperic.hq.product.PluginManager;
import net.hyperic.util.config.ConfigResponse;

public class MX4JControlPlugin 
    extends ControlPlugin
{
    public void doAction(String action)
        throws PluginException
    {
        String jmxUrl = this.config.getValue(MX4JUtil.PROP_JMXURL);
        String name = this.config.getValue(MX4JDetector.PROP_SERVICE_NAME);
        String object = "hyperic:type=EchoServer,name=" + name;

        // Default to error
        setResult(RESULT_FAILURE);

        if (action.equals("start")) {
            MX4JUtil.invoke(jmxUrl, object, action);
            setResult(RESULT_SUCCESS);
        } else if (action.equals("stop")) {
            MX4JUtil.invoke(jmxUrl, object, action);
            setResult(RESULT_SUCCESS);
        }
    }

    protected boolean isRunning() {

        String jmxUrl = this.config.getValue(MX4JUtil.PROP_JMXURL);
        String name = this.config.getValue(MX4JDetector.PROP_SERVICE_NAME);
        String object = "hyperic:type=EchoServer,name=" + name;
        
        Integer avail;
        try {
            avail = (Integer)MX4JUtil.getValue(jmxUrl, object,
                                               "Availability");
        } catch (Exception e) {
            return false;
        }
        
        if (avail.intValue() == 0) {
            return false;
        } else {
            return true;
        }
    }
}
