/**
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 org.apache.geronimo.testsupport.commands;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.geronimo.mavenplugins.geronimo.ServerProxy;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
import org.apache.tools.ant.taskdefs.ExecuteWatchdog;
import org.apache.tools.ant.taskdefs.PumpStreamHandler;
import org.apache.tools.ant.taskdefs.condition.Os;

public class CommandTestSupport {
        
    public static final String GSH = "gsh";
    public static final String DEPLOY = "deploy";
    
    protected static final long timeout = 30000;    
    protected static String geronimoHome;
    
    static {
        geronimoHome = getGeronimoHome();
    }
        
    private static String getGeronimoHome() {
        ServerProxy server = null;
        try {
            server = new ServerProxy("localhost", 1099, "system", "manager");           
        } catch (Exception e) {
            throw new RuntimeException("Unable to setup ServerProxy", e);
        }
        
        String home = server.getGeronimoHome();
        Throwable exception = server.getLastError();
        
        server.closeConnection();
        
        if (exception != null) {
            throw new RuntimeException("Failed to get Geronimo home", exception);
        } else {
            return home;
        }
    }
        
    public CommandTestSupport() {
    }
    
    public void execute(String command, String[] args, InputStream in, OutputStream out) throws Exception {
        execute(command, (args == null) ? null : Arrays.asList(args), in, out);
    }
        
    public void execute(String command, List<String> args, InputStream in, OutputStream out) throws Exception {
        List<String> cmdLine = new ArrayList<String>();
        if (isWindows()) {
            cmdLine.add("cmd.exe");
            cmdLine.add("/c");
        }
        cmdLine.add(resolveCommandForOS(command));
        // add command-specific arguments
        cmdLine.addAll(getCommandArguments(command));
        // add user arguments
        if (args != null) {
            cmdLine.addAll(args);
        }
        
        ExecuteWatchdog watchdog = new ExecuteWatchdog( timeout );
        ExecuteStreamHandler streamHandler = new PumpStreamHandler( out, out, in );
        Execute exec = new Execute( streamHandler, watchdog );
        exec.setCommandline( cmdLine.toArray(new String[] {}) );
        List<String> env = getCommandEnvironment(command);
        if (!env.isEmpty()) {
            exec.setEnvironment(env.toArray(new String[] {}) );
        }
        exec.execute();
    }
    
    protected List<String> getCommandArguments(String command) {
        if (GSH.equals(command)) {
            return Arrays.asList("-T", "false");
        } else {
            return Collections.emptyList();
        }
    }
        
    protected List<String> getCommandEnvironment(String command) {
        if (DEPLOY.equals(command)) {
            //this makes the output can be captured in Linux
            return Arrays.asList("JAVA_OPTS=-Djline.terminal=jline.UnsupportedTerminal");
        } else {
            return Collections.emptyList();
        }
    }

    protected String resolveCommandForOS(String command) {      
        if (isWindows()) {
            return geronimoHome + "/bin/" + command + ".bat";            
        } else {
            return geronimoHome + "/bin/" + command;
        }     
    }
        
    public boolean isWindows() {
        return Os.isFamily("windows");
    }
    
}

@@ -916,32 +916,53 @@
 o.OutputStream;%0A
+import java.io.File;%0A
 import java.util
@@ -4219,22 +4219,43 @@
 mmand) %7B
+%0A
     
-  
+%09String filename = %22%22;
 %0A       
@@ -4278,38 +4278,42 @@
 ) %7B%0A            
-return
+filename =
  geronimoHome + 
@@ -4377,26 +4377,325 @@
         
-    return
+%09try %7B%0A        %09%09File cmdfile1 = new File(geronimoHome + %22/bin/%22 + command);%0A        %09%09File cmdfile2 = new File(geronimoHome + %22/bin/%22 + command + %22.sh%22);%0A        %09%09if(cmdfile1.exists()) %7B %0A        %09%09%09filename = geronimoHome + %22/bin/%22 + command;%0A        %09%09%7D else if (cmdfile2.exists()) %7B%0A        %09%09%09filename =
  geronim
@@ -4711,32 +4711,40 @@
 /bin/%22 + command
+ + %22.sh%22
 ;%0A        %7D     
@@ -4741,14 +4741,117 @@
     
-%7D     
+%09%09%7D%0A        %09%7D catch(Exception e) %7B %0A        %09%09%0A        %09%7D            %0A        %7D    %0A        return filename;
 %0A   

Consider linux commands with .sh suffix