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 System.setErr(new PrintStream(new ByteArrayOutputStream()));
100 int res = Main.compile((String[])args.toArray(new String[args.size()]));
102 System.setErr(oldErr);
104
105 boolean errors = res != 0;
106 if(errors){
107 args = argsSave;
109 for(int i = 0; i < sourceFiles.size(); i++){
110 String aSourceFile = (String)sourceFiles.get(i);
111 args.add(aSourceFile);
112 res = Main.compile((String[])args.toArray(new String[args.size()]));
114 if(res != 0){
115 Err.prln("\nThe offending input was:\n");
117 String source = (String)sourceListings.get(i);
118 source = Strings.addLineNumbers(source);
119 Err.prln(source);
120 }
121 args.remove(args.size() -1);
122 }
123
124 }
125
126 try{
129 loadAllClasses(classesDir, null);
130 }catch(IOException ioe){
131 throw new GateException(ioe);
132 }
133
134 Files.rmdir(workDir);
136
137 if(errors) throw new GateException(
138 "There were errors; see error log for details!");
139 }
140
141
146 protected static List getPathComponents(String classname){
147 StringTokenizer strTok = new StringTokenizer(classname, ".", false);
149 List pathComponents = new ArrayList();
150 while(strTok.hasMoreTokens()){
151 String pathComponent = strTok.nextToken();
152 pathComponents.add(pathComponent);
153 }
154 return pathComponents;
155 }
156
157
163 protected static File getDirectory(File workDir, List pathComponents){
164 File currentDir = workDir;
165 for(int i = 0; i < pathComponents.size() - 1; i++){
166 String dirName = (String)pathComponents.get(i);
167 currentDir = new File(currentDir, dirName);
169 if(currentDir.exists()){
170 if(currentDir.isDirectory()){
171 }else{
173 throw new GateRuntimeException(
174 "Path exists but is not a directory ( " +
175 currentDir.toString() + ")!");
176 }
177 }else{
178 if (!currentDir.mkdir())
179 throw new GateRuntimeException(
180 "Cannot create a temporary directory!");
181 }
182 }
183 return currentDir;
184 }
185
186
190 protected static void loadAllClasses(File classesDirectory,
191 String packageName) throws IOException{
192 File[] files = classesDirectory.listFiles();
193 if(packageName == null){
195 packageName = "";
197 }else{
198 packageName += packageName.length() == 0 ?
200 classesDirectory.getName() :
201 "." + classesDirectory.getName();
202 }
203
204 for(int i = 0; i < files.length; i++){
205 if(files[i].isDirectory()) loadAllClasses(files[i], packageName);
206 else{
207 String filename = files[i].getName();
208 if(filename.endsWith(".class")){
209 String className = packageName + "." +
210 filename.substring(0, filename.length() - 6);
211 byte[] bytes = Files.getByteArray(files[i]);
213 classLoader.defineGateClass(className, bytes, 0, bytes.length);
214 }
215 }
216 }
217
218 }
219 protected static GateClassLoader classLoader;
220 }
221