package org.scala_tools.maven.executions;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
/**
 * Abstract helper implementation for JavaMainCaller interface.
 * @author josh
 *
 */
public abstract class JavaMainCallerSupport implements JavaMainCaller {

    protected AbstractMojo requester;
    protected List<String> env  = new ArrayList<String>();
    protected String mainClassName;
    protected List<String> jvmArgs = new ArrayList<String>();
    protected List<String> args = new ArrayList<String>();


    protected JavaMainCallerSupport(AbstractMojo requester, String mainClassName, String classpath, String[] jvmArgs, String[] args) throws Exception {
        this.requester = requester;
        for (String key : System.getenv().keySet()) {
            env.add(key + "=" + System.getenv(key));
        }
        this.mainClassName = mainClassName;
        addJvmArgs("-classpath", classpath);
        if(jvmArgs != null) {
           addJvmArgs(jvmArgs);
        }
        if(args != null) {
           addArgs(args);
        }
    }

    public void addJvmArgs(String... args) {
        if(args != null) {
            for(String arg : args) {
                this.jvmArgs.add(arg);
            }
        }
    }

    public void addToClasspath(File entry) throws Exception {
        if ((entry == null) || !entry.exists()) {
            return;
        }
        boolean isClasspath = false;
        for (int i = 0; i < jvmArgs.size(); i++) {
            String item = jvmArgs.get(i);
            if (isClasspath) {
                item = item + File.pathSeparator + entry.getCanonicalPath();
                jvmArgs.set(i, item);
                isClasspath = false;
                break;
            } else {
                isClasspath = "-classpath".equals(item);
            }
        }
    }

    public void addOption(String key, String value) {
        if ((value == null) || (key == null)) {
            return;
        }
        addArgs(key, value);
    }

    public void addOption(String key, File value) {
        if ((value == null) || (key == null)) {
            return;
        }
        addArgs(key, value.getAbsolutePath());
    }

    public void addOption(String key, boolean value) {
        if ((!value) || (key == null)) {
            return;
        }
        addArgs(key);
    }
    public void addArgs(String... args) {
        if(args != null) {
            this.args.addAll(Arrays.asList(args));
        }
    }

    public void addEnvVar(String key, String value) {
        this.env.add(key + "=" + value);

    }
    public void run(boolean displayCmd) throws Exception {
        run(displayCmd, true);
    }
}

@@ -89,33 +89,8 @@
 st;%0A
-import java.util.Arrays;%0A
 impo
@@ -108,16 +108,16 @@
 l.List;%0A
+
 %0Aimport 
@@ -154,16 +154,61 @@
 ctMojo;%0A
+import org.codehaus.plexus.util.StringUtils;%0A
 /**%0A * A
@@ -1017,41 +1017,8 @@
 h);%0A
-        if(jvmArgs != null) %7B%0A   
     
@@ -1046,48 +1046,8 @@
 s);%0A
-        %7D%0A        if(args != null) %7B%0A   
     
@@ -1065,26 +1065,16 @@
 (args);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1173,32 +1173,87 @@
 g arg : args) %7B%0A
+                if (StringUtils.isNotEmpty(arg)) %7B%0A    
                 
@@ -1275,16 +1275,34 @@
 d(arg);%0A
+                %7D%0A
         
@@ -1981,39 +1981,61 @@
 if (
-(value == null) %7C%7C (key == null
+StringUtils.isEmpty(value) %7C%7C StringUtils.isEmpty(key
 )) %7B
@@ -2165,16 +2165,17 @@
     if (
+ 
 (value =
@@ -2181,36 +2181,47 @@
 == null) %7C%7C 
-(key == null
+StringUtils.isEmpty(key
 )) %7B%0A       
@@ -2380,20 +2380,31 @@
  %7C%7C 
-(key == null
+StringUtils.isEmpty(key
 )) %7B
@@ -2547,46 +2547,155 @@
     
-this.args.addAll(Arrays.asList(args));
+for(String arg : args) %7B%0A                if (StringUtils.isNotEmpty(arg)) %7B%0A                    this.args.add(arg);%0A                %7D%0A            %7D
 %0A   

fix NPE when reading jvmArgs or args for execution