Method: consCpArg(File[])

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.InputStreamReader;
27: import java.io.PrintStream;
28: import java.util.List;
29: import java.util.Vector;
30:
31: import org.overture.codegen.tests.util.JavaToolsUtils;
32:
33: public class JavaCommandLineCompiler
34: {
35:         public static ProcessResult compile(File dir, File[] cpJars)
36:         {
37:                 String javaHome = System.getenv(JavaToolsUtils.JAVA_HOME);
38:                 File javac = new File(new File(javaHome, JavaToolsUtils.BIN_FOLDER), JavaToolsUtils.JAVAC);
39:                 return compile(javac, dir, cpJars);
40:         }
41:
42:         public static ProcessResult compile(File javac, File dir, File[] cpJars)
43:         {
44:                 boolean compileOk = true;
45:
46:                 List<File> files = getJavaSourceFiles(dir);
47:
48:                 String arguments = buildFileArgs(files);
49:
50:                 StringBuilder out = new StringBuilder();
51:                 Process p = null;
52:                 try
53:                 {
54:                         String line = "";
55:                         ProcessBuilder pb = null;
56:                         String arg = "";
57:
58:                         String cpArg = "";
59:                         if (cpJars != null && cpJars.length > 0)
60:                         {
61:                                 cpArg = consCpArg(cpJars);
62:                         }
63:
64:                         if (JavaToolsUtils.isWindows())
65:                         {
66:                                 pb = new ProcessBuilder(javac.getAbsolutePath(), cpArg, arguments.trim());
67:                         } else
68:                         {
69:                                 arg = "javac"// -nowarn -J-client -J-Xms100m -J-Xmx100m"
70:                                                 + cpArg + " " + arguments.replace('\"', ' ').trim();
71:                         }
72:
73:                         if (pb != null)
74:                         {
75:                                 pb.directory(dir);
76:                                 pb.redirectErrorStream(true);
77:                                 p = pb.start();
78:                         } else
79:                         {
80:                                 p = Runtime.getRuntime().exec(arg, null, dir);
81:                         }
82:
83:                         p.waitFor();
84:
85:                         BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream()));
86:                         String secondLastLine = "";
87:                         while ((line = input.readLine()) != null)
88:                         {
89:                                 out.append("\n" + line);
90:                                 secondLastLine = line;
91:                         }
92:
93:                         compileOk = !secondLastLine.toLowerCase().contains("error");
94:                         input.close();
95:                 } catch (Exception err)
96:                 {
97:                         System.err.println(out.toString());
98:                         err.printStackTrace();
99:
100:                 } finally
101:                 {
102:                         if (p != null)
103:                         {
104:                                 p.destroy();
105:                         }
106:                 }
107:
108:                 return new ProcessResult(compileOk ? 0 : 1, out);
109:         }
110:
111:         private static String consCpArg(File[] cpJars)
112:         {
113:                 StringBuilder sb = new StringBuilder();
114:                 sb.append(" -cp ");
115:
116:                 String sep = "";
117:
118:•                for (File jar : cpJars)
119:                 {
120:                         sb.append(sep);
121:                         sb.append(jar.getAbsolutePath());
122:                         sep = File.pathSeparator;
123:                 }
124:
125:                 return sb.toString();
126:         }
127:
128:         public static void printCompileMessage(List<File> files, File cpJar)
129:         {
130:                 PrintStream out = System.out;
131:
132:                 out.println("Compiling (JAVA):");
133:                 if (cpJar != null)
134:                 {
135:                         out.println("Class path: " + cpJar.getName());
136:                 }
137:
138:                 for (File file : files)
139:                 {
140:                         out.println(" " + file.getName());
141:                 }
142:
143:         }
144:
145:         private static String buildFileArgs(List<File> files)
146:         {
147:                 StringBuilder sb = new StringBuilder();
148:
149:                 for (File file : files)
150:                 {
151:                         sb.append("\"" + file.getAbsolutePath() + "\" ");
152:                 }
153:
154:                 return sb.toString();
155:         }
156:
157:         private static List<File> getJavaSourceFiles(File file)
158:         {
159:                 List<File> files = new Vector<File>();
160:
161:                 if (file.isFile())
162:                 {
163:                         return files;
164:                 }
165:
166:                 for (File f : file.listFiles())
167:                 {
168:                         if (f.isDirectory())
169:                         {
170:                                 files.addAll(getJavaSourceFiles(f));
171:                         } else if (f.getName().toLowerCase().endsWith(".java"))
172:                         {
173:                                 files.add(f);
174:                         }
175:                 }
176:                 return files;
177:         }
178: }