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

Last change on this file since 762 was 724, checked in by alloc, 12 years ago

Platform tools library (information about platform, program execution)

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