/**
 * 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 java.util.Collection;
import java.util.HashMap;

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

import org.objectweb.jonas.management.monitoring.DomainMonitor;
import org.objectweb.jonas.management.monitoring.ServerProxy;

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

/**
 * Factory for logical clusters
 */
public class LogicalClusterFactory extends ClusterFactory {

    /**
     * List of the logical clusters in this domain.
     * key = clusterName, value = LogicalCluster
     */
    private HashMap myclusters = new HashMap();

    public LogicalClusterFactory(DomainMonitor dm) {
        super(dm);
    }

    public BaseCluster getCluster(String name) {
        return (BaseCluster) myclusters.get(name);
    }

    public boolean notifyServer(ServerProxy proxy) {
        String serverName = proxy.getServerName();
        logger.log(BasicLevel.DEBUG, serverName);

        LogicalCluster domaincluster = (LogicalCluster) myclusters.get(domainName);
        if (domaincluster == null) {
            domaincluster = createLogicalCluster(domainName);
            if (domaincluster == null) {
                return false;
            }
        }

        // add a server to the default cluster
        return domaincluster.addServer(serverName, proxy);
    }

    public Collection getClusterList() {
        return myclusters.values();
    }

    /**
     * Create a logical cluster
     */
    public LogicalCluster createLogicalCluster(String name) {
        LogicalCluster cluster = null;
        ObjectName clon = null;
        try {
            cluster = new LogicalCluster(this);
            clon = cluster.setName(name);
        } catch (JMException e) {
            logger.log(BasicLevel.ERROR, "Cannot create LogicalCluster:" + e);
            return null;
        }
        // Register the MBean if not done
        if (!mbeanServer.isRegistered(clon)) {
            try {
                // A MBean is registered for each Cluster
                logger.log(BasicLevel.DEBUG, "Resister Cluster MBean : "  + clon);
                mbeanServer.registerMBean(cluster, clon);
            } catch (Exception e) {
                logger.log(BasicLevel.ERROR, "Cannot register cluster:" + e);
                return null;
            }
        }
        if (logger.isLoggable(BasicLevel.DEBUG)) {
            logger.log(BasicLevel.DEBUG, "Adding cluster : "  + name);
        }
        myclusters.put(name, cluster);
        return cluster;
    }

}