001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.externalprocess;
011:
012: import java.io.File;
013: import java.io.IOException;
014:
015: /**
016: * Process Factory creates a external process.
017: *
018: * The factory could be used to create a process in another way than java.lang.
019: * Runtime.exec();
020: *
021: * @author Nico Klasens (Finalist IT Group)
022: * @version $Id: ProcessFactory.java,v 1.3 2003/05/12 13:10:47 kees Exp $
023: * @since MMBase-1.6
024: */
025: public class ProcessFactory {
026:
027: /**
028: * instance of the Process Factory
029: */
030: static private ProcessFactory instance = new ProcessFactory();
031:
032: /**
033: * Runtime of the current java process
034: */
035: private Runtime runtime;
036:
037: /**
038: * get the Process Factory.instance
039: * @return ProcessFactory
040: */
041: public static ProcessFactory getFactory() {
042: return instance;
043: }
044:
045: /**
046: * Create an new process factory.
047: */
048: private ProcessFactory() {
049: runtime = Runtime.getRuntime();
050: }
051:
052: /**
053: * Executes the specified command in a separate process.
054: *
055: * @param cmd the command to call
056: * @return Process a Process object for managing the external process
057: * @throws IOException if an I/O error occurs.
058: */
059: public Process exec(String cmd) throws IOException {
060: return runtime.exec(cmd);
061: }
062:
063: /**
064: * Executes the specified command and arguments in a separate process.
065: *
066: * @param cmdarray array containing the command to call and its arguments
067: * @return Process a Process object for managing the external process
068: * @throws IOException if an I/O error occurs.
069: */
070: public Process exec(String[] cmdarray) throws IOException {
071: return runtime.exec(cmdarray);
072: }
073:
074: /**
075: * Executes the specified command and arguments in a separate process with
076: * the specified environment.
077: *
078: * @param cmdarray array containing the command to call and its arguments
079: * @param envp array of strings, each element of which has environment
080: * variable settings in format name=value.
081: * @return Process a Process object for managing the external process
082: * @throws IOException if an I/O error occurs.
083: */
084: public Process exec(String[] cmdarray, String[] envp)
085: throws IOException {
086: return runtime.exec(cmdarray, envp);
087: }
088:
089: /**
090: * Executes the specified command in a separate process with the specified
091: * environment.
092: *
093: * @param cmd the command to call
094: * @param envp array of strings, each element of which has environment
095: * variable settings in format name=value.
096: * @return Process a Process object for managing the external process
097: * @throws IOException if an I/O error occurs.
098: */
099: public Process exec(String cmd, String[] envp) throws IOException {
100: return runtime.exec(cmd, envp);
101: }
102:
103: /**
104: * Executes the specified command in a separate process with the specified
105: * environment and working directory.
106: *
107: * @param cmd the command to call
108: * @param envp array of strings, each element of which has environment
109: * variable settings in format name=value.
110: * @param dir the working directory of the subprocess
111: * @return Process a Process object for managing the external process
112: * @throws IOException if an I/O error occurs.
113: */
114: public Process exec(String cmd, String[] envp, String dir)
115: throws IOException {
116:
117: if (dir != null && !"".equals(dir.trim())) {
118: return runtime.exec(cmd, envp, new File(dir));
119: } else {
120: return exec(cmd, envp);
121: }
122: }
123:
124: /**
125: * Executes the specified command and arguments in a separate process with
126: * the specified environment and working directory.
127: *
128: * @param cmdarray array containing the command to call and its arguments
129: * @param envp array of strings, each element of which has environment
130: * variable settings in format name=value.
131: * @param dir the working directory of the subprocess
132: * @return Process a Process object for managing the external process
133: * @throws IOException if an I/O error occurs.
134: */
135: public Process exec(String cmdarray[], String[] envp, String dir)
136: throws IOException {
137: if (dir != null && !"".equals(dir.trim())) {
138: return runtime.exec(cmdarray, envp, new File(dir));
139: } else {
140: return exec(cmdarray, envp);
141: }
142: }
143: }
|