package com.orctom.mojo.was.service.impl;

import com.orctom.mojo.was.model.WebSphereModel;
import com.orctom.mojo.was.model.WebSphereServiceException;
import com.orctom.mojo.was.service.IWebSphereService;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
import org.codehaus.plexus.util.cli.StreamConsumer;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

/**
 * Using jython
 * Created by CH on 3/11/14.
 */
public class WebSphereServiceScriptImpl implements IWebSphereService {

    private WebSphereModel model;
    private String workingDir;

    private static final String TEMPLATE = "jython/websphere.py";
    private static final String TEMPLATE_EXT = ".py";
    private static final String TEMP_DIR = "/was-maven-plugin/py/";

    public WebSphereServiceScriptImpl(WebSphereModel model, String workingDir) {
        this.model = model;
        this.workingDir = workingDir + TEMP_DIR;
    }

    @Override
    public void restartServer() {
        execute("restartServer");
    }

    public void startServer() {
        execute("startServer");
    }

    public void stopServer() {
        execute("stopServer");
    }

    public Collection<String> listApplications() {
        String value = execute("listApplications");
        if (StringUtils.isNotBlank(value)) {
            return Arrays.asList(StringUtils.split(value, " "));
        }
        return null;
    }

    @Override
    public void installApplication() {
        StringBuilder options = new StringBuilder(100);
        //-appname {{applicationName}} -preCompileJSPs true -cluster {{cluster}} -server {{server}}
        options.append("-preCompileJSPs true");
        if (StringUtils.isNotEmpty(model.getApplicationName())) {
            options.append(" -appname ").append(model.getApplicationName());
        }

        if (StringUtils.isNotEmpty(model.getCluster())) {
            options.append(" -cluster ").append(model.getCluster());
        }

        if (StringUtils.isNotEmpty(model.getServer())) {
            options.append(" -server ").append(model.getServer());
        }
        model.setOptions(options.toString());
        execute("installApplication");
    }

    @Override
    public void uninstallApplication() {
        execute("uninstallApplication");
    }

    @Override
    public void startApplication() {
        execute("startApplication");
    }

    @Override
    public void stopApplication() {
        execute("stopApplication");
    }

    @Override
    public void deploy() {
        execute("deploy");
    }

    private String execute(String task) {
        try {
            Commandline commandline = getCommandline(task);

            final StringBuilder rtValue = new StringBuilder(100);
            StreamConsumer outConsumer = new StreamConsumer() {
                boolean isReturnValueLine = false;
                public void consumeLine(String line) {
                    System.out.println(line);
                    if (isReturnValueLine && StringUtils.isBlank(line)) {
                        isReturnValueLine = false;
                    }
                    if (isReturnValueLine) {
                        rtValue.append(line.substring("  [wsadmin] ".length())).append(" ");
                    }
                    if (line.startsWith("  [wsadmin] WAS")) {
                        isReturnValueLine = true;
                    }
                }
            };

            CommandLineUtils.StringStreamConsumer errorConsumer = new CommandLineUtils.StringStreamConsumer();

            CommandUtils.executeCommand(commandline, outConsumer, errorConsumer, model.isVerbose());

            String error = errorConsumer.getOutput();
            if (StringUtils.isNotEmpty(error)) {
                System.out.println(error);
            }

            return rtValue.toString();
        } catch (Exception e) {
            throw new WebSphereServiceException("Failed to execute: " + task, e);
        }
    }

    private Commandline getCommandline(String task) throws IOException {
        File buildScript = CommandUtils.getBuildScript(task, TEMPLATE, model, workingDir, TEMPLATE_EXT);
        Commandline commandLine = new Commandline();
        commandLine.setExecutable(CommandUtils.getExecutable(model.getWasHome(), "wsadmin").getAbsolutePath());
        commandLine.setWorkingDirectory(workingDir);

        commandLine.createArg().setLine("-conntype " + model.getConnectorType());
        commandLine.createArg().setLine("-host " + model.getHost());
        commandLine.createArg().setLine("-port " + model.getPort());
        if (StringUtils.isNotBlank(model.getUser())) {
            commandLine.createArg().setLine("-user " + model.getUser());
            if (StringUtils.isNotBlank(model.getPassword())) {
                commandLine.createArg().setLine("-password " + model.getPassword());
            }
        }
        commandLine.createArg().setLine("-lang jython");
        commandLine.createArg().setLine("-f " + buildScript.getAbsolutePath());
        commandLine.createArg().setLine("-o "  + task);

        return commandLine;
    }
}

@@ -351,158 +351,52 @@
 ne;%0A
-import org.codehaus.plexus.util.cli.StreamConsumer;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.util.Arrays;%0Aimport java.util.Collec
+%0Aimport java.io.File;%0Aimport java.io.IOExcep
 tion
@@ -1176,259 +1176,8 @@
  %7D%0A%0A
-    public Collection%3CString%3E listApplications() %7B%0A        String value = execute(%22listApplications%22);%0A        if (StringUtils.isNotBlank(value)) %7B%0A            return Arrays.asList(StringUtils.split(value, %22 %22));%0A        %7D%0A        return null;%0A    %7D%0A%0A
     
@@ -2325,22 +2325,20 @@
 private 
-String
+void
  execute
@@ -2444,752 +2444,104 @@
     
-final StringBuilder rtValue = new StringBuilder(100);%0A            StreamConsumer outConsumer = new StreamConsumer() %7B%0A                boolean isReturnValueLine = false;%0A                public void consumeLine(String line) %7B%0A                    System.out.println(line);%0A                    if (isReturnValueLine && StringUtils.isBlank(line)) %7B%0A                        isReturnValueLine = false;%0A                    %7D%0A                    if (isReturnValueLine) %7B%0A                        rtValue.append(line.substring(%22  %5Bwsadmin%5D %22.length())).append(%22 %22);%0A                    %7D%0A                    if (line.startsWith(%22  %5Bwsadmin%5D WAS%22)) %7B%0A                        isReturnValueLine = true;%0A                    %7D%0A                %7D%0A            %7D;%0A
+CommandLineUtils.StringStreamConsumer outConsumer = new CommandLineUtils.StringStreamConsumer();
 %0A   
@@ -2747,24 +2747,180 @@
 erbose());%0A%0A
+            String info = outConsumer.getOutput();%0A            if (StringUtils.isNotEmpty(info)) %7B%0A                System.out.println(info);%0A            %7D%0A%0A
             
@@ -3025,35 +3025,35 @@
          System.
-out
+err
 .println(error);
@@ -3070,48 +3070,8 @@
    %7D
-%0A%0A            return rtValue.toString();
 %0A   
@@ -4297,17 +4297,16 @@
 ne(%22-o %22
- 
  + task)

clean code