/**
 * (c) 2014 StreamSets, Inc. All rights reserved. May not
 * be copied, modified, or distributed in whole or part without
 * written consent of StreamSets, Inc.
 */
package com.streamsets.pipeline.runner.preview;

import com.streamsets.pipeline.api.StageException;
import com.streamsets.pipeline.runner.Pipeline;
import com.streamsets.pipeline.runner.PipelineRuntimeException;
import com.streamsets.pipeline.runner.StageOutput;
import com.streamsets.pipeline.validation.Issues;

import java.util.Collections;
import java.util.List;

public class PreviewPipeline {
  private final Pipeline pipeline;
  private final Issues issues;

  public PreviewPipeline(Pipeline pipeline, Issues issues) {
    this.issues = issues;
    this.pipeline = pipeline;
  }

  @SuppressWarnings("unchecked")
  public PreviewPipelineOutput run() throws StageException, PipelineRuntimeException{
    return run(Collections.EMPTY_LIST);
  }

  public PreviewPipelineOutput run(List<StageOutput> stageOutputsToOverride)
      throws StageException, PipelineRuntimeException{
    pipeline.init();
    pipeline.run(stageOutputsToOverride);
    pipeline.destroy();
    return new PreviewPipelineOutput(issues, pipeline.getRunner());
  }

}

@@ -1063,24 +1063,36 @@
 ine.init();%0A
+    try %7B%0A  
     pipeline
@@ -1121,16 +1121,34 @@
 rride);%0A
+    %7D finally %7B%0A  
     pipe
@@ -1159,24 +1159,30 @@
 .destroy();%0A
+    %7D%0A
     return n

Handle exception and clean up during preview