package org.ow2.chameleon.wisdom.maven.mojos;

import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.ow2.chameleon.wisdom.maven.processors.Pipeline;
import org.ow2.chameleon.wisdom.maven.processors.Pipelines;
import org.ow2.chameleon.wisdom.maven.processors.ProcessorException;
import org.ow2.chameleon.wisdom.maven.utils.DependencyCopy;
import org.ow2.chameleon.wisdom.maven.utils.WisdomExecutor;
import org.ow2.chameleon.wisdom.maven.utils.WisdomRuntimeExpander;

import java.io.IOException;

/**
 * Run Mojo
 */
@Mojo(name = "run", threadSafe = false,
        requiresDependencyResolution = ResolutionScope.COMPILE,
        requiresDirectInvocation = true,
        requiresProject = true)
public class RunMojo extends AbstractWisdomMojo {

    private Pipeline pipeline;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        try {
            init();
        } catch (ProcessorException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }

        new WisdomExecutor().execute(this);

        if (pipeline != null) {
            pipeline.tearDown();
        }

    }

    public void init() throws MojoExecutionException, ProcessorException {
        // Expand if needed.
        if (WisdomRuntimeExpander.expand(this, getWisdomRootDirectory())) {
            getLog().info("Wisdom Runtime installed in " + getWisdomRootDirectory().getAbsolutePath());
        }

        // Copy compile dependencies that are bundles to the application directory.
        try {
            DependencyCopy.copy(this);
        } catch (IOException e) {
            throw new MojoExecutionException("Cannot copy dependencies", e);
        }
        pipeline = Pipelines.watcher()
                .initialize(this)
                .watch();
    }


}

@@ -200,32 +200,145 @@
 ilureException;%0A
+import org.apache.maven.plugins.annotations.Execute;%0Aimport org.apache.maven.plugins.annotations.LifecyclePhase;%0A
 import org.apach
@@ -371,16 +371,16 @@
 s.Mojo;%0A
-
 import o
@@ -1032,16 +1032,66 @@
 t = true
+%0A        )%0A@Execute(phase = LifecyclePhase.PACKAGE
 )%0Apublic
@@ -1139,40 +1139,8 @@
  %7B%0A%0A
-    private Pipeline pipeline;%0A%0A
     
@@ -1428,85 +1428,8 @@
 s);%0A
-%0A        if (pipeline != null) %7B%0A            pipeline.tearDown();%0A        %7D%0A%0A
     
@@ -1984,24 +1984,25 @@
       %7D%0A
+%0A
         
 pipeline
@@ -1997,71 +1997,37 @@
     
-pipeline = Pipelines.watcher()%0A                .initialize(this
+Pipelines.fromSession(session
 )%0A  

Modify the run mojo to use the new pipeline