/*
 * Copyright 2014 Timothy Brooks
 *
 * 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 net.uncontended.precipice;

import net.uncontended.precipice.circuit.CircuitBreaker;
import net.uncontended.precipice.concurrent.IntegerSemaphore;
import net.uncontended.precipice.concurrent.PrecipiceSemaphore;
import net.uncontended.precipice.metrics.ActionMetrics;
import net.uncontended.precipice.metrics.Metric;

import java.util.concurrent.atomic.AtomicBoolean;

public abstract class AbstractService implements Service {
    protected final PrecipiceSemaphore semaphore;
    protected final AtomicBoolean isShutdown = new AtomicBoolean(false);
    protected final ActionMetrics actionMetrics;
    protected final CircuitBreaker circuitBreaker;

    public AbstractService(CircuitBreaker circuitBreaker, ActionMetrics actionMetrics, PrecipiceSemaphore semaphore) {
        this.circuitBreaker = circuitBreaker;
        this.actionMetrics = actionMetrics;
        this.semaphore = semaphore;
    }

    @Override
    public ActionMetrics getActionMetrics() {
        return actionMetrics;
    }

    @Override
    public CircuitBreaker getCircuitBreaker() {
        return circuitBreaker;
    }

    protected void acquirePermitOrRejectIfActionNotAllowed() {
        if (isShutdown.get()) {
            throw new RejectedActionException(RejectionReason.SERVICE_SHUTDOWN);
        }

        boolean isPermitAcquired = semaphore.acquirePermit();
        if (!isPermitAcquired) {
            actionMetrics.incrementMetricCount(Metric.MAX_CONCURRENCY_LEVEL_EXCEEDED);
            throw new RejectedActionException(RejectionReason.MAX_CONCURRENCY_LEVEL_EXCEEDED);
        }

        if (!circuitBreaker.allowAction()) {
            actionMetrics.incrementMetricCount(Metric.CIRCUIT_OPEN);
            semaphore.releasePermit();
            throw new RejectedActionException(RejectionReason.CIRCUIT_OPEN);
        }
    }
}

@@ -688,70 +688,8 @@
 er;%0A
-import net.uncontended.precipice.concurrent.IntegerSemaphore;%0A
 impo
@@ -748,16 +748,16 @@
 aphore;%0A
+
 import n
@@ -1062,35 +1062,8 @@
 down
- = new AtomicBoolean(false)
 ;%0A  
@@ -1276,24 +1276,285 @@
 emaphore) %7B%0A
+        this(circuitBreaker, actionMetrics, semaphore, new AtomicBoolean(false));%0A    %7D%0A%0A    public AbstractService(CircuitBreaker circuitBreaker, ActionMetrics actionMetrics, PrecipiceSemaphore semaphore,%0A                           AtomicBoolean isShutdown) %7B%0A
         this
@@ -1627,24 +1627,24 @@
 ionMetrics;%0A
-
         this
@@ -1663,24 +1663,62 @@
  semaphore;%0A
+        this.isShutdown = isShutdown;%0A
     %7D%0A%0A    @

Add option to pass shutdown flag to abstract service