source: java/PlatformTools/src/net/oni2/platformtools/applicationinvoker/ApplicationInvoker.java@ 1159

Last change on this file since 1159 was 775, checked in by alloc, 12 years ago

java PlatformTools:

  • Fixing possible nullpointer ex
File size: 5.8 KB
Line 
1package net.oni2.platformtools.applicationinvoker;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.InputStreamReader;
9import java.util.Date;
10import java.util.List;
11import java.util.Vector;
12
13import net.oni2.platformtools.PlatformInformation;
14import net.oni2.platformtools.PlatformInformation.Platform;
15
16/**
17 * @author Christian Illy
18 */
19public class ApplicationInvoker {
20 private static Vector<String> buildCommandLine(EExeType exeType,
21 File program, Vector<String> params, boolean ignoreFileNotFound)
22 throws ERuntimeNotInstalledException, FileNotFoundException {
23 if (!ignoreFileNotFound)
24 if (program.getParentFile() != null && !program.exists())
25 throw new FileNotFoundException();
26 Vector<String> cmdLine = new Vector<String>();
27 switch (exeType) {
28 case DOTNET:
29 if (!DotNet.isInstalled())
30 throw new ERuntimeNotInstalledException(
31 "No .NET runtime found");
32 if (PlatformInformation.getPlatform() != Platform.WIN)
33 cmdLine.add(DotNet.getRuntimeExe().getPath());
34 cmdLine.add(program.getPath());
35 break;
36 case JAR:
37 if (!Java.isInstalled())
38 throw new ERuntimeNotInstalledException("JRE not found");
39 cmdLine.add(Java.getRuntimeExe().getPath());
40 cmdLine.add("-jar");
41 cmdLine.add(program.getPath());
42 break;
43 case OSBINARY:
44 cmdLine.add(program.getPath());
45 break;
46 case WINEXE:
47 switch (PlatformInformation.getPlatform()) {
48 case LINUX:
49 if (!Wine.isInstalled())
50 throw new ERuntimeNotInstalledException(
51 "Can not run Windows executable because Wine was not found");
52 cmdLine.add(Wine.getRuntimeExe().getPath());
53 cmdLine.add(program.getPath());
54 break;
55 case MACOS:
56 throw new ERuntimeNotInstalledException(
57 "Can not run a Windows executable on MacOS");
58 case WIN:
59 cmdLine.add(program.getPath());
60 break;
61 case UNKNOWN:
62 throw new ERuntimeNotInstalledException(
63 "Can not run a Windows executable on an unidentified system");
64 }
65 break;
66 }
67 if (params != null)
68 cmdLine.addAll(params);
69 return cmdLine;
70 }
71
72 /**
73 * Execute a short running application and wait for termination
74 *
75 * @param exeType
76 * Type of executable to be run
77 * @param workingDirectory
78 * Working directory for executed process
79 * @param program
80 * Executable path
81 * @param params
82 * List of command and arguments
83 * @param ignoreFileNotFound
84 * Ignore if the program file is not found
85 * @return Error code and list of output lines
86 * @throws IOException
87 * Exc
88 * @throws ERuntimeNotInstalledException
89 * If the program to be executed requires a runtime which could
90 * not be found on the system
91 * @throws FileNotFoundException
92 * Program to be executed not found
93 */
94 public static ApplicationInvocationResult executeAndWait(EExeType exeType,
95 File workingDirectory, File program, Vector<String> params,
96 boolean ignoreFileNotFound) throws ERuntimeNotInstalledException,
97 FileNotFoundException, IOException {
98 return executeAndWait(workingDirectory,
99 buildCommandLine(exeType, program, params, ignoreFileNotFound));
100 }
101
102 /**
103 * Execute an app without waiting
104 *
105 * @param exeType
106 * Type of executable to be run
107 * @param workingDirectory
108 * Working directory for executed process
109 * @param program
110 * Executable path
111 * @param params
112 * List of command and arguments
113 * @param ignoreFileNotFound
114 * Ignore if the program file is not found
115 * @throws ERuntimeNotInstalledException
116 * If the program to be executed requires a runtime which could
117 * not be found on the system
118 * @throws FileNotFoundException
119 * Program to be executed not found
120 */
121 public static void execute(EExeType exeType, File workingDirectory,
122 File program, Vector<String> params, boolean ignoreFileNotFound)
123 throws ERuntimeNotInstalledException, FileNotFoundException {
124 execute(buildCommandLine(exeType, program, params, ignoreFileNotFound),
125 workingDirectory);
126 }
127
128 /**
129 * Execute a short running application and wait for termination
130 *
131 * @param workingDirectory
132 * Working directory for executed process
133 * @param cmdLine
134 * List of command and arguments
135 * @return Error code and list of output lines
136 * @throws IOException
137 * Exc
138 */
139 private static ApplicationInvocationResult executeAndWait(
140 File workingDirectory, Vector<String> cmdLine) throws IOException {
141 long start = new Date().getTime();
142 ProcessBuilder pb = new ProcessBuilder(cmdLine);
143 pb.redirectErrorStream(true);
144 Process proc = pb.start();
145 if (workingDirectory != null)
146 pb.directory(workingDirectory);
147
148 InputStream is = proc.getInputStream();
149 InputStreamReader isr = new InputStreamReader(is);
150 BufferedReader br = new BufferedReader(isr);
151
152 String line;
153 Vector<String> lines = new Vector<String>();
154
155 while ((line = br.readLine()) != null) {
156 lines.add(line);
157 }
158 try {
159 proc.waitFor();
160 } catch (InterruptedException e) {
161 // TODO Auto-generated catch block
162 e.printStackTrace();
163 }
164 return new ApplicationInvocationResult(proc.exitValue(), cmdLine,
165 lines, (int) (new Date().getTime() - start));
166 }
167
168 /**
169 * Execute an app without waiting
170 *
171 * @param cmd
172 * Command and parameters
173 * @param workingDirectory
174 * Working directory of app
175 */
176 private static void execute(List<String> cmd, File workingDirectory) {
177 try {
178 ProcessBuilder pb = new ProcessBuilder(cmd);
179 if (workingDirectory != null)
180 pb.directory(workingDirectory);
181 pb.start();
182 } catch (IOException e) {
183 e.printStackTrace();
184 }
185 }
186
187}
Note: See TracBrowser for help on using the repository browser.