Source code: com/flexstor/common/util/FlexProcess.java
1 /*
2 * FlexProcess.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:31 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.util;
12
13 import java.io.File;
14 import java.io.IOException;
15
16 public class FlexProcess
17 {
18 // Name of data file containing arguments for external process
19 protected final static String TEMP_FILE = "Temporary Data";
20 protected final static String LINE_FEED = System.getProperty("line.separator");
21 protected final static char PATH_SEPARATOR = System.getProperty("path.separator").charAt(0);
22
23 protected String sExecName;
24 protected String[] saArguments = new String[0]; // program arguments
25 protected String[] saServerNames = new String[0]; // server names for UNC paths
26 protected boolean bCommand = false; //force command mode, determines if arguments will be
27 //passes to executable or written to file on Mac
28
29 /**
30 * This constructor takes an executable name and an array of Strings
31 * as arguments. The argument array will be prepared for each specific
32 * platform, i.e.
33 * Win: prepend server name to create a UNC path
34 * Mac: write to temporary file to feed into AppleSript
35 * Other: nothing
36 * the server name is required to create a UNC name for Windows platforms.
37 * The class then call Runtime.exec() to create the new process.
38 */
39 protected FlexProcess(String sExecName, String[] saArguments, String[] saServerNames)
40 {
41 this.sExecName = new String(sExecName);
42 if (saArguments != null)
43 {
44 this.saArguments = new String[saArguments.length];
45 // need to create a copy here, because arguments might be changed during execution!
46 System.arraycopy(saArguments, 0, this.saArguments, 0, saArguments.length);
47 }
48 if (saServerNames != null)
49 {
50 this.saServerNames = new String[saServerNames.length];
51 System.arraycopy(saServerNames, 0, this.saServerNames, 0, saServerNames.length);
52 }
53 }
54
55 /**
56 * Gets the name of the temporary folder. Defaults to path of
57 * executable if subclasses do not override this method.
58 * @return temp folder
59 */
60 public String getTempFolder()
61 {
62 String sTmpDir = "";
63
64 // write temp file to directory where executable resides
65 if (sExecName.lastIndexOf(File.separatorChar) > 0)
66 sTmpDir = sExecName.substring(0, 1 + sExecName.lastIndexOf(File.separatorChar));
67
68 return sTmpDir;
69 }
70
71 /**
72 * Gets the name of the temporary data file. Defaults to path of
73 * executable if subclasses do not override this method.
74 * @return temp file name
75 */
76 public String getTempFileName()
77 {
78 return getTempFolder() + TEMP_FILE;
79 }
80
81 public FlexProcess setCommandMode(boolean bCommand)
82 {
83 this.bCommand = bCommand;
84 return this;
85 }
86
87 /**
88 * Overwritten by subclasses.
89 * Responsibilty is to modify the argument array for the specific platform.
90 * @param the argument list for the external process
91 */
92 protected void prepareArguments()
93 throws IOException
94 {
95 // by default does nothing
96 }
97
98 /**
99 * Convenience method for one argument.
100 */
101 public static FlexProcess createProcess(String sExecName, String sArgument, String sServerName)
102 {
103 String[] saArgument = new String[1];
104 String[] saServer = new String[1];
105 saArgument[0] = sArgument;
106 saServer[0] = sServerName;
107 return createProcess(sExecName, saArgument, saServer);
108 }
109
110 public static FlexProcess createProcess(String sExecName, String[] saArguments, String[] saServerNames)
111 {
112 if (System.getProperty("mrj.version") != null)
113 return (new MacProcess(sExecName, saArguments, saServerNames));
114 else if (System.getProperty("os.name").indexOf("Window") != -1)
115 return (new WinProcess(sExecName, saArguments, saServerNames));
116 else
117 return (new FlexProcess(sExecName, saArguments, saServerNames));
118 }
119
120 /**
121 * Execute the external process
122 */
123 public void execute()
124 throws IOException
125 {
126 String[] saCmd;
127
128 prepareArguments();
129 saCmd = new String[saArguments.length + 1];
130 saCmd[0] = sExecName;
131 System.arraycopy(saArguments, 0, saCmd, 1, saArguments.length);
132
133 Diagnostic.trace(Diagnostic.CAT_PROCESS, "Application: " + saCmd[0]);
134 for (int i = 1; i < saCmd.length; i++)
135 Diagnostic.trace(Diagnostic.CAT_PROCESS, "Argument: " + saCmd[i]);
136
137 try
138 {
139 Runtime.getRuntime().exec(saCmd); // start the application
140 }
141 // invalid path on the Mac throws an InternalError, not an IOException ?
142 // TO CATCH THAT, WE NEED TO CATCH THROWABLE
143 catch (Throwable e)
144 {
145 IOException ioe = new IOException();
146 ioe.fillInStackTrace();
147 throw ioe;
148 //throw ((new IOException()).fillInStackTrace());
149 }
150 }
151
152 } //FlexProcess