1
14 package gate.util;
15
16 import java.io.*;
17 import java.util.*;
18
19 import com.sun.tools.javac.Main;
20
21 import gate.Gate;
22 import gate.GateConstants;
23 import gate.creole.ExecutionException;
24
25
30 public class Javac implements GateConstants{
31
32
40 public static void loadClasses(Map sources)throws GateException{
41 if(classLoader == null) classLoader = Gate.getClassLoader();
42 File workDir;
43 File srcDir;
44 File classesDir;
45 try{
46 workDir = File.createTempFile("gate", "");
47 if(!workDir.delete()) throw new GateRuntimeException(
48 "Cannot delete a temporary file!");
49 if(! workDir.mkdir())throw new GateRuntimeException(
50 "Cannot create a temporary directory!");
51 srcDir = new File(workDir, "src");
52 if(! srcDir.mkdir())throw new GateRuntimeException(
53 "Cannot create a temporary directory!");
54 classesDir = new File(workDir, "classes");
55 if(! classesDir.mkdir())throw new GateRuntimeException(
56 "Cannot create a temporary directory!");
57 }catch(IOException ioe){
58 throw new ExecutionException(ioe);
59 }
60
61 List sourceFiles = new ArrayList();
62 List sourceListings = new ArrayList();
63
64 Iterator fileIter = sources.keySet().iterator();
65 while(fileIter.hasNext()){
66 String className = (String)fileIter.next();
67 List pathComponents = getPathComponents(className);
68 String source = (String)sources.get(className);
69 File directory = getDirectory(srcDir, pathComponents);
70 String fileName = (String) pathComponents.get(pathComponents.size() - 1);
71 File srcFile = new File(directory, fileName + ".java");
72 try{
73 Writer fw = new OutputStreamWriter(new FileOutputStream(srcFile, false),
76 "UTF-8");
77 fw.write(source);
78 fw.flush();fw.close();
79 sourceFiles.add(srcFile.getCanonicalPath());
80 sourceListings.add(source);
81 }catch(IOException ioe){
82 throw new GateException(ioe);
83 }
84 }
85 List args = new ArrayList();
88 args.add("-sourcepath");
89 args.add(srcDir.getAbsolutePath());
90 args.add("-encoding");
91 args.add("UTF-8");
92 args.add("-d");
93 args.add(classesDir.getAbsolutePath());
94 List argsSave = new ArrayList(args);
96 args.addAll(sourceFiles);
97 PrintStream oldErr = System.err;
99 int res = -1;
101 try{
102
106 System.setErr(new PrintStream(new ByteArrayOutputStream(10 * 1024)));
108 res = Main.compile((String[])args.toArray(new String[args.size()]));
109 }catch(Throwable t){
110 System.setErr(oldErr);
113 throw new GateRuntimeException(t);
114 }finally{
115 System.setErr(oldErr);
117 }
118
119 boolean errors = res != 0;
120 if(errors){
121 args = argsSave;
123 for(int i = 0; i < sourceFiles.size(); i++){
124 String aSourceFile = (String)sourceFiles.get(i);
125 args.add(aSourceFile);
126 res = Main.compile((String[])args.toArray(new String[args.size()]));
128 if(res != 0){
129 Err.prln("\nThe offending input was:\n");
131 String source = (String)sourceListings.get(i);
132 source = Strings.addLineNumbers(source);
133 Err.prln(source);
134 }
135 args.remove(args.size() -1);
136 }
137
138 }
139
140 try{
143 loadAllClasses(classesDir, null);
144 }catch(IOException ioe){
145 throw new GateException(ioe);
146 }
147
148 Files.rmdir(workDir);
150
151 if(errors) throw new GateException(
152 "There were errors; see error log for details!");
153 }
154
155
160 protected static List getPathComponents(String classname){
161 StringTokenizer strTok = new StringTokenizer(classname, ".", false);
163 List pathComponents = new ArrayList();
164 while(strTok.hasMoreTokens()){
165 String pathComponent = strTok.nextToken();
166 pathComponents.add(pathComponent);
167 }
168 return pathComponents;
169 }
170
171
177 protected static File getDirectory(File workDir, List pathComponents){
178 File currentDir = workDir;
179 for(int i = 0; i < pathComponents.size() - 1; i++){
180 String dirName = (String)pathComponents.get(i);
181 currentDir = new File(currentDir, dirName);
183 if(currentDir.exists()){
184 if(currentDir.isDirectory()){
185 }else{
187 throw new GateRuntimeException(
188 "Path exists but is not a directory ( " +
189 currentDir.toString() + ")!");
190 }
191 }else{
192 if (!currentDir.mkdir())
193 throw new GateRuntimeException(
194 "Cannot create a temporary directory!");
195 }
196 }
197 return currentDir;
198 }
199
200
204 protected static void loadAllClasses(File classesDirectory,
205 String packageName) throws IOException{
206 File[] files = classesDirectory.listFiles();
207 if(packageName == null){
209 packageName = "";
211 }else{
212 packageName += packageName.length() == 0 ?
214 classesDirectory.getName() :
215 "." + classesDirectory.getName();
216 }
217
218 for(int i = 0; i < files.length; i++){
219 if(files[i].isDirectory()) loadAllClasses(files[i], packageName);
220 else{
221 String filename = files[i].getName();
222 if(filename.endsWith(".class")){
223 String className = packageName + "." +
224 filename.substring(0, filename.length() - 6);
225 byte[] bytes = Files.getByteArray(files[i]);
227 classLoader.defineGateClass(className, bytes, 0, bytes.length);
228 }
229 }
230 }
231
232 }
233 protected static GateClassLoader classLoader;
234 }
235