001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.geronimo.testsupport.commands;
020
021 import java.io.InputStream;
022 import java.io.OutputStream;
023 import java.util.ArrayList;
024 import java.util.Arrays;
025 import java.util.Collections;
026 import java.util.List;
027
028 import org.apache.geronimo.mavenplugins.geronimo.ServerProxy;
029 import org.apache.tools.ant.taskdefs.Execute;
030 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
031 import org.apache.tools.ant.taskdefs.ExecuteWatchdog;
032 import org.apache.tools.ant.taskdefs.PumpStreamHandler;
033 import org.apache.tools.ant.taskdefs.condition.Os;
034
035 public class CommandTestSupport {
036
037 public static final String GSH = "gsh";
038 public static final String DEPLOY = "deploy";
039
040 protected static final long timeout = 30000;
041 protected static String geronimoHome;
042
043 static {
044 geronimoHome = getGeronimoHome();
045 }
046
047 private static String getGeronimoHome() {
048 ServerProxy server = null;
049 try {
050 server = new ServerProxy("localhost", 1099, "system", "manager");
051 } catch (Exception e) {
052 throw new RuntimeException("Unable to setup ServerProxy", e);
053 }
054
055 String home = server.getGeronimoHome();
056 Throwable exception = server.getLastError();
057
058 server.closeConnection();
059
060 if (exception != null) {
061 throw new RuntimeException("Failed to get Geronimo home", exception);
062 } else {
063 return home;
064 }
065 }
066
067 public CommandTestSupport() {
068 }
069
070 public void execute(String command, String[] args, InputStream in, OutputStream out) throws Exception {
071 execute(command, (args == null) ? null : Arrays.asList(args), in, out);
072 }
073
074 public void execute(String command, List<String> args, InputStream in, OutputStream out) throws Exception {
075 List<String> cmdLine = new ArrayList<String>();
076 if (isWindows()) {
077 cmdLine.add("cmd.exe");
078 cmdLine.add("/c");
079 }
080 cmdLine.add(resolveCommandForOS(command));
081 // add command-specific arguments
082 cmdLine.addAll(getCommandArguments(command));
083 // add user arguments
084 if (args != null) {
085 cmdLine.addAll(args);
086 }
087
088 ExecuteWatchdog watchdog = new ExecuteWatchdog( timeout );
089 ExecuteStreamHandler streamHandler = new PumpStreamHandler( out, out, in );
090 Execute exec = new Execute( streamHandler, watchdog );
091 exec.setCommandline( cmdLine.toArray(new String[] {}) );
092 List<String> env = getCommandEnvironment(command);
093 if (!env.isEmpty()) {
094 exec.setEnvironment(env.toArray(new String[] {}) );
095 }
096 exec.execute();
097 }
098
099 protected List<String> getCommandArguments(String command) {
100 if (GSH.equals(command)) {
101 return Arrays.asList("-T", "false");
102 } else {
103 return Collections.emptyList();
104 }
105 }
106
107 protected List<String> getCommandEnvironment(String command) {
108 if (DEPLOY.equals(command)) {
109 //this makes the output can be captured in Linux
110 return Arrays.asList("JAVA_OPTS=-Djline.terminal=jline.UnsupportedTerminal");
111 } else {
112 return Collections.emptyList();
113 }
114 }
115
116 protected String resolveCommandForOS(String command) {
117 if (isWindows()) {
118 return geronimoHome + "/bin/" + command + ".bat";
119 } else {
120 if (GSH.equals(command)) {
121 return geronimoHome + "/bin/" + command;
122 } else {
123 return geronimoHome + "/bin/" + command + ".sh";
124 }
125 }
126 }
127
128 public boolean isWindows() {
129 return Os.isFamily("windows");
130 }
131
132 }