/*
 * Copyright 2008-2009 LinkedIn, Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package voldemort.server;

import java.util.concurrent.atomic.AtomicBoolean;

import javax.management.MBeanOperationInfo;

import org.apache.log4j.Logger;

import voldemort.annotations.jmx.JmxGetter;
import voldemort.annotations.jmx.JmxOperation;

/**
 * A helper template for implementing VoldemortService
 * 
 * @author jay
 * 
 */
public abstract class AbstractService implements VoldemortService {

    private static final Logger logger = Logger.getLogger(VoldemortService.class);

    private AtomicBoolean isStarted;
    private final String name;

    public AbstractService(String name) {
        this.name = name;
        this.isStarted = new AtomicBoolean(false);
    }

    public String getName() {
        return name;
    }

    @JmxGetter(name = "started", description = "Determine if the service has been started.")
    public boolean isStarted() {
        return isStarted.get();
    }

    @JmxOperation(description = "Start the service.", impact = MBeanOperationInfo.ACTION)
    public void start() {
        boolean isntStarted = isStarted.compareAndSet(false, true);
        if(!isntStarted)
            throw new IllegalStateException("Server is already started!");

        logger.info("Starting " + getName());
        startInner();
    }

    @JmxOperation(description = "Stop the service.", impact = MBeanOperationInfo.ACTION)
    public void stop() {
        logger.info("Stopping " + getName());
        synchronized(this) {
            if(!isStarted()) {
                logger.info("The service is already stopped, ignoring duplicate attempt.");
            }

            stopInner();
            isStarted.set(false);
        }
    }

    protected abstract void startInner();

    protected abstract void stopInner();

}

/*
 * Copyright 2008-2009 LinkedIn, Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package voldemort.server;

import java.util.concurrent.atomic.AtomicBoolean;

import javax.management.MBeanOperationInfo;

import org.apache.log4j.Logger;

import voldemort.annotations.jmx.JmxGetter;
import voldemort.annotations.jmx.JmxOperation;

/**
 * A helper template for implementing VoldemortService
 * 
 * @author jay
 * 
 */
public abstract class AbstractService implements VoldemortService {

    private static final Logger logger = Logger.getLogger(VoldemortService.class);

    private final AtomicBoolean isStarted;
    private final String name;

    public AbstractService(String name) {
        this.name = name;
        this.isStarted = new AtomicBoolean(false);
    }

    public String getName() {
        return name;
    }

    @JmxGetter(name = "started", description = "Determine if the service has been started.")
    public boolean isStarted() {
        return isStarted.get();
    }

    @JmxOperation(description = "Start the service.", impact = MBeanOperationInfo.ACTION)
    public void start() {
        boolean isntStarted = isStarted.compareAndSet(false, true);
        if(!isntStarted)
            throw new IllegalStateException("Server is already started!");

        logger.info("Starting " + getName());
        startInner();
    }

    @JmxOperation(description = "Stop the service.", impact = MBeanOperationInfo.ACTION)
    public void stop() {
        logger.info("Stopping " + getName());
        synchronized(this) {
            if(!isStarted()) {
                logger.info("The service is already stopped, ignoring duplicate attempt.");
            }

            stopInner();
            isStarted.set(false);
        }
    }

    protected abstract void startInner();

    protected abstract void stopInner();

}

Add final modifier to AtomicBoolean isStarted.