KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > compiler > Utils


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  */

4 /***
5  * Jonathan: an Open Distributed Processing Environment
6  * Copyright (C) 1999 France Telecom R&D
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * Release: 2.0
23  *
24  * Contact: jonathan@objectweb.org
25  *
26  * Author: Kathleen Milsted
27  *
28  * with contributions from:
29  * Francois Horn
30  * Bruno Dumant
31  * Vincent Sheffer
32  *
33  */

34 package org.objectweb.carol.cmi.compiler;
35
36 import java.io.BufferedReader JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.InputStreamReader JavaDoc;
40 import java.io.PrintStream JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.Collections JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 /**
47  * This is a utility class containing useful static methods for the stub
48  * compiler.
49  */

50 public class Utils {
51
52     /**
53      * Class name of the sun javac compiler
54      */

55     private static final String JavaDoc COMPILER_CLASS_NAME = "com.sun.tools.javac.Main";
56
57     static void compileFile(Compiler JavaDoc cCtx, String JavaDoc fullFileName) throws CompilerException {
58         compileFiles(cCtx, Collections.singleton(fullFileName));
59     }
60
61     static void compileFiles(Compiler JavaDoc cCtx, Collection JavaDoc fullFileNames)
62         throws CompilerException {
63
64         if (cCtx.isInvokeCmd()) {
65             String JavaDoc classpath = "";
66             if (cCtx.getClassPath() != null) {
67                 classpath = cCtx.getClassPath() + System.getProperty("path.separator", "")
68                         + System.getProperty("java.class.path", "");
69             } else {
70                 classpath = System.getProperty("java.class.path", "");
71             }
72
73             ArrayList JavaDoc args = new ArrayList JavaDoc();
74             args.add("-d");
75             args.add(cCtx.getDestDir());
76             args.add("-classpath");
77             args.add(classpath);
78             args.addAll(fullFileNames);
79
80             try {
81                 // Use reflection
82
Class JavaDoc c = Class.forName(COMPILER_CLASS_NAME);
83                 Object JavaDoc compiler = c.newInstance();
84                 java.lang.reflect.Method JavaDoc compile = c.getMethod("compile", new Class JavaDoc[] {(new String JavaDoc[] {}).getClass()});
85                 int result = ((Integer JavaDoc) compile.invoke(compiler, new Object JavaDoc[] {args.toArray(new String JavaDoc[]{})})).intValue();
86                 if (result != 0) {
87                     throw new CompilerException("Compilation of " + fullFileNames + " ended abnormally with code "
88                             + result);
89                 }
90             } catch (Exception JavaDoc e) {
91                 throw new CompilerException("While compiling " + fullFileNames, e);
92             }
93         } else {
94             StringBuffer JavaDoc files = new StringBuffer JavaDoc();
95             for (Iterator JavaDoc it = fullFileNames.iterator(); it.hasNext(); ) {
96                 String JavaDoc fullFileName = (String JavaDoc) it.next();
97                 files.append(fullFileName);
98                 files.append(" ");
99             }
100
101             String JavaDoc command = cCtx.getCompiler() + " -d " + cCtx.getDestDir() + " " + files;
102
103             String JavaDoc classpath = "";
104             if (cCtx.getClassPath() != null) {
105                 classpath = cCtx.getClassPath() + System.getProperty("path.separator", "")
106                         + System.getProperty("java.class.path", "");
107             } else {
108                 classpath = System.getProperty("java.class.path", "");
109             }
110
111             String JavaDoc[] env = new String JavaDoc[] {"CLASSPATH=" + classpath};
112
113             Process JavaDoc proc;
114             try {
115                 proc = Runtime.getRuntime().exec(command, env);
116             } catch (IOException JavaDoc e) {
117                 throw new CompilerException("While compiling " + fullFileNames, e);
118             }
119
120             Thread JavaDoc stdoutThread = new Thread JavaDoc(new RunnableStreamListener(new BufferedReader JavaDoc(new InputStreamReader JavaDoc(proc
121                     .getInputStream())), System.out), "stdout listener for " + cCtx.getCompiler());
122             stdoutThread.start();
123
124             Thread JavaDoc stderrThread = new Thread JavaDoc(new RunnableStreamListener(new BufferedReader JavaDoc(new InputStreamReader JavaDoc(proc
125                     .getErrorStream())), System.err), "stderr listener for " + cCtx.getCompiler());
126             stderrThread.start();
127             int n;
128             try {
129                 n = proc.waitFor();
130             } catch (InterruptedException JavaDoc e1) {
131                 throw new CompilerException("While compiling " + fullFileNames, e1);
132             }
133             if (n != 0) {
134                 throw new CompilerException("Compilation of " + fullFileNames + " ended abnormally with code " + n);
135             }
136         }
137     }
138
139     static void deleteFiles(Collection JavaDoc fileNames) {
140         for (Iterator JavaDoc it = fileNames.iterator(); it.hasNext(); ) {
141             deleteFile((String JavaDoc) it.next());
142         }
143     }
144
145     static void deleteFile(String JavaDoc fileName) {
146         if (fileName == null || fileName == "") return;
147         File JavaDoc f = new File JavaDoc(fileName);
148         if (f.exists()) {
149             f.delete();
150         }
151     }
152 }
153
154 class RunnableStreamListener implements Runnable JavaDoc {
155
156     BufferedReader JavaDoc is;
157
158     PrintStream JavaDoc ps;
159
160     RunnableStreamListener(BufferedReader JavaDoc istream, PrintStream JavaDoc pstream) {
161         is = istream;
162         ps = pstream;
163     }
164
165     public void run() {
166         String JavaDoc line;
167         try {
168             while ((line = is.readLine()) != null)
169                 ps.println(line);
170         } catch (IOException JavaDoc e) {
171             ps.println(e.toString());
172         }
173     }
174 }
175
Popular Tags