001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001-2003, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.util;
037:
038: import java.io.File;
039: import java.io.IOException;
040:
041: import org.apache.log4j.Logger;
042:
043: /**
044: * Extends the <code>Commandline</code> class to provide a means
045: * to manipulate the OS environment under which the command will
046: * run.
047: *
048: * @author <a href="mailto:rjmpsmith@hotmail.com">Robert J. Smith</a>
049: */
050: public class EnvCommandline extends Commandline {
051:
052: private static final Logger LOG = Logger
053: .getLogger(EnvCommandline.class);
054:
055: /**
056: * Provides the OS environment under which the command
057: * will run.
058: */
059: private OSEnvironment env = new OSEnvironment();
060:
061: /**
062: * Constructor which takes a command line string and attempts
063: * to parse it into it's various components.
064: *
065: * @param command The command
066: */
067: public EnvCommandline(String command) {
068: super (command);
069: }
070:
071: /**
072: * Default constructor
073: */
074: public EnvCommandline() {
075: super ();
076: }
077:
078: /**
079: * Sets a variable within the environment under which the command will be
080: * run.
081: *
082: * @param var
083: * The environment variable to set
084: * @param value
085: * The value of the variable
086: */
087: public void setVariable(String var, String value) {
088: env.add(var, value);
089: }
090:
091: /**
092: * Gets the value of an environment variable. The variable
093: * name is case sensitive.
094: *
095: * @param var The variable for which you wish the value
096: *
097: * @return The value of the variable, or <code>null</code>
098: * if not found
099: */
100: public String getVariable(String var) {
101: return env.getVariable(var);
102: }
103:
104: /**
105: * Executes the command.
106: */
107: public Process execute() throws IOException {
108: Process process;
109:
110: // Let the user know what's happening
111: File workingDir = getWorkingDir();
112: if (workingDir == null) {
113: LOG.debug("Executing \"" + this + "\"");
114: process = Runtime.getRuntime().exec(getCommandline(),
115: env.toArray());
116: } else {
117: LOG.debug("Executing \"" + this + "\" in directory "
118: + workingDir.getAbsolutePath());
119: process = Runtime.getRuntime().exec(getCommandline(),
120: env.toArray(), workingDir);
121: }
122:
123: process.getOutputStream().close();
124:
125: return process;
126: }
127: }
|