/*
 * Copyright 2012 The Clustermeister Team.
 *
 * 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 com.github.nethad.clustermeister.integration;

import com.github.nethad.clustermeister.provisioning.rmi.NodeConnectionListener;
import org.jppf.management.JPPFManagementInfo;
import org.jppf.management.JPPFSystemInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author thomas
 */
public abstract class AbstractScenario implements NodeConnectionListener {
    
    private JPPFTestDriver driver;
    private JPPFTestNode node;
    private StringBuilder report = new StringBuilder("EXECUTION REPORT:\n");
    private boolean scenarioStarted = false;
    private boolean shuttingDown = false;
    
    public abstract void runScenario() throws Exception;
    
    protected void addToReport(String key, Object value) {
        report.append(key).append("\t").append(value.toString()).append("\n");
    }

    protected void execute() throws InterruptedException {
        startDriver();
        startNode();
    }

    private void startDriver() {
        driver = new JPPFTestDriver(this);
        driver.startDriver();
    }

    private void startNode() throws RuntimeException {
        node = new JPPFTestNode();
        node.prepare();
        node.startNode();
    }

    public void runScenario_wrapper() throws Exception {
        try {
            runScenario();
        } finally {
            shutdown();
        }
    }
    
    private void shutdown() {
        shuttingDown = true;
        node.shutdown();
        driver.shutdown();
        System.out.println(report.toString());
        System.exit(0);
    }

    @Override
    public void onNodeConnected(JPPFManagementInfo jppfmi, JPPFSystemInformation jppfsi) {
        if (!scenarioStarted) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        runScenario_wrapper();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }).start();
            scenarioStarted = true;
        }
    }

    @Override
    public void onNodeDisconnected(JPPFManagementInfo jppfmi) {
        if (!shuttingDown) {
            shutdown();
        }
    }
    
}

@@ -1232,21 +1232,59 @@
  false;%0A
+    private boolean startNode = true;%0A
     %0A
-
     publ
@@ -1568,24 +1568,28 @@
 ();%0A        
+if (
 startNode();
@@ -1577,32 +1577,209 @@
    if (startNode
+) %7B%0A            startNode();%0A        %7D%0A    %7D%0A    %0A    protected void execute(boolean startNode) throws InterruptedException %7B%0A        this.startNode = startNode;%0A        execute
 ();%0A    %7D%0A%0A    p
@@ -2247,32 +2247,64 @@
 ingDown = true;%0A
+        if (node != null) %7B%0A    
         node.shu
@@ -2304,32 +2304,42 @@
 ode.shutdown();%0A
+        %7D%0A
         driver.s

make node start optional