/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2006 Bull S.A.S.
 * Contact: jonas-team@objectweb.org
 * Copyright (C) 2006 Distributed Systems Lab.
 * Universidad Politécnica de Madrid (Spain)
 * Contact: http://lsd.ls.fi.upm.es/lsd
 *
 * 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.ha;

import javax.naming.Context;

import org.objectweb.carol.cmi.configuration.TraceCmi;
import org.objectweb.carol.cmi.ha.ReplicationManager;
import org.objectweb.carol.cmi.ha.ReplicationManagerImpl;
import org.objectweb.carol.cmi.ha.interceptor.HACurrent;
import org.objectweb.carol.cmi.ha.interceptor.HACurrentDelegate;
import org.objectweb.jonas.ha.interceptor.HACurrentDelegateImpl;
import org.objectweb.jonas.service.AbsServiceImpl;
import org.objectweb.jonas.service.ServiceException;

/**
 * This class binds the HA service to Jonas and initializes the HA architecture
 * @author Francisco Perez-Sorrosal (fpsorrosal@no-spam@fi.upm.es)
 * @author Alberto Paz-Jimenez (apaz@no-spam@fi.upm.es)
 * @author benoit pelletier
 */
public class HaServiceImpl extends AbsServiceImpl implements HaService {

    private final String SERVICE_NAME = "HAService";
    
    private boolean started = false;

    private ReplicationManager replicationMgr;

    private HACurrentDelegate haCurrentDlg = null;
    
    public void doInit(Context ctx) throws ServiceException {
        TraceCmi.infoCmiHA("Initializing replication service...");
        try {
            
			replicationMgr = new ReplicationManagerImpl(ctx);
			
            // Init the HA current class
            haCurrentDlg = HACurrentDelegateImpl.getCurrent();
            HACurrent.getHACurrent().setDlg(haCurrentDlg);

            if (TraceCmi.isDebugCmiHA()) {
                TraceCmi.debugCmiHA("replicationMgr=" + replicationMgr);
                TraceCmi.debugCmiHA("haCurrentDlg=" + haCurrentDlg);
            }

        } catch (Exception e) {
            throw new ServiceException("Error in GCL name"); 
        }
    }

    public void doStart() throws ServiceException {
        // COMPLETE: Allow to start and shutdown service dinamically
        started = true;
        TraceCmi.infoCmiHA("Replication service started");
    }

    public void doStop() throws ServiceException {
        // COMPLETE: Allow to start and shutdown service dinamically
        if (started) {
            started = false;
            replicationMgr.clear(); // Turn off the GCL
            TraceCmi.infoCmiHA("Replication service stopped");
        }
    }

    public boolean isStarted() {
        return started;
    }

    public void setName(String name) {
        // Nothing to do...
    }

    public String getName() {
        return SERVICE_NAME;
    }

    /**
     * Return the replication manager
     */
    public ReplicationManager getReplicationManager() {

        if (TraceCmi.isDebugCmiHA()) {
            TraceCmi.debugCmiHA("replicationMgr=" + replicationMgr);
        }

        return replicationMgr;
    }
}
