List of usage examples for java.lang ProcessBuilder environment
Map environment
To view the source code for java.lang ProcessBuilder environment.
Click Source Link
From source file:com.panet.imeta.job.entries.shell.JobEntryShell.java
private void executeShell(Result result, List<RowMetaAndData> cmdRows, String[] args) { LogWriter log = LogWriter.getInstance(); FileObject fileObject = null; String realScript = null;//from ww w . ja v a2 s.c o m FileObject tempFile = null; try { // What's the exact command? String base[] = null; List<String> cmds = new ArrayList<String>(); if (log.isBasic()) log.logBasic(toString(), Messages.getString("JobShell.RunningOn", Const.getOS())); if (insertScript) { realScript = environmentSubstitute(script); } else { String realFilename = environmentSubstitute(getFilename()); fileObject = KettleVFS.getFileObject(realFilename); } if (Const.getOS().equals("Windows 95")) { base = new String[] { "command.com", "/C" }; } else if (Const.getOS().startsWith("Windows")) { base = new String[] { "cmd.exe", "/C" }; } else { if (!insertScript) { // Just set the command to the script we need to execute... // base = new String[] { KettleVFS.getFilename(fileObject) }; } else { // Create a unique new temporary filename in the working directory, put the script in there // Set the permissions to execute and then run it... // try { tempFile = KettleVFS.createTempFile("kettle", "shell", workDirectory); tempFile.createFile(); OutputStream outputStream = tempFile.getContent().getOutputStream(); outputStream.write(realScript.getBytes()); outputStream.close(); String tempFilename = KettleVFS.getFilename(tempFile); // Now we have to make this file executable... // On Unix-like systems this is done using the command "/bin/chmod +x filename" // ProcessBuilder procBuilder = new ProcessBuilder("chmod", "+x", tempFilename); Process proc = procBuilder.start(); // Eat/log stderr/stdout all messages in a different thread... StreamLogger errorLogger = new StreamLogger(proc.getErrorStream(), toString() + " (stderr)"); StreamLogger outputLogger = new StreamLogger(proc.getInputStream(), toString() + " (stdout)"); new Thread(errorLogger).start(); new Thread(outputLogger).start(); proc.waitFor(); // Now set this filename as the base command... // base = new String[] { tempFilename }; } catch (Exception e) { throw new Exception("Unable to create temporary file to execute script", e); } } } // Construct the arguments... if (argFromPrevious && cmdRows != null) { // Add the base command... for (int i = 0; i < base.length; i++) cmds.add(base[i]); if (Const.getOS().equals("Windows 95") || Const.getOS().startsWith("Windows")) { // for windows all arguments including the command itself // need to be // included in 1 argument to cmd/command. StringBuffer cmdline = new StringBuffer(300); cmdline.append('"'); if (insertScript) cmdline.append(realScript); else cmdline.append(optionallyQuoteField(KettleVFS.getFilename(fileObject), "\"")); // Add the arguments from previous results... for (int i = 0; i < cmdRows.size(); i++) // Normally just // one row, but // once in a // while to // remain // compatible we // have // multiple. { RowMetaAndData r = (RowMetaAndData) cmdRows.get(i); for (int j = 0; j < r.size(); j++) { cmdline.append(' '); cmdline.append(optionallyQuoteField(r.getString(j, null), "\"")); } } cmdline.append('"'); cmds.add(cmdline.toString()); } else { // Add the arguments from previous results... for (int i = 0; i < cmdRows.size(); i++) // Normally just // one row, but // once in a // while to // remain // compatible we // have // multiple. { RowMetaAndData r = (RowMetaAndData) cmdRows.get(i); for (int j = 0; j < r.size(); j++) { cmds.add(optionallyQuoteField(r.getString(j, null), "\"")); } } } } else if (args != null) { // Add the base command... for (int i = 0; i < base.length; i++) cmds.add(base[i]); if (Const.getOS().equals("Windows 95") || Const.getOS().startsWith("Windows")) { // for windows all arguments including the command itself // need to be // included in 1 argument to cmd/command. StringBuffer cmdline = new StringBuffer(300); cmdline.append('"'); if (insertScript) cmdline.append(realScript); else cmdline.append(optionallyQuoteField(KettleVFS.getFilename(fileObject), "\"")); for (int i = 0; i < args.length; i++) { cmdline.append(' '); cmdline.append(optionallyQuoteField(args[i], "\"")); } cmdline.append('"'); cmds.add(cmdline.toString()); } else { for (int i = 0; i < args.length; i++) { cmds.add(args[i]); } } } StringBuffer command = new StringBuffer(); Iterator<String> it = cmds.iterator(); boolean first = true; while (it.hasNext()) { if (!first) command.append(' '); else first = false; command.append((String) it.next()); } if (log.isBasic()) log.logBasic(toString(), Messages.getString("JobShell.ExecCommand", command.toString())); // Build the environment variable list... ProcessBuilder procBuilder = new ProcessBuilder(cmds); Map<String, String> env = procBuilder.environment(); String[] variables = listVariables(); for (int i = 0; i < variables.length; i++) { env.put(variables[i], getVariable(variables[i])); } if (getWorkDirectory() != null && !Const.isEmpty(Const.rtrim(getWorkDirectory()))) { String vfsFilename = environmentSubstitute(getWorkDirectory()); File file = new File(KettleVFS.getFilename(KettleVFS.getFileObject(vfsFilename))); procBuilder.directory(file); } Process proc = procBuilder.start(); // any error message? StreamLogger errorLogger = new StreamLogger(proc.getErrorStream(), toString() + " (stderr)"); // any output? StreamLogger outputLogger = new StreamLogger(proc.getInputStream(), toString() + " (stdout)"); // kick them off new Thread(errorLogger).start(); new Thread(outputLogger).start(); proc.waitFor(); if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobShell.CommandFinished", command.toString())); // What's the exit status? result.setExitStatus(proc.exitValue()); if (result.getExitStatus() != 0) { if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("JobShell.ExitStatus", environmentSubstitute(getFilename()), "" + result.getExitStatus())); result.setNrErrors(1); } // close the streams // otherwise you get "Too many open files, java.io.IOException" after a lot of iterations proc.getErrorStream().close(); proc.getOutputStream().close(); } catch (IOException ioe) { log.logError(toString(), Messages.getString("JobShell.ErrorRunningShell", environmentSubstitute(getFilename()), ioe.toString())); result.setNrErrors(1); } catch (InterruptedException ie) { log.logError(toString(), Messages.getString("JobShell.Shellinterupted", environmentSubstitute(getFilename()), ie.toString())); result.setNrErrors(1); } catch (Exception e) { log.logError(toString(), Messages.getString("JobShell.UnexpectedError", environmentSubstitute(getFilename()), e.toString())); result.setNrErrors(1); } finally { // If we created a temporary file, remove it... // if (tempFile != null) { try { tempFile.delete(); } catch (Exception e) { Messages.getString("JobShell.UnexpectedError", tempFile.toString(), e.toString()); } } } if (result.getNrErrors() > 0) { result.setResult(false); } else { result.setResult(true); } }