/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999-2004 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.service;

import javax.naming.Context;

import org.objectweb.jonas.common.JProp;
import org.objectweb.jonas.management.ReconfigDispatcher;

/**
 * Abstract implementation of a Service
 */
public abstract class AbsServiceImpl extends ReconfigDispatcher implements Service {

    /**
     * service name
     */
    private String name = null;
    /**
     * service status
     */
    private boolean started = false;

    /**
     * Name of the domain of server
     */
    private String domainName = null;

    /**
     * Name of the JOnAS server
     */
    private String jonasServerName = null;


    /**
     * Initialize the service
     * @param ctx configuration of the service
     * @throws ServiceException service initialization failed
     */
    public void init(Context ctx) throws ServiceException {
        // General properties infos
        try {
            JProp oProp = JProp.getInstance();
            domainName = oProp.getValue(JProp.DOMAIN_NAME, JProp.JONAS_DEF_NAME);
            jonasServerName = oProp.getValue(JProp.JONAS_NAME, JProp.JONAS_DEF_NAME);
        } catch (Exception e) {
            domainName = JProp.JONAS_DEF_NAME;
            jonasServerName = JProp.JONAS_DEF_NAME;
        }
        this.doInit(ctx);
    }

    /**
     * Start the service
     * @throws ServiceException service start-up failed
     */
    public void start() throws ServiceException {
        this.doStart();
        this.started = true;
    }

    /**
     * Stop the service
     * @throws ServiceException service stopping failed
     */
    public void stop() throws ServiceException {
        if (this.isStarted()) {
            this.started = false;
            this.doStop();
        }
    }

    /**
     * Returns true if the service is started, false otherwise
     * @return true if the service is started
     */
    public boolean isStarted() {
        return this.started;
    }

    /**
     * Returns the service's name
     * @return the service name
     */
    public String getName() {
        return this.name;
    }

    /**
     * Set the service's name
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }


    /**
     * Return the domain's name of this service.
     * @return the domain name
     */
    public String getDomainName() {
        return this.domainName;
    }

    /**
     * Return the JOnAS server's name of this service.
     * @return the server name
     */
    public String getJonasServerName() {
        return this.jonasServerName;
    }

    /**
     * Abstract initialization method to be implemented by sub-classes
     * @param ctx configuration of the service
     * @throws ServiceException service initialization failed
     */
    protected abstract void doInit(Context ctx) throws ServiceException;
    /**
     * Abstract start-up method to be implemented by sub-classes
     * @throws ServiceException service start-up failed
     */
    protected abstract void doStart() throws ServiceException;
    /**
     * Abstract method for service stopping to be implemented by sub-classes
     * @throws ServiceException service stopping failed
     */
    protected abstract void doStop() throws ServiceException;
}
