/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2006 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.management.cluster;

import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.objectweb.jonas.common.Log;
import org.objectweb.jonas.management.monitoring.ServerProxy;
import org.objectweb.jonas.server.J2EEServer;
import org.objectweb.util.monolog.api.Logger;


/**
 * A ClusterMember object refers to the ServerProxy corresponding
 * to the managed server which is being member of a given cluster.
 * There may be several ClusterMember instances for the same ServerProxy object.
 * @author Philippe Durieux
 */
public abstract class ClusterMember {

    /**
     * Name of this Member. Depend on the derived class.
     */
    protected String name;

    /**
     * MBean ObjectName
     */
    protected ObjectName objectName = null;

    /**
     * The ServerProxy representing the managed server.
     */
    protected ServerProxy proxy = null;

    /**
     * The J2EEServer object
     */
    protected J2EEServer j2eeServer = null;

    /**
     * MBeanServer where the MBean is registered.
     */
    protected MBeanServer mbeanServer = null;

    /**
     * logger for traces
     */
    protected static Logger logger = Log.getLogger("org.objectweb.jonas.domain.management");

    /**
     * Constructor in case of Remote Server
     * @param name serverName
     * @param proxy The ServerProxy representing the managed server.
     */
    public ClusterMember(String name, ServerProxy proxy) {
        this.name = name;
        this.proxy = proxy;
    }

    /**
     * Constructor in case of the Local Server
     * @param name serverName
     * @param the J2EEServer object
     */
    public ClusterMember(String name, J2EEServer server) {
        this.name = name;
        this.j2eeServer = server;
    }


    /**
     * @return the MBeanServer reference
     */
    public MBeanServer getMbeanServer() {
        return mbeanServer;
    }

    /**
     * Set the MBeanServer reference
     * @param mbeanServer MBeanServer reference
     */
    public void setMbeanServer(MBeanServer mbeanServer) {
        this.mbeanServer = mbeanServer;
    }

    /**
     * @return the member state if it is connected to a ServerProxy as a String
     * @throws JMException could not get ServerProxy state because of a JMX exception
     */
    public String getState() {
        String state = "NOT_CONNECTED";
        if (proxy != null) {
            state = proxy.getState();
        }
        return state;
    }

    /**
     * @return the member state if it is connected to a ServerProxy.
     * @throws JMException could not get ServerProxy state because of a JMX exception
     */
    public int getServerState() {
        int state = ServerProxy.UNKNOWN;
        if (proxy != null) {
            state = proxy.getServerState();
        }
        return state;
    }

    /**
     * @return name of this member
     */
    public String getName() {
        return name;
    }

    /**
     * Get the Mbean ObjectName (for jonasAdmin)
     * @return ObjectName of this member
     */
    public ObjectName getObjectName() {
        return objectName;
    }

    /**
     * Set the ObjectName. Should be called when the MBean is registered.
     */
    public void setObjectName(ObjectName on) {
        objectName = on;
    }

    /**
     * @return The ServerProxy OBJECT_NAME referenced by this cluster member
     */
    public String getServerProxy() {
        return proxy.getObjectName();
    }

    /**
     * @return The proxy pointed by this Member
     */
    public ServerProxy getProxy() {
        return proxy;
    }

    /**
     * TODO: J2EEServer and ServerProxy should be the same object
     * @return The J2EEServer pointed by this Member
     */
    public J2EEServer getJ2EEServer() {
        if (j2eeServer != null) {
            return j2eeServer;
        }
        throw new RuntimeException("Not implemented yet in case of remote server");
    }

    /**
     * The JOnAS server's name which is represented by the proxy, null if the memeber
     * is not connected to a proxy
     * @return the JOnAS server's name which is represented by the proxy
     * @throws JMException could not get serverName from the ServerProxy because of a JMX exception
     */
    public String getServerName() throws JMException {
        String serverName = null;
        if (proxy != null) {
            serverName = proxy.getServerName();
        }
        return serverName;
    }

}