KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > EnvCommandline


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 /*
38  * The Apache Software License, Version 1.1
39  *
40  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
41  * reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  *
47  * 1. Redistributions of source code must retain the above copyright
48  * notice, this list of conditions and the following disclaimer.
49  *
50  * 2. Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in
52  * the documentation and/or other materials provided with the
53  * distribution.
54  *
55  * 3. The end-user documentation included with the redistribution, if
56  * any, must include the following acknowlegement:
57  * "This product includes software developed by the
58  * Apache Software Foundation (http://www.apache.org/)."
59  * Alternately, this acknowlegement may appear in the software itself,
60  * if and wherever such third-party acknowlegements normally appear.
61  *
62  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
63  * Foundation" must not be used to endorse or promote products derived
64  * from this software without prior written permission. For written
65  * permission, please contact apache@apache.org.
66  *
67  * 5. Products derived from this software may not be called "Apache"
68  * nor may "Apache" appear in their names without prior written
69  * permission of the Apache Group.
70  *
71  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
72  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
73  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
74  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
75  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
76  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
77  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
78  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
79  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
80  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
81  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82  * SUCH DAMAGE.
83  * ====================================================================
84  *
85  * This software consists of voluntary contributions made by many
86  * individuals on behalf of the Apache Software Foundation. For more
87  * information on the Apache Software Foundation, please see
88  * <http://www.apache.org/>.
89  */

90 package net.sourceforge.cruisecontrol.util;
91
92 import java.io.File JavaDoc;
93 import java.io.IOException JavaDoc;
94
95 import org.apache.log4j.Logger;
96
97 /**
98  * Extends the <code>Commandline</code> class to provide a means
99  * to manipulate the OS environment under which the command will
100  * run.
101  *
102  * @author <a HREF="mailto:rjmpsmith@hotmail.com">Robert J. Smith</a>
103  */

104 public class EnvCommandline extends Commandline {
105     
106     private static final Logger LOG = Logger.getLogger(EnvCommandline.class);
107     
108     /**
109      * Provides the OS environment under which the command
110      * will run.
111      */

112     private OSEnvironment env = new OSEnvironment();
113
114     /**
115      * Constructor which takes a command line string and attempts
116      * to parse it into it's various components.
117      *
118      * @param command The command
119      */

120     public EnvCommandline(String JavaDoc command) {
121         super(command);
122     }
123
124     /**
125      * Default constructor
126      */

127     public EnvCommandline() {
128         super();
129     }
130     
131     /**
132      * Sets a variable within the environment under which the command will be
133      * run.
134      *
135      * @param var
136      * The environment variable to set
137      * @param value
138      * The value of the variable
139      */

140     public void setVariable(String JavaDoc var, String JavaDoc value) {
141         env.add(var, value);
142     }
143         
144      /**
145       * Gets the value of an environment variable. The variable
146       * name is case sensitive.
147       *
148       * @param var The variable for which you wish the value
149       *
150       * @return The value of the variable, or <code>null</code>
151       * if not found
152       */

153      public String JavaDoc getVariable(String JavaDoc var) {
154          return env.getVariable(var);
155      }
156
157     /**
158      * Executes the command.
159      */

160     public Process JavaDoc execute() throws IOException JavaDoc {
161         Process JavaDoc process;
162
163         // Let the user know what's happening
164
File JavaDoc workingDir = getWorkingDir();
165         if (workingDir == null) {
166             LOG.debug("Executing \"" + this + "\"");
167             process = Runtime.getRuntime().exec(getCommandline(), env.toArray());
168         } else {
169             LOG.debug(
170                 "Executing \""
171                     + this
172                     + "\" in directory "
173                     + workingDir.getAbsolutePath());
174             process = Runtime.getRuntime().exec(getCommandline(), env.toArray(), workingDir);
175         }
176
177         return process;
178     }
179 }
180
Popular Tags