KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > externalprocess > ProcessFactory


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.externalprocess;
11
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 /**
16  * Process Factory creates a external process.
17  *
18  * The factory could be used to create a process in another way than java.lang.
19  * Runtime.exec();
20  *
21  * @author Nico Klasens (Finalist IT Group)
22  * @version $Id: ProcessFactory.java,v 1.3 2003/05/12 13:10:47 kees Exp $
23  * @since MMBase-1.6
24  */

25 public class ProcessFactory {
26
27     /**
28      * instance of the Process Factory
29      */

30     static private ProcessFactory instance = new ProcessFactory();
31
32     /**
33      * Runtime of the current java process
34      */

35     private Runtime JavaDoc runtime;
36
37     /**
38     * get the Process Factory.instance
39     * @return ProcessFactory
40     */

41     public static ProcessFactory getFactory() {
42         return instance;
43     }
44
45     /**
46      * Create an new process factory.
47      */

48     private ProcessFactory() {
49         runtime = Runtime.getRuntime();
50     }
51
52     /**
53      * Executes the specified command in a separate process.
54      *
55      * @param cmd the command to call
56      * @return Process a Process object for managing the external process
57      * @throws IOException if an I/O error occurs.
58      */

59     public Process JavaDoc exec(String JavaDoc cmd) throws IOException JavaDoc {
60         return runtime.exec(cmd);
61     }
62
63     /**
64      * Executes the specified command and arguments in a separate process.
65      *
66      * @param cmdarray array containing the command to call and its arguments
67      * @return Process a Process object for managing the external process
68      * @throws IOException if an I/O error occurs.
69      */

70     public Process JavaDoc exec(String JavaDoc[] cmdarray) throws IOException JavaDoc {
71         return runtime.exec(cmdarray);
72     }
73
74     /**
75      * Executes the specified command and arguments in a separate process with
76      * the specified environment.
77      *
78      * @param cmdarray array containing the command to call and its arguments
79      * @param envp array of strings, each element of which has environment
80      * variable settings in format name=value.
81      * @return Process a Process object for managing the external process
82      * @throws IOException if an I/O error occurs.
83      */

84     public Process JavaDoc exec(String JavaDoc[] cmdarray, String JavaDoc[] envp) throws IOException JavaDoc {
85         return runtime.exec(cmdarray, envp);
86     }
87
88     /**
89      * Executes the specified command in a separate process with the specified
90      * environment.
91      *
92      * @param cmd the command to call
93      * @param envp array of strings, each element of which has environment
94      * variable settings in format name=value.
95      * @return Process a Process object for managing the external process
96      * @throws IOException if an I/O error occurs.
97      */

98     public Process JavaDoc exec(String JavaDoc cmd, String JavaDoc[] envp) throws IOException JavaDoc {
99         return runtime.exec(cmd, envp);
100     }
101
102     /**
103      * Executes the specified command in a separate process with the specified
104      * environment and working directory.
105      *
106      * @param cmd the command to call
107      * @param envp array of strings, each element of which has environment
108      * variable settings in format name=value.
109      * @param dir the working directory of the subprocess
110      * @return Process a Process object for managing the external process
111      * @throws IOException if an I/O error occurs.
112      */

113     public Process JavaDoc exec(String JavaDoc cmd, String JavaDoc[] envp, String JavaDoc dir) throws IOException JavaDoc {
114
115         if (dir != null && !"".equals(dir.trim())) {
116             return runtime.exec(cmd, envp, new File JavaDoc(dir));
117         } else {
118             return exec(cmd, envp);
119         }
120     }
121
122     /**
123     * Executes the specified command and arguments in a separate process with
124     * the specified environment and working directory.
125     *
126     * @param cmdarray array containing the command to call and its arguments
127     * @param envp array of strings, each element of which has environment
128     * variable settings in format name=value.
129     * @param dir the working directory of the subprocess
130     * @return Process a Process object for managing the external process
131     * @throws IOException if an I/O error occurs.
132     */

133     public Process JavaDoc exec(String JavaDoc cmdarray[], String JavaDoc[] envp, String JavaDoc dir) throws IOException JavaDoc {
134         if (dir != null && !"".equals(dir.trim())) {
135             return runtime.exec(cmdarray, envp, new File JavaDoc(dir));
136         } else {
137             return exec(cmdarray, envp);
138         }
139     }
140 }
141
Popular Tags