Package juju.reattore.perfcap.var.impl

Source Code of juju.reattore.perfcap.var.impl.CommandLineVar

/*  Reattore HTTP Server

    Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    $Id: CommandLineVar.java,v 1.3 2003/03/05 04:31:57 michaelh Exp $
*/

package juju.reattore.perfcap.var.impl;

import java.io.*;
import java.util.regex.*;
import java.util.*;

import org.apache.commons.logging.*;

import juju.reattore.perfcap.var.Variable;
import juju.reattore.util.Spawner;

/** A 'Variable' that actually starts a background process.

    @tag command
    @group Variable
*/
public class CommandLineVar
    implements Variable {

    private static Log log = LogFactory.getLog(CommandLineVar.class);

    private String dir;
    private String startupCmd;
    private String shutdownCmd;
    private String name = "Command";

    private Spawner proc;

    private int startDelay = 1000;
    private Pattern waitFor;
   
    private boolean first = true;

    /** The working directory to run from.

        @param dir Working dir.
        @default The current directory.
     */
    public void setDir(String dir) {
        this.dir = dir;
    }

    /** The Runtime.exec command to run to start the process.

        @param cmd The command.
     */
    public void setStartupCmd(String cmd) {
        this.startupCmd = cmd;
    }

    /** The Runtime.exec command to run to stop the process.  If not
        set, defaults to killing the process.

        @param cmd The command.
     */
    public void setShutdownCmd(String cmd) {
        this.shutdownCmd = cmd;
    }

    /** The regex string that signifies that the process has started.

        @param waitFor The Matcher.match regex.
     */
    public void setWaitFor(String waitFor) {
        if (waitFor != null) {
            this.waitFor = Pattern.compile(waitFor);
        }
        else {
            this.waitFor = null;
        }
    }
                                 
    /** The time in ms to wait for the process to start.

        @param delay Time in ms.
        @default 1000
     */
    public void setStartDelay(int delay) {
        this.startDelay = delay;
    }

    /** The name to identify this process as.  Also returned as the
        value.

        @param name The name.
        @default Command
     */
    public void setName(String name) {
        this.name = name;
    }

    /** @see Variable */
    public String getName() {
        return name != null ? name : startupCmd;
    }

    /** @see Variable */
    public Object getValue() {
        return getName();
    }

    /** @see Variable */
    public boolean hasNext() {
        return first;
    }

    /** @see Variable */
    public void next() {
        first = false;
    }

    /** @see Variable */
    public void begin()
        throws Exception {

        log.info("Running '" + startupCmd + "' in dir: " + dir);

        proc = new Spawner();
        proc.setCommand(startupCmd);
        proc.setDir(dir);

        proc.spawn();

        first = true;

        if (waitFor != null) {
            List out = proc.getOutput();

            found:
            do {
                synchronized (out) {
                    Iterator it = out.iterator();
                    while (it.hasNext()) {
                        String line = (String)it.next();
            
                        if (waitFor.matcher(line).matches()) {
                            break found;
                        }
                    }

                    out.wait();
                }
            }
            while (true);
        }
        else {
            Thread.sleep(startDelay);
        }
    }

    /** @see Variable */
    public void end()
        throws Exception {

        if (proc == null) {
            /* Do nothing */
        }
        else {
            if (shutdownCmd == null) {
                log.info("Terminating process");
                proc.destroy();

                log.info("Exit value: " + proc.exitValue());
            }
            else {
                log.info("Waiting for main process");
                proc.waitDone();

                log.info("Exit value: " + proc.exitValue());

                log.info("Shutting down using " + shutdownCmd);

                Spawner shut = new Spawner();
                shut.setCommand(shutdownCmd);
               
                shut.spawn();
                shut.waitDone();

                Thread.sleep(2000);
            }
        }
    }
}
TOP

Related Classes of juju.reattore.perfcap.var.impl.CommandLineVar

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.