/** * Executes the command through a system 'exec'. This method will be used only if the supporting * Java Native Interface library could not be loaded. */ private synchronized void pure_exec(String[] cmd) throws IOException { if (null != this.environ.getExecutable()) { cmd[0] = this.environ.getExecutable(); } p = rt.exec(cmd, this.environ.getEnvp()); InputStream is = p.getInputStream(); Debug.verbose("P4Process.exec().is: " + is); InputStreamReader isr = new InputStreamReader(is); Debug.verbose("P4Process.exec().isr: " + isr); in = new BufferedReader(isr); InputStream es = p.getErrorStream(); Debug.verbose("P4Process.exec().es: " + es); InputStreamReader esr = new InputStreamReader(es); Debug.verbose("P4Process.exec().esr: " + esr); err = new BufferedReader(esr); OutputStream os = p.getOutputStream(); Debug.verbose("P4Process.exec().os: " + os); OutputStreamWriter osw = new OutputStreamWriter(os); Debug.verbose("P4Process.exec().osw: " + osw); out = new FilterWriter(new BufferedWriter(osw)) { public void write(String str) throws IOException { super.write(str); System.out.print("P4DebugOutput: " + str); } }; }
/** * Executes a p4 command. This uses the class environment information to execute the p4 command * specified in the String array. This array contains all the command line arguments that will be * specified for execution, including "p4" in the first position. * * @param cmd Array of command line arguments ("p4" must be first). */ public synchronized void exec(String[] cmd) throws IOException { String[] pre_cmds = new String[12]; int i = 0; pre_cmds[i++] = cmd[0]; pre_cmds[i++] = "-s"; // Forces all commands to use stdout for message // reporting, no longer read stderr if (!getEnv().getPort().trim().equals("")) { pre_cmds[i++] = "-p"; pre_cmds[i++] = getEnv().getPort(); } if (!getEnv().getUser().trim().equals("")) { pre_cmds[i++] = "-u"; pre_cmds[i++] = getEnv().getUser(); } if (!getEnv().getClient().trim().equals("")) { pre_cmds[i++] = "-c"; pre_cmds[i++] = getEnv().getClient(); } if (!getEnv().getPassword().trim().equals("")) { pre_cmds[i++] = "-P"; pre_cmds[i++] = getEnv().getPassword(); } if (cmd[1].equals("-x")) { pre_cmds[i++] = "-x"; pre_cmds[i++] = cmd[2]; } new_cmd = new String[(i + cmd.length) - 1]; for (int j = 0; j < (i + cmd.length) - 1; j++) { if (j < i) { new_cmd[j] = pre_cmds[j]; } else { new_cmd[j] = cmd[(j - i) + 1]; } } Debug.verbose("P4Process.exec: ", new_cmd); if (P4JNI.isValid()) { native_exec(new_cmd); using_native = true; } else { pure_exec(new_cmd); using_native = false; } }
/** * Reads the next line from the process. This method will be used only if the supporting Java * Native Interface library could not be loaded. */ private synchronized String pure_readLine() { String line; long current, timeout = ((new Date()).getTime()) + threshold; if (null == p || null == in || null == err) return null; // Debug.verbose("P4Process.readLine()"); try { for (; ; ) { if (null == p || null == in || null == err) { Debug.error("P4Process.readLine(): Something went null"); return null; } current = (new Date()).getTime(); if (current >= timeout) { Debug.error("P4Process.readLine(): Timeout"); // If this was generating a new object from stdin, return an // empty string. Otherwise, return null. for (int i = 0; i < new_cmd.length; i++) { if (new_cmd[i].equals("-i")) return ""; } return null; } // Debug.verbose("P4Process.readLine().in: "+in); try { /** * If there's something coming in from stdin, return it. We assume that the p4 command was * called with -s which sends all messages to standard out pre-pended with a string that * indicates what kind of messsage it is error warning text info exit */ // Some errors still come in on Standard error while (err.ready()) { line = err.readLine(); if (null != line) { addP4Error(line + "\n"); } } if (in.ready()) { line = in.readLine(); Debug.verbose("From P4:" + line); if (line.startsWith("error")) { if (!line.trim().equals("") && (-1 == line.indexOf("up-to-date")) && (-1 == line.indexOf("no file(s) to resolve"))) { addP4Error(line); } } else if (line.startsWith("warning")) { } else if (line.startsWith("text")) { } else if (line.startsWith("info")) { } else if (line.startsWith("exit")) { int exit_code = new Integer(line.substring(line.indexOf(" ") + 1, line.length())).intValue(); if (0 == exit_code) { Debug.verbose("P4 Exec Complete."); } else { Debug.error("P4 exited with an Error!"); } return null; } if (!raw) line = line.substring(line.indexOf(":") + 1).trim(); Debug.verbose("P4Process.readLine(): " + line); return line; } } catch (NullPointerException ne) { } // If there's nothing on stdin or stderr, check to see if the // process has exited. If it has, return null. try { exit_code = p.exitValue(); return null; } catch (IllegalThreadStateException ie) { Debug.verbose("P4Process: Thread is not done yet."); } // Sleep for a second, so this thread can't become a CPU hog. try { Debug.verbose("P4Process: Sleeping..."); Thread.sleep(100); // Sleep for 1/10th of a second. } catch (InterruptedException ie) { } } } catch (IOException ex) { return null; } }