/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2008 Bull S.A.
 * Contact: jonas-team@ow2.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.ow2.jonas.lib.management.extensions.base;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import org.objectweb.util.monolog.api.Logger;
import org.ow2.jonas.lib.management.tools.ManagementRepr;
import org.ow2.jonas.lib.management.tools.WhereAreYou;
import org.ow2.jonas.lib.util.Log;


/**
 * Base class for JOnAS management.
 * This class is extended by classes specialized for specific management aspects.
 * @author Adriana.Danes@bull.net
 * @author Oualaa Hani
 */
public class BaseManagement {
    protected static Logger logger = Log.getLogger("org.ow2.jonas.management.extension");
    /**
     * Current server name.
     */
    private String serverName = null;
    /**
     * Current domain name.
     */
    private String domainName = null;

    /**
     * Base constructor.
     */
    public BaseManagement() {
        serverName = WhereAreYou.getServerName();
        domainName = WhereAreYou.getDomainName();
    }

    /**
     * @return The current server name.
     */
    public String getServerName() {
        return serverName;
    }

    /**
     * @return The current domain name.
     */
    public String getDomainName() {
        return domainName;
    }

    /**
     * Get the String attribute value of an MBean in the current MBean Server.
     * @param objectName The MBean's ObjectName
     * @param attrName The attribute name
     * @return The value of the attribute
     */
    protected String getStringAttribute(final ObjectName objectName, final String attrName) {
        String pServer = getServerName();
        String s = (String) ManagementRepr.getAttribute(objectName, attrName, pServer);
        return s;
    }

    /**
     * Get the integer attribute value of an MBean in the current MBean Server.
     * @param objectName The MBean's ObjectName
     * @param attrName The attribute name
     * @return The value of the attribute
     */
    protected int getIntegerAttribute(final ObjectName objectName, final String attrName) {
        String pServer = getServerName();
        Integer o = (Integer) ManagementRepr.getAttribute(objectName, attrName, pServer);
        return o.intValue();
    }

    /**
     * Return the value of a key property in an OBJECT_NAME.
     * @param objectName the OBJECT_NAME (String form)
     * @param keyName key property name
     * @return key value
     */
    protected String getKeyValue(final String objectName, final String keyName) {
        ObjectName on = null;
        try {
            on = ObjectName.getInstance(objectName);
        } catch (MalformedObjectNameException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } catch (NullPointerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        return on.getKeyProperty(keyName);
    }

    /**
     * Return the values of a key property in String OBJECT_NAMEs.
     * @param objectNames the OBJECT_NAMEs
     * @param keyName key name
     * @return array of key values
     */
    protected String[] getKeyValues(final String[] objectNames, final String keyName) {
        ObjectName on = null;
        String[] keyValues = new String[objectNames.length];
        for (int i = 0; i < objectNames.length; i++) {
            String s_on = objectNames[i];
            try {
                on = ObjectName.getInstance(s_on);
            } catch (MalformedObjectNameException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                keyValues[i] = null;
            } catch (NullPointerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                keyValues[i] = null;
            }
            keyValues[i] = on.getKeyProperty(keyName);
        }
        return keyValues;
    }
}
