|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ProcessCreator.java | 0% | 0% | 0% | 0% |
|
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.List; | |
44 | import java.util.Map; | |
45 | ||
46 | /** | |
47 | * This class abstracts out process creation, similar to ProcessCreator, | |
48 | * which is only available in Java 1.5. | |
49 | * This ProcessCreator cannot deal with process sequences and chains. | |
50 | * It can only create one processes. | |
51 | */ | |
52 | ||
53 | public class ProcessCreator { | |
54 | protected String _cmdline = null; | |
55 | protected String _evaluatedCmdLine = null; | |
56 | protected String _workdir; | |
57 | protected String _evaluatedWorkDir = null; | |
58 | protected String[] _cmdarray; // command line, already split | |
59 | protected Map<String,String> _env; | |
60 | protected PropertyMaps _props = PropertyMaps.TEMPLATE; | |
61 | ||
62 | /** Degenerate constructor, only for subclasses that completely override this class. */ | |
63 | 0 | protected ProcessCreator() { } |
64 | ||
65 | /** Constructor for a process creator with the given command line and map of properties. | |
66 | * @param cmdline command line | |
67 | * @param workdir working directory | |
68 | * @param pm PropertyMaps used for substitution when replacing variables | |
69 | */ | |
70 | 0 | public ProcessCreator(String cmdline, String workdir, PropertyMaps pm) { |
71 | 0 | _cmdline = cmdline; |
72 | 0 | _workdir = workdir; |
73 | 0 | _props = pm; |
74 | } | |
75 | ||
76 | /** Constructor for a process creator with the given command line already split up, | |
77 | * and map of properties. | |
78 | * @param cmdarray array of command line arguments | |
79 | * @param workdir working directory | |
80 | */ | |
81 | 0 | public ProcessCreator(String[] cmdarray, String workdir) { |
82 | 0 | _cmdarray = cmdarray; |
83 | 0 | _workdir = workdir; |
84 | } | |
85 | ||
86 | /** Cached copy of the reconstructed command line. */ | |
87 | protected String _cachedCmdLine = null; | |
88 | ||
89 | /** Get the command line. | |
90 | * @return command line | |
91 | */ | |
92 | 0 | public String cmdline() { |
93 | 0 | if (_cmdline == null) { |
94 | 0 | if (_cachedCmdLine == null) { |
95 | 0 | StringBuilder sb = new StringBuilder(); |
96 | 0 | for (int i = 0; i < _cmdarray.length; ++i) { |
97 | 0 | sb.append(" "); |
98 | 0 | sb.append(StringOps.unescapeFileName(_cmdarray[i])); |
99 | } | |
100 | 0 | _cachedCmdLine = sb.toString(); |
101 | 0 | if (_cachedCmdLine.length() > 0) { |
102 | 0 | _cachedCmdLine = _cachedCmdLine.substring(1); |
103 | } | |
104 | } | |
105 | 0 | return _cachedCmdLine; |
106 | } | |
107 | else { | |
108 | 0 | return _cmdline; |
109 | } | |
110 | } | |
111 | ||
112 | /** Returns a map of this process creator's environment. | |
113 | * @return environment map | |
114 | */ | |
115 | 0 | public Map<String,String> environment() { |
116 | 0 | return _env; |
117 | } | |
118 | ||
119 | /** Returns this process creator's working directory. | |
120 | * @return working directory | |
121 | */ | |
122 | 0 | public String workDir() { |
123 | 0 | return _workdir; |
124 | } | |
125 | ||
126 | /** Return the command line after evaluation, or null if it hasn't been replaced yet. */ | |
127 | 0 | public String evaluatedCommandLine() { |
128 | 0 | return _evaluatedCmdLine; |
129 | } | |
130 | ||
131 | /** Return the work directory after evaluation, or null if it hasn't been replaced yet. */ | |
132 | 0 | public String evaluatedWorkDir() { |
133 | 0 | return _evaluatedWorkDir; |
134 | } | |
135 | ||
136 | /** Return the PropertyMaps object used for substitution. */ | |
137 | 0 | public PropertyMaps getPropertyMaps() { return _props; } |
138 | ||
139 | /** Starts a new process using the attributes of this process creator. | |
140 | */ | |
141 | 0 | public Process start() throws IOException { |
142 | // set up work directory | |
143 | 0 | _evaluatedWorkDir = StringOps.replaceVariables(_workdir, _props, PropertyMaps.GET_CURRENT); |
144 | 0 | _evaluatedWorkDir = StringOps.unescapeFileName(_evaluatedWorkDir); |
145 | 0 | File dir = null; |
146 | 0 | if (!_evaluatedWorkDir.trim().equals("")) { dir = new File(_evaluatedWorkDir); } |
147 | ||
148 | // set up environment | |
149 | 0 | String[] env = null; |
150 | 0 | if ((_env != null) && (_env.size() > 0)) { |
151 | 0 | env = new String[_env.size()]; |
152 | 0 | int i = 0; |
153 | 0 | for(String key: _env.keySet()) { |
154 | 0 | String value = _env.get(key); |
155 | 0 | env[i] = key + "=" + value; |
156 | } | |
157 | } | |
158 | ||
159 | // set up command line, if necessary | |
160 | 0 | if (_cmdline != null) { |
161 | 0 | _evaluatedCmdLine = StringOps.replaceVariables(_cmdline, _props, PropertyMaps.GET_CURRENT); |
162 | 0 | List<List<List<String>>> seqs = StringOps.commandLineToLists(_evaluatedCmdLine); |
163 | 0 | if (seqs.size() != 1) { throw new IllegalArgumentException("ProcessCreator needs a command line with just one process."); } |
164 | 0 | List<List<String>> pipe = seqs.get(0); |
165 | 0 | if (pipe.size()<1) { throw new IllegalArgumentException("ProcessCreator needs a command line with just one process."); } |
166 | 0 | List<String> cmds = pipe.get(0); |
167 | 0 | _cmdarray = new String[cmds.size()]; |
168 | 0 | for (int i = 0; i < cmds.size(); ++i) { |
169 | 0 | _cmdarray[i] = StringOps.unescapeFileName(cmds.get(i)); |
170 | } | |
171 | } | |
172 | ||
173 | 0 | return Runtime.getRuntime().exec(_cmdarray,env,dir); |
174 | } | |
175 | } |
|