Mercurial > hg > ThermostatQA
view src/org/thermostat/qa2/framework/services/ShellService.java @ 170:0d34a379de82
- Added new framework (package org.thermosta.qa2.framework)
- Removed hardcoded display in org.thermostat.qa.framework.GuiRobot class
author | Zdenek Zambersky <zzambers@redhat.com> |
---|---|
date | Tue, 10 Mar 2015 13:38:36 +0100 |
parents | |
children | 5d335809cd51 |
line wrap: on
line source
/* ThermostatQA - test framework for Thermostat Monitoring Tool Copyright 2015 Red Hat, Inc. This file is part of ThermostatQA ThermostatQA is distributed under the GNU General Public License, version 2 or any later version (with a special exception described below, commonly known as the "Classpath Exception"). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING. Linking ThermostatQA code with other modules is making a combined work based on ThermostatQA. Thus, the terms and conditions of the GPL cover the whole combination. As a special exception, the copyright holders of ThermostatQA give you permission to link this code with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on ThermostatQA code. If you modify ThermostatQA, you may extend this exception to your version of the software, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package org.thermostat.qa2.framework.services; import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.thermostat.qa2.framework.NativeProcess; import org.thermostat.qa2.framework.Shell; import org.thermostat.qa2.framework.utils.FileUtilities; /** * * @author Zdeněk Žamberský */ public abstract class ShellService extends AbstractProcessService { String commands; Map<String, String> environment = new HashMap(); String pidFile; public ShellService(String... commands) { this.commands = ""; for (String command : commands) { this.commands += command + " "; } } public void addEnvVariable(String name, String value) { environment.put(name, value); } @Override public NativeProcess startServiceProcess() throws Exception { pidFile = "." + File.separator + getServiceName().replace(" ", "_") + ".pid"; pidFile = FileUtilities.getUniqueFile(pidFile); Shell shell = new Shell(File.separator + "bin" + File.separator + "bash"); shell.setLabel(getServiceName()); shell.start(); shell.writeln("function onExit {"); shell.writeln(" if [ -z \"${EXIT_CODE}\" ] && [ -n \"$!\" ] ; then"); shell.writeln(" echo \"killing " + getServiceName() + " processes ...\""); shell.writeln(" pkill -P $!"); shell.writeln(" wait $!"); shell.writeln(" fi"); shell.writeln("}"); shell.writeln("trap onExit EXIT"); shell.writeln("Interupted=0"); shell.writeln("echo $$ > " + pidFile); for (Entry<String, String> entry : environment.entrySet()) { shell.writeln("export " + entry.getKey() + "=" + entry.getValue()); } shell.writeln(commands + " &"); shell.writeln("PROCESS_PID=$!"); // shell.writeln("echo \"Agent PID: ${PROCESS_PID}\""); shell.writeln("wait $!"); shell.writeln("EXIT_CODE=$?"); shell.writeln("exit ${EXIT_CODE}"); return shell; } @Override public void stopServiceImpl() throws Exception { try { List<String> pid = FileUtilities.getLineListFromFile(pidFile); NativeProcess process = new NativeProcess("kill", pid.get(0)); process.setLabel(getServiceName()); process.start(); process.waitFor(); } catch (FileNotFoundException e) { // service already stopped } thread.join(); } @Override public void runAfterInWrappingThread() throws Exception { NativeProcess process = new NativeProcess("rm", pidFile); process.setLabel(getServiceName()); process.start(); process.waitForRaw(); } }