1 19 20 package org.openide.execution; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.Serializable ; 25 import java.text.Format ; 26 import java.util.Arrays ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.logging.Logger ; 31 import org.openide.util.Utilities; 32 33 44 public final class NbProcessDescriptor extends Object implements Serializable { 45 46 private static final long serialVersionUID = -4535211234565221486L; 47 48 49 private static Logger execLog; 50 51 52 private String processName; 53 54 private String arguments; 55 56 private String info; 57 58 62 public NbProcessDescriptor(String processName, String arguments) { 63 this (processName, arguments, null); 64 } 65 66 71 public NbProcessDescriptor(String processName, String arguments, String info) { 72 this.processName = processName; 73 this.arguments = arguments; 74 this.info = info; 75 } 76 77 78 81 public String getProcessName () { 82 return processName; 83 } 84 85 88 public String getArguments () { 89 return arguments; 90 } 91 92 95 public String getInfo () { 96 return info; 97 } 98 99 117 118 128 public Process exec (Format format, String [] envp, File cwd) throws IOException { 129 return exec (format, envp, false, cwd); 130 } 131 132 148 public Process exec (Format format, String [] envp, boolean appendEnv, File cwd) throws IOException { 149 String stringArgs = format == null ? arguments : format.format (arguments); 150 String [] args = parseArguments (stringArgs); 151 String [] call = null; 152 153 envp = substituteEnv(format, envp); 154 155 call = new String [args.length + 1]; 157 call[0] = format == null ? processName : format.format(processName); 158 System.arraycopy (args, 0, call, 1, args.length); 159 160 logArgs(call); 161 162 ProcessBuilder pb = new ProcessBuilder (call); 163 164 if (envp != null) { 165 Map <String ,String > e = pb.environment(); 166 if (!appendEnv) e.clear(); 167 for (int i = 0; i < envp.length; i++) { 168 String nameval = envp[i]; 169 int idx = nameval.indexOf ('='); if (idx == -1) throw new IOException ("No equal sign in name=value: " + nameval); e.put (nameval.substring (0, idx), nameval.substring (idx + 1)); 173 } 174 } 175 176 if (cwd != null) pb.directory(cwd); 177 return pb.start(); 178 } 179 180 private static void logArgs(String [] args) { 181 getExecLog().fine("Running: " + Arrays.asList(args)); } 183 184 192 public Process exec (Format format, String [] envp) throws IOException { 193 return exec (format, envp, null); 194 } 195 196 203 public Process exec (Format format) throws IOException { 204 return exec (format, null); 205 } 206 207 212 public Process exec () throws IOException { 213 return exec (null, null); 214 } 215 216 217 public int hashCode() { 218 return processName.hashCode() + arguments.hashCode (); 219 } 220 221 222 public boolean equals(Object o) { 223 if (! (o instanceof NbProcessDescriptor)) return false; 224 NbProcessDescriptor him = (NbProcessDescriptor) o; 225 return processName.equals(him.processName) && arguments.equals(him.arguments); 226 } 227 228 232 private static String [] parseArguments(String sargs) { 233 return Utilities.parseParameters(sargs); 234 } 235 236 237 private static Logger getExecLog() { 238 if (execLog == null) { 239 execLog = Logger.getLogger(NbProcessDescriptor.class.getName()); 240 } 241 return execLog; 242 } 243 244 250 private static String [] substituteEnv(Format format, String [] envp) { 251 if (envp == null || envp.length == 0 || format == null) { 252 return envp; 253 } 254 255 String [] ret = new String [envp.length]; 256 StringBuffer adder = new StringBuffer (); 257 for (int i = 0; i < envp.length; i++) { 258 ret[i] = envp[i]; 259 if (ret[i] == null) { 260 continue; 261 } 262 263 int idx = ret[i].indexOf('='); 264 if (idx < 0) { 265 continue; 266 } 267 268 String val = ret[i].substring(idx + 1); 269 String key = ret[i].substring(0, idx); 270 adder.append(key).append('=').append(format.format(val)); 271 ret[i] = adder.toString(); 272 adder.setLength(0); 273 } 274 275 return ret; 276 } 277 } 278 | Popular Tags |