KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > engine > EngineRuntime


1 package org.antlr.works.engine;
2
3 import org.antlr.works.IDE;
4 import org.antlr.works.prefs.AWPrefs;
5 import org.antlr.works.utils.Console;
6 import org.antlr.works.utils.StreamWatcher;
7 import org.antlr.works.utils.StreamWatcherDelegate;
8 import org.antlr.works.utils.Utils;
9 import org.antlr.xjlib.foundation.XJSystem;
10 import org.antlr.xjlib.foundation.XJUtils;
11
12 import java.io.File JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16 /*
17
18 [The "BSD licence"]
19 Copyright (c) 2005-2006 Jean Bovet
20 All rights reserved.
21
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions
24 are met:
25
26 1. Redistributions of source code must retain the above copyright
27 notice, this list of conditions and the following disclaimer.
28 2. Redistributions in binary form must reproduce the above copyright
29 notice, this list of conditions and the following disclaimer in the
30 documentation and/or other materials provided with the distribution.
31 3. The name of the author may not be used to endorse or promote products
32 derived from this software without specific prior written permission.
33
34 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
35 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
38 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45 */

46
47 public class EngineRuntime {
48
49     public static Map JavaDoc<Thread JavaDoc,Process JavaDoc> processPerThread = new HashMap JavaDoc<Thread JavaDoc, Process JavaDoc>();
50
51     public static void setProcess(Process JavaDoc p) {
52         processPerThread.put(Thread.currentThread(), p);
53     }
54
55     public static void removeProcess() {
56         processPerThread.remove(Thread.currentThread());
57     }
58
59     public static Process JavaDoc getProcess(Thread JavaDoc t) {
60         return processPerThread.get(t);
61     }
62
63     public static Process JavaDoc getProcess() {
64         return processPerThread.get(Thread.currentThread());
65     }
66
67     public static String JavaDoc getClassPath(String JavaDoc outputPath) {
68         String JavaDoc appPath = IDE.getApplicationPath();
69
70         String JavaDoc isDebuggingPlugin = System.getProperty("org.antlr.works.debug.plugin");
71         if(isDebuggingPlugin != null && Boolean.parseBoolean(isDebuggingPlugin)) {
72             // If we are debugging the plugin in IntelliJ, add all the jar in the lib
73
// folder of the plugin sandbox (usually on Mac OS X at ~/Library/Preferences/sandbox/plugins/...)
74
String JavaDoc sandbox = XJUtils.getPathByDeletingLastComponent(appPath)+File.separatorChar+"lib";
75             File JavaDoc dir = new File JavaDoc(sandbox);
76             if(dir.isDirectory()) {
77                 for(File JavaDoc f : dir.listFiles()) {
78                     String JavaDoc candidate = f.getAbsolutePath();
79                     if(candidate.endsWith(".jar")) {
80                         appPath += File.pathSeparatorChar+candidate;
81                     }
82                 }
83             }
84         }
85
86         // Need to include the path of the application in order to be able
87
// to compile the parser if the system classpath doesn't have ANTLR or ST
88
String JavaDoc classPath = outputPath;
89         if(appPath != null)
90             classPath += File.pathSeparatorChar+Utils.unquotePath(appPath);
91
92         if(AWPrefs.getUseSystemClassPath())
93             classPath += File.pathSeparatorChar+Utils.unquotePath(System.getProperty("java.class.path"));
94
95         if(AWPrefs.getUseCustomClassPath())
96             classPath += File.pathSeparatorChar+Utils.unquotePath(AWPrefs.getCustomClassPath());
97
98         classPath += File.pathSeparatorChar+".";
99
100         // On Mac OS X, quoting the path works fine except within IntelliJ when
101
// AW is working as a plugin. Without quoting, it works everywhere in Mac
102
// OS X so I decided to quote only on Windows.
103
if(XJSystem.isWindows())
104             return Utils.quotePath(classPath);
105         else
106             return classPath;
107     }
108
109     public static String JavaDoc runANTLR(Console console, String JavaDoc file, String JavaDoc libPath, String JavaDoc outputPath, StreamWatcherDelegate delegate) {
110         String JavaDoc error = null;
111         StreamWatcher esw = null;
112         int result = 0;
113         try {
114             String JavaDoc[] args = new String JavaDoc[9];
115             args[0] = "java";
116             args[1] = "-cp";
117             args[2] = getClassPath(outputPath);
118             args[3] = "org.antlr.Tool";
119             args[4] = "-o";
120             args[5] = Utils.quotePath(outputPath);
121             args[6] = "-lib";
122             args[7] = Utils.quotePath(libPath);
123             args[8] = file;
124
125             IDE.debugVerbose(console, EngineRuntime.class, "Run ANTLR: "+Utils.toString(args));
126
127             Process JavaDoc p = Runtime.getRuntime().exec(args);
128             setProcess(p);
129             esw = new StreamWatcher(p.getErrorStream(), "ANTLR[error]", delegate);
130             esw.start();
131             new StreamWatcher(p.getInputStream(), "ANTLR[stdout]", delegate).start();
132             result = p.waitFor();
133         } catch(Exception JavaDoc e) {
134             error = "Failed to run ANTLR with exception:\n"+e.toString();
135         } finally {
136             removeProcess();
137         }
138
139         if(result != 0) {
140             error = "Failed to run ANTLR with result:\n"+result;
141         }
142
143         /** Make sure ANTLR didn't return an error in the error string
144          *
145          */

146
147         if(error == null && esw != null) {
148             for (String JavaDoc line : esw.getLines()) {
149                 if (line.startsWith("ANTLR Parser Generator"))
150                     continue;
151
152                 if (line.startsWith("no such locale file"))
153                     continue;
154
155                 error = line;
156                 break;
157             }
158         }
159         return error;
160     }
161
162     public static String JavaDoc runJava(Console console, String JavaDoc currentPath, String JavaDoc[] params, StreamWatcherDelegate delegate) {
163         String JavaDoc error = null;
164         int result = 0;
165         try {
166             String JavaDoc[] args = new String JavaDoc[3+params.length];
167             args[0] = "java";
168             args[1] = "-cp";
169             args[2] = getClassPath(currentPath);
170             System.arraycopy(params, 0, args, 3, params.length);
171
172             IDE.debugVerbose(console, EngineRuntime.class, "Run Java: "+Utils.toString(args));
173
174             Process JavaDoc p = Runtime.getRuntime().exec(args, null, new File JavaDoc(currentPath));
175             setProcess(p);
176             new StreamWatcher(p.getErrorStream(), "Java[error]", delegate).start();
177             new StreamWatcher(p.getInputStream(), "Java[stdout]", delegate).start();
178             result = p.waitFor();
179         } catch(Exception JavaDoc e) {
180             error = "Failed to run Java with exception:\n"+e.toString();
181         } finally {
182             removeProcess();
183         }
184
185         if(result != 0) {
186             error = "Failed to run Java with result:\n"+result;
187         }
188
189         return error;
190     }
191
192     public static String JavaDoc compileFiles(Console console, String JavaDoc[] files, String JavaDoc outputFileDir, StreamWatcherDelegate delegate) {
193         String JavaDoc error = null;
194
195         int result = 0;
196         try {
197             String JavaDoc compiler = AWPrefs.getCompiler();
198             String JavaDoc classPath = getClassPath(outputFileDir);
199
200             if(compiler.equalsIgnoreCase(AWPrefs.COMPILER_JAVAC)) {
201                 String JavaDoc[] args = new String JavaDoc[5+files.length];
202                 if(AWPrefs.getJavaCCustomPath())
203                     args[0] = XJUtils.concatPath(AWPrefs.getJavaCPath(), "javac");
204                 else
205                     args[0] = "javac";
206                 args[1] = "-classpath";
207                 args[2] = classPath;
208                 args[3] = "-d";
209                 args[4] = Utils.quotePath(outputFileDir);
210                 System.arraycopy(files, 0, args, 5, files.length);
211
212                 IDE.debugVerbose(console, EngineRuntime.class, "Compile: "+Utils.toString(args));
213
214                 Process JavaDoc p = Runtime.getRuntime().exec(args);
215                 setProcess(p);
216                 new StreamWatcher(p.getErrorStream(), "Compiler[error]", delegate).start();
217                 new StreamWatcher(p.getInputStream(), "Compiler[stdout]", delegate).start();
218                 result = p.waitFor();
219             } else if(compiler.equalsIgnoreCase(AWPrefs.COMPILER_JIKES)) {
220                 String JavaDoc jikesPath = XJUtils.concatPath(AWPrefs.getJikesPath(), "jikes");
221
222                 String JavaDoc[] args = new String JavaDoc[5+files.length];
223                 args[0] = jikesPath;
224                 args[1] = "-classpath";
225                 args[2] = classPath;
226                 args[3] = "-d";
227                 args[4] = Utils.quotePath(outputFileDir);
228                 System.arraycopy(files, 0, args, 5, files.length);
229
230                 IDE.debugVerbose(console, EngineRuntime.class, "Compile: "+Utils.toString(args));
231
232                 Process JavaDoc p = Runtime.getRuntime().exec(args);
233                 setProcess(p);
234                 new StreamWatcher(p.getErrorStream(), "Compiler[error]", delegate).start();
235                 new StreamWatcher(p.getInputStream(), "Compiler[stdout]", delegate).start();
236                 result = p.waitFor();
237             } else if(compiler.equalsIgnoreCase(AWPrefs.COMPILER_INTEGRATED)) {
238                 String JavaDoc[] args = new String JavaDoc[2+files.length];
239                 args[0] = "-d";
240                 args[1] = outputFileDir;
241                 System.arraycopy(files, 0, args, 2, files.length);
242
243                 Class JavaDoc<?> javac = Class.forName("com.sun.tools.javac.Main");
244                 Class JavaDoc[] p = new Class JavaDoc[] { String JavaDoc[].class };
245                 Method JavaDoc m = javac.getMethod("compile", p);
246                 Object JavaDoc[] a = new Object JavaDoc[] { args };
247                 Object JavaDoc r = m.invoke(javac.newInstance(), a);
248                 result = (Integer JavaDoc) r;
249                 //result = com.sun.tools.javac.Main.compile(args);
250
}
251
252         } catch(Error JavaDoc e) {
253             error = "Compiler error:\n"+e.toString();
254             e.printStackTrace();
255         } catch(Exception JavaDoc e) {
256             error = "Compiler exception:\n"+e.toString();
257             e.printStackTrace();
258         } finally {
259             removeProcess();
260         }
261
262         if(result != 0) {
263             error = "Compiler failed with result code "+result;
264         }
265
266         return error;
267     }
268
269 }
270
Popular Tags