/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 com.streamsets.pipeline.task;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.streamsets.pipeline.container.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

public abstract class AbstractTask implements Task {
  private static final Logger LOG = LoggerFactory.getLogger(AbstractTask.class);
  private static final Map<Status, Set<Status>> VALID_TRANSITIONS = ImmutableMap.of(
      Status.CREATED, (Set<Status>)ImmutableSet.of(Status.INITIALIZED),
      Status.INITIALIZED, ImmutableSet.of(Status.RUNNING, Status.STOPPED),
      Status.RUNNING, ImmutableSet.of(Status.STOPPED),
      Status.STOPPED, ImmutableSet.of(Status.INITIALIZED, Status.STOPPED),
      Status.ERROR, ImmutableSet.<Status>of()
  );

  private static final String STATE_ERROR_MSG = "Current status is '{}'";
  private final String name;
  private final CountDownLatch latch;
  private volatile Status status;

  public AbstractTask(String name) {
    this.name = Preconditions.checkNotNull(name, "name cannot be null");
    setStatus(Status.CREATED);
    latch = new CountDownLatch(1);
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public synchronized void init() {
    Preconditions.checkState(VALID_TRANSITIONS.get(getStatus()).contains(Status.INITIALIZED),
                             Utils.format(STATE_ERROR_MSG, getStatus()));
    try {
      LOG.debug("Task '{}' initializing", getName());
      initTask();
      LOG.debug("Task '{}' initialized", getName());
    } catch (RuntimeException ex) {
      LOG.warn("Task '{}' failed to initialize, {}, calling stopTask() and going into ERROR", getName(),
               ex.getMessage(), ex);
      safeStop(Status.ERROR);
      throw ex;
    }
    setStatus(Status.INITIALIZED);
  }

  @Override
  public synchronized void run() {
    Preconditions.checkState(VALID_TRANSITIONS.get(getStatus()).contains(Status.RUNNING),
                             Utils.format(STATE_ERROR_MSG, getStatus()));
    try {
      LOG.debug("Task '{}' starting", getName());
      runTask();
      LOG.debug("Task '{}' running", getName());
    } catch (RuntimeException ex) {
      LOG.warn("Task '{}' failed to start, {}, calling stopTask() and going into ERROR", getName(), ex.getMessage(),
               ex);
      safeStop(Status.ERROR);
      throw ex;
    }
    setStatus(Status.RUNNING);
  }

  @Override
  public synchronized void stop() {
    Preconditions.checkState(VALID_TRANSITIONS.get(getStatus()).contains(Status.STOPPED),
                             Utils.format(STATE_ERROR_MSG, getStatus()));
    if (getStatus() != Status.STOPPED) {
      LOG.debug("Task '{}' stopping", getName());
      safeStop(Status.STOPPED);
    }
  }

  private void safeStop(Status endStatus) {
    try {
      stopTask();
      LOG.debug("Task '{}' stopped from status '{}'", getName(), getStatus());
      setStatus(endStatus);
    } catch (RuntimeException ex) {
      LOG.warn("Task '{}' failed to stop properly, {}", getName(), ex.getMessage(), ex);
      setStatus(Status.ERROR);
    }
  }

  private void setStatus(Status status) {
    if (this.status == Status.RUNNING) {
      latch.countDown();
    }
    this.status = status;
  }

  @Override
  public Status getStatus() {
    return status;
  }

  @Override
  public void waitWhileRunning() throws InterruptedException {
    Preconditions.checkState(getStatus() == Status.RUNNING || getStatus() == Status.STOPPED,
                             Utils.format(STATE_ERROR_MSG, getStatus()));
    if (getStatus() == Status.RUNNING) {
      latch.await();
    }
  }

  @Override
  public String toString() {
    return Utils.format("{}[status='{}']", getName(), getStatus());
  }

  protected void initTask() {
  }

  protected void runTask() {
  }

  protected void stopTask() {
  }

}

@@ -1632,36 +1632,16 @@
 eSet.of(
-Status.INITIALIZED, 
 Status.S

Fixing task stop valid transitions