/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 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
 *
 * Initial developer(s): ____________________________________.
 * Contributor(s): 
 *   Julien Lehembre (Libelis)
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.registry;

import javax.naming.Context;
import javax.naming.NamingException;

import org.objectweb.carol.jndi.ns.NameServiceManager;
import org.objectweb.jonas.naming.NamingManager;
import org.objectweb.jonas.service.AbsServiceImpl;
import org.objectweb.jonas.service.ServiceException;

/**
 * @author Helene Joanin Contributor(s): Julien Lehembre (Libelis)
 *  
 */

public class RegistryServiceImpl extends AbsServiceImpl implements
        RegistryService {

    // Registry service configuration properties
    public static final String MODE = "jonas.service.registry.mode";

    public static final String CLASS = "jonas.service.registry.class";

    public static final String DEFAULT_MODE = "automatic";

    public static final String COLLOCATED = "collocated";

    public static final String REMOTE = "remote";

    boolean bStartRegistry = true;

    boolean bIgnoreError = true;

    public RegistryServiceImpl() {
    }

    public void doInit(Context ctx) {

        String propRegistry = null;
        try {
            propRegistry = (String) ctx.lookup(MODE);
        } catch (NamingException e) {
            // No problem if there is no value --> default value
            propRegistry = DEFAULT_MODE;
        }

        if (DEFAULT_MODE.equalsIgnoreCase(propRegistry)) {
            bStartRegistry = true;
            bIgnoreError = true;
        } else if (COLLOCATED.equalsIgnoreCase(propRegistry)) {
            bStartRegistry = true;
            bIgnoreError = false;
        } else if (REMOTE.equalsIgnoreCase(propRegistry)) {
            bStartRegistry = false;
        }

    }

    public void doStart() throws ServiceException {
        if (bStartRegistry) {
            try {
                // TODO : remove old lines
                if (bIgnoreError) {
                    // start the carol name service manager
                    // NameServiceManager.getNSManagerCurrent().startNonStartedNS();
                    NameServiceManager.startNonStartedNS();
                } else {
                    // NameServiceManager.getNSManagerCurrent().startNS();
                    NameServiceManager.startNS();
                }
            } catch (Exception e) {
                throw new ServiceException("JOnAS: Cannot start the registry ("
                        + e + ")");
            }
        }

        // Start the NamingManager now to see quickly if there is
        // a problem with naming.
        try {
            NamingManager.getInstance();
        } catch (NamingException e) {
            throw new ServiceException("JOnAS: Cannot start Naming Manager ("
                    + e + ")");
        }

    }

    public void doStop() throws ServiceException {
        try {
            //stop the carol name service manager
            // TODO : remove old line
            // NameServiceManager.getNSManagerCurrent().stopNS();
            NameServiceManager.stopNS();
        } catch (Exception e) {
            throw new ServiceException("Pb when stopping registry service "
                    + e.getMessage());
        }
    }
}
