Package: JavaExecution$1

JavaExecution$1

nameinstructionbranchcomplexitylinemethod
run()
M: 38 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * VDM Code Generator
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.codegen.tests.exec.util;
23:
24: import java.io.BufferedReader;
25: import java.io.File;
26: import java.io.IOException;
27: import java.io.InputStream;
28: import java.io.InputStreamReader;
29: import java.util.Arrays;
30: import java.util.List;
31: import java.util.Vector;
32:
33: import org.overture.codegen.tests.util.JavaToolsUtils;
34:
35: public class JavaExecution
36: {
37:         public final static String cpPathSeparator = System.getProperty("path.separator");
38:
39:         public static String run(String mainClassName, String[] preArgs,
40:                         String[] postArgs, File directory, File... cp)
41:         {
42:                 String javaHome = System.getenv(JavaToolsUtils.JAVA_HOME);
43:                 File java = new File(new File(javaHome, JavaToolsUtils.BIN_FOLDER), JavaToolsUtils.JAVA);
44:                 return run(java, directory, mainClassName, preArgs, postArgs, cp);
45:         }
46:
47:         public static String run(File java, File directory, String mainClassName,
48:                         String[] preArgs, String[] postArgs, File... cp)
49:         {
50:                 String cpArgs = consCpArg(cp);
51:
52:                 Process p = null;
53:                 ProcessBuilder pb = null;
54:
55:                 try
56:                 {
57:                         String javaArg = JavaToolsUtils.isWindows() ? java.getAbsolutePath()
58:                                         : "java";
59:
60:                         List<String> commands = new Vector<String>();
61:                         commands.add(javaArg);
62:                         commands.addAll(Arrays.asList(preArgs));
63:                         commands.add("-cp");
64:                         commands.add(cpArgs);
65:                         commands.add(mainClassName.trim());
66:                         commands.addAll(Arrays.asList(postArgs));
67:
68:                         pb = new ProcessBuilder(commands);
69:
70:                         pb.directory(directory);
71:                         pb.redirectErrorStream(true);
72:
73:                         try
74:                         {
75:                                 p = pb.start();
76:                                 return getProcessOutput(p);
77:
78:                         } catch (InterruptedException e)
79:                         {
80:                                 e.printStackTrace();
81:                                 return null;
82:                         } finally
83:                         {
84:                                 if (p != null)
85:                                 {
86:                                         p.destroy();
87:                                 }
88:                         }
89:                 } catch (IOException e)
90:                 {
91:                         e.printStackTrace();
92:                         return null;
93:                 }
94:
95:         }
96:
97:         private static String getProcessOutput(Process p)
98:                         throws InterruptedException
99:         {
100:                 final StringBuffer sb = new StringBuffer();
101:                 final InputStream is = p.getInputStream();
102:                 is.mark(0);
103:
104:                 // the background thread watches the output from the process
105:                 Thread t = new Thread(new Runnable()
106:                 {
107:                         public void run()
108:                         {
109:                                 try
110:                                 {
111:                                         BufferedReader reader = new BufferedReader(new InputStreamReader(is));
112:                                         String line;
113:•                                        while ((line = reader.readLine()) != null)
114:                                         {
115:                                                 sb.append(line + "\n");
116:
117:                                         }
118:                                 } catch (IOException e)
119:                                 {
120:                                         e.printStackTrace();
121:                                 } finally
122:                                 {
123:                                         try
124:                                         {
125:                                                 is.close();
126:                                         } catch (IOException e)
127:                                         {
128:                                                 e.printStackTrace();
129:                                         }
130:                                 }
131:                         }
132:                 });
133:
134:                 t.start();
135:                 p.waitFor();
136:                 t.join();
137:
138:                 return sb.toString();
139:         }
140:
141:         private static String consCpArg(File... file)
142:         {
143:                 String cp = "";
144:                 for (File file2 : file)
145:                 {
146:                         String cpPart = consCpArg(file2);
147:
148:                         if (cp.length() == 0)
149:                         {
150:                                 cp = cpPart;
151:                         } else
152:                         {
153:                                 cp += cpPathSeparator + cpPart;
154:                         }
155:                 }
156:
157:                 return cp;
158:         }
159:
160:         private static String consCpArg(File file)
161:         {
162:                 if (file == null)
163:                 {
164:                         return JavaToolsUtils.CURRENT_FOLDER;
165:                 }
166:
167:                 if (file.isFile())
168:                 {
169:                         if (file.getName().endsWith(".class"))
170:                         {
171:                                 return JavaToolsUtils.CURRENT_FOLDER;
172:                         } else
173:                         {
174:                                 return file.getAbsolutePath();
175:                         }
176:
177:                 }
178:                 StringBuilder sb = new StringBuilder();
179:
180:                 File[] allFiles = file.listFiles();
181:
182:                 if (allFiles == null || allFiles.length == 0)
183:                 {
184:                         return JavaToolsUtils.CURRENT_FOLDER;
185:                 }
186:
187:                 if (file.isDirectory())
188:                 {
189:                         sb.append(file.getAbsolutePath() + cpPathSeparator);
190:                 }
191:
192:                 for (int i = 0; i < allFiles.length; i++)
193:                 {
194:                         File currentFile = allFiles[i];
195:
196:                         if (currentFile.isDirectory())
197:                         {
198:                                 sb.append(currentFile.getAbsolutePath() + cpPathSeparator);
199:                         }
200:                 }
201:
202:                 if (sb.length() == 0)
203:                 {
204:                         return JavaToolsUtils.CURRENT_FOLDER;
205:                 }
206:
207:                 return sb.substring(0, sb.length() - 1);
208:         }
209: }