Clover coverage report - DrJava Test Coverage (drjava-20120422-r5456)
Coverage timestamp: Sun Apr 22 2012 03:13:25 CDT
file stats: LOC: 233   Methods: 9
NCLOC: 140   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
GeneralProcessCreator.java 0% 0% 0% 0%
coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.util;
 38   
 39    import edu.rice.cs.drjava.config.PropertyMaps;
 40   
 41    import java.io.File;
 42    import java.io.IOException;
 43    import java.util.ArrayList;
 44    import java.util.List;
 45    import java.util.Map;
 46   
 47    /**
 48    * This class abstracts out process creation, similar to ProcessCreator,
 49    * which is only available in Java 1.5. Additionally, it transparently
 50    * creates process sequences and process chains, including piping.
 51    */
 52   
 53    public class GeneralProcessCreator extends ProcessCreator {
 54    protected List<List<List<String>>> _seqs;
 55   
 56    /** Constructor for a process creator with the given command line and the work directory.
 57    * @param cmdline command line
 58    * @param workdir working directory
 59    * @param pm PropertyMaps object used for substitution
 60    */
 61  0 public GeneralProcessCreator(String cmdline, String workdir, PropertyMaps pm) {
 62  0 _cmdline = cmdline;
 63  0 _workdir = workdir;
 64  0 _props = pm;
 65    }
 66   
 67    /** Constructor for a process creator with the given command line already split up, and
 68    * the work directory.
 69    * @param seqs a sequence of commands to pipe
 70    * @param workdir working directory
 71    * @param pm PropertyMaps object used for substitution
 72    */
 73  0 public GeneralProcessCreator(List<List<List<String>>> seqs, String workdir, PropertyMaps pm) {
 74  0 _seqs = seqs;
 75  0 _workdir = workdir;
 76  0 _props = pm;
 77    }
 78   
 79    /** Reconstructs the command line for a simple process. */
 80  0 protected static String getProcessCmdLine(List<String> cmds) {
 81  0 StringBuilder sb = new StringBuilder();
 82  0 for (int i = 0; i < cmds.size(); ++i) {
 83  0 sb.append(" ");
 84  0 sb.append(StringOps.unescapeFileName(cmds.get(i)));
 85    }
 86  0 String s = sb.toString();
 87  0 if (s.length() > 0) {
 88  0 s = s.substring(1);
 89    }
 90  0 return s;
 91    }
 92   
 93    /** Reconstructs the command line for a process chain. */
 94  0 protected static String getProcessChainCmdLine(List<List<String>> pipe) {
 95  0 StringBuilder sb = new StringBuilder();
 96  0 final String sep = " " + ProcessChain.PIPE_SEPARATOR + " ";
 97  0 for (int i = 0; i < pipe.size(); ++i) {
 98  0 sb.append(sep);
 99  0 sb.append(getProcessCmdLine(pipe.get(i)));
 100    }
 101  0 String s = sb.toString();
 102  0 if (s.length() > 0) {
 103  0 s = s.substring(sep.length());
 104    }
 105  0 return s;
 106    }
 107   
 108    /** Reconstructs the command line for a process sequence. */
 109  0 protected static String getProcessSequenceCmdLine(List<List<List<String>>> seqs) {
 110  0 StringBuilder sb = new StringBuilder();
 111  0 final String sep = " " + ProcessChain.PROCESS_SEPARATOR + " ";
 112  0 for (int i = 0; i < seqs.size(); ++i) {
 113  0 sb.append(sep);
 114  0 sb.append(getProcessChainCmdLine(seqs.get(i)));
 115    }
 116  0 String s = sb.toString();
 117  0 if (s.length() > 0) {
 118  0 s = s.substring(sep.length());
 119    }
 120  0 return s;
 121    }
 122   
 123    /** Get the command line.
 124    * @return command line
 125    */
 126  0 public String cmdline() {
 127  0 if (_cmdline == null) {
 128  0 if (_cachedCmdLine == null) {
 129  0 if (_seqs.size() == 1) {
 130    // only one piping chain, creating a process sequence is not necessary
 131  0 List<List<String>> pipe = _seqs.get(0);
 132  0 if (pipe.size() == 1) {
 133    // only one process, creating a piping chain is not necessary
 134  0 List<String> cmds = pipe.get(0);
 135  0 _cachedCmdLine = getProcessCmdLine(cmds);
 136    }
 137    else {
 138    // more than one process, create a process chain
 139  0 _cachedCmdLine = getProcessChainCmdLine(pipe);
 140    }
 141    }
 142    else {
 143    // more than one piping chain, create a process sequence
 144  0 _cachedCmdLine = getProcessSequenceCmdLine(_seqs);
 145    }
 146    }
 147  0 return _cachedCmdLine;
 148    }
 149    else {
 150  0 return _cmdline;
 151    }
 152    }
 153   
 154    /** Returns a map of this process creator's environment.
 155    * @return environment map
 156    */
 157  0 public Map<String,String> environment() {
 158  0 return _env;
 159    }
 160   
 161    /** Returns this process creator's working directory.
 162    * @return working directory
 163    */
 164  0 public String workDir() {
 165  0 return _workdir;
 166    }
 167   
 168    public static final edu.rice.cs.util.Log LOG = new edu.rice.cs.util.Log("process.txt",false);
 169   
 170    /** Starts a new process using the attributes of this process creator. */
 171  0 public Process start() throws IOException {
 172    // set up work directory
 173  0 _evaluatedWorkDir = StringOps.replaceVariables(_workdir, _props, PropertyMaps.GET_CURRENT);
 174  0 _evaluatedWorkDir = StringOps.unescapeFileName(_evaluatedWorkDir);
 175  0 File dir = null;
 176  0 if (!_evaluatedWorkDir.trim().equals("")) { dir = new File(_evaluatedWorkDir); }
 177   
 178    // set up environment
 179  0 String[] env = null;
 180  0 if ((_env != null) && (_env.size() > 0)) {
 181  0 env = new String[_env.size()];
 182  0 int i = 0;
 183  0 for(String key: _env.keySet()) {
 184  0 String value = _env.get(key);
 185  0 env[i] = key + "=" + value;
 186    }
 187    }
 188   
 189    // set up command line
 190  0 if (_cmdline != null) {
 191  0 _evaluatedCmdLine = StringOps.replaceVariables(_cmdline, _props, PropertyMaps.GET_CURRENT);
 192  0 _seqs = StringOps.commandLineToLists(_evaluatedCmdLine);
 193    }
 194  0 LOG.log("\t" + edu.rice.cs.plt.iter.IterUtil.toString(_seqs));
 195  0 if (_seqs.size()<1) { throw new IOException("No process to start."); }
 196  0 if (_seqs.size() == 1) {
 197    // only one piping chain, creating a process sequence is not necessary
 198  0 List<List<String>> pipe = _seqs.get(0);
 199  0 if (pipe.size()<1) { throw new IOException("No process to start."); }
 200  0 if (pipe.size() == 1) {
 201    // only one process, creating a piping chain is not necessary
 202  0 List<String> cmds = pipe.get(0);
 203  0 if (cmds.size()<1) { throw new IOException("No process to start."); }
 204  0 String[] cmdarray = new String[cmds.size()];
 205  0 for (int i = 0; i < cmds.size(); ++i) {
 206  0 cmdarray[i] = StringOps.unescapeFileName(cmds.get(i));
 207    }
 208    // creating a simple process
 209  0 return Runtime.getRuntime().exec(cmdarray,env,dir);
 210    }
 211    // more than one process, create a process chain
 212  0 ProcessCreator[] creators = new ProcessCreator[pipe.size()];
 213  0 for (int i = 0; i < pipe.size(); ++i) {
 214  0 List<String> cmds = pipe.get(i);
 215  0 if (cmds.size()<1) { throw new IOException("No process to start."); }
 216  0 String[] cmdarray = new String[cmds.size()];
 217  0 for (int j=0; j<cmds.size(); ++j) {
 218  0 cmdarray[j] = StringOps.unescapeFileName(cmds.get(j));
 219    }
 220  0 creators[i] = new ProcessCreator(cmdarray, _workdir);
 221    }
 222  0 return new ProcessChain(creators);
 223    }
 224    // more than one piping chain, create a process sequence
 225  0 ProcessCreator[] creators = new ProcessCreator[_seqs.size()];
 226  0 for (int i = 0; i < _seqs.size(); ++i) {
 227  0 List<List<List<String>>> l = new ArrayList<List<List<String>>>();
 228  0 l.add(_seqs.get(i));
 229  0 creators[i] = new GeneralProcessCreator(l, _workdir, _props);
 230    }
 231  0 return new ProcessSequence(creators);
 232    }
 233    }