/**
 * 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.jmx.JonasObjectName;
import org.objectweb.jonas.management.monitoring.ServerProxy;

import org.objectweb.util.monolog.api.BasicLevel;

/**
 * Implements Tomcat Cluster MBean
 * @author Philippe Durieux
 */
public class CmiCluster extends BaseCluster implements CmiClusterMBean {

    /**
     * The type of Cluster, that is part of the MBean ObjectName
     */
    protected String type = "CmiCluster";

    /**
     * mcast port
     */
    protected int mcastPort;

    /**
     * mcast addr
     */
    protected String mcastAddr;

    /**
     * protocol used
     */
    protected String protocol;

    /**
     * CMI cluster constructor
     * @param cf ClusterFactory
     * @throws JMException could not create MBean instance
     */
    public CmiCluster(CmiClusterFactory cf) throws JMException {
        super(cf);
    }

    public ClusterMember createClusterMember(String svname, ServerProxy proxy) {
        return new CmiClusterMember(svname, proxy);
    }

    // --------------------------------------------------------------------------
    // Other public methods
    // --------------------------------------------------------------------------

    /**
     * @return the mcast port
     */
    public int getMcastPort() {
        return mcastPort;
    }

    /**
     * Set the multicast port
     */
    public void setMcastPort(int port) {
        mcastPort = port;
    }

    /**
     * @return the mcast addr
     */
    public String getMcastAddr() {
        return mcastAddr;
    }

    /**
     * Set the multicast addr
     */
    public void setMcastAddr(String addr) {
        mcastAddr = addr;
    }

    /**
     * @return the protocol
     */
    public String getProtocol() {
        return protocol;
    }

    /**
     * Set the protocol
     */
    public void setProtocol(String proto) {
        protocol = proto;
    }

    /**
     * @return The String type to be put in the ObjectName
     */
    public String getType() {
        return type;
    }

    /**
     * Add a CMI Server to the list of the Cluster
     * Make link between the member and the ServerProxy.
     * @param serverName name of the managed server which corresponds to a Tomcat session replication cluster memeber
     * @param proxy The ServerProxy related object.
     * @return True if correctly added in the List.
     */
    public boolean addCmiServer(String serverName, ServerProxy proxy) {

        // create the CmiClusterMember instance ( = a jonas server)
        CmiClusterMember cmi = new CmiClusterMember(serverName, proxy);

        // Add this member if not already there
        boolean added = addMember(cmi);
        if (added) {
            // Build the ObjectName and register MBean
            try {
                ObjectName on = JonasObjectName.clusterMember(serverName, getType(), name);
                cmi.setObjectName(on);
                MBeanServer mbeanServer = jmx.getJmxServer();
                if (mbeanServer.isRegistered(on)) {
                    mbeanServer.unregisterMBean(on);
                }
                mbeanServer.registerMBean(cmi, on);
            } catch (JMException e) {
                logger.log(BasicLevel.WARN, "Cannot register tomcat " + serverName + ": " + e);
            }
        }
        return added;
    }

}