package org.jenkinsci.plugins.radargun.config;

import hudson.DescriptorExtensionList;
import hudson.FilePath;
import hudson.model.Describable;
import hudson.model.AbstractBuild;
import hudson.model.Descriptor;

import java.io.IOException;

import jenkins.model.Jenkins;

import org.jenkinsci.plugins.radargun.util.Resolver;

/**
 * ScenarioSource
 * 
 * @author vjuranek
 * 
 */
public abstract class ScenarioSource implements Describable<ScenarioSource> {

    protected static final String DEFAULT_SCENARIO_NAME = "radarGunScenario";
    protected static final String DEFAULT_SCENARIO_SUFFIX = ".xml";

    private transient String tmpScenarioPath;
    private transient FilePath tmpScenario;

    protected abstract FilePath createTmpScenrioFile(AbstractBuild<?, ?> build) throws InterruptedException, IOException;

    public String getTmpScenarioPath(AbstractBuild<?, ?> build) throws InterruptedException, IOException {
        if (tmpScenarioPath == null) {
            tmpScenario = createTmpScenrioFile(build);
            tmpScenarioPath = tmpScenario.getRemote();
        }
        return tmpScenarioPath;
    }

    /**
     * Replace parameters in scenario and stores scenario into tmp file in workspace
     */
    public FilePath tmpScenarioFromContent(String scenarioContent, AbstractBuild<?, ?> build)
            throws InterruptedException, IOException {
        //String scenario = Resolver.buildVar(build, scenarioContent);
        // TODO env. var expansion? Expand on node where it will be launched
        FilePath path = build.getWorkspace().createTextTempFile(DEFAULT_SCENARIO_NAME, DEFAULT_SCENARIO_SUFFIX,
                scenarioContent, true);
        return path;
    }

    public FilePath tmpScenarioFromFile(String scenarioPath, AbstractBuild<?, ?> build) throws InterruptedException,
            IOException {
        String path = Resolver.buildVar(build, scenarioPath);
        FilePath fp = new FilePath(build.getWorkspace(), path);
        String scenarioContent = fp.readToString(); // TODO not very safe, if e.g. some malicious user provide path to
                                                    // huge file
        // TODO env. var expansion? Expand on node where it will be launched
        return tmpScenarioFromContent(scenarioContent, build);
    }
    
    public void cleanup() throws InterruptedException, IOException {
        if(tmpScenario != null)
            tmpScenario.delete();   
    }

    @Override
    @SuppressWarnings("unchecked")
    public Descriptor<ScenarioSource> getDescriptor() {
        return Jenkins.getInstance().getDescriptorOrDie(getClass());
    }

    public static final DescriptorExtensionList<ScenarioSource, Descriptor<ScenarioSource>> all() {
        return Jenkins.getInstance().getDescriptorList(ScenarioSource.class);
    }

    public static abstract class ScenarioSourceDescriptor extends Descriptor<ScenarioSource> {
    }

}

@@ -790,32 +790,44 @@
 ruptedException,
+%0A           
  IOException;%0A%0A 
@@ -824,16 +824,16 @@
 eption;%0A
-
 %0A    pub
@@ -936,51 +936,8 @@
 n %7B%0A
-        if (tmpScenarioPath == null) %7B%0A    
     
@@ -983,36 +983,32 @@
 build);%0A        
-    
 tmpScenarioPath 
@@ -1038,18 +1038,8 @@
 ();%0A
-        %7D%0A
     
@@ -1333,16 +1333,17 @@
       //
+ 
 String s
@@ -2255,13 +2255,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2331,16 +2331,17 @@
       if
+ 
 (tmpScen
@@ -2353,16 +2353,18 @@
 != null)
+ %7B
 %0A       
@@ -2389,19 +2389,58 @@
 elete();
-   
+%0A            tmpScenario = null;%0A        %7D
 %0A    %7D%0A%0A

Create scenario every time