1 4 34 package org.objectweb.carol.cmi.compiler; 35 36 import java.io.BufferedReader ; 37 import java.io.File ; 38 import java.io.IOException ; 39 import java.io.InputStreamReader ; 40 import java.io.PrintStream ; 41 import java.util.ArrayList ; 42 import java.util.Collection ; 43 import java.util.Collections ; 44 import java.util.Iterator ; 45 46 50 public class Utils { 51 52 55 private static final String COMPILER_CLASS_NAME = "com.sun.tools.javac.Main"; 56 57 static void compileFile(Compiler cCtx, String fullFileName) throws CompilerException { 58 compileFiles(cCtx, Collections.singleton(fullFileName)); 59 } 60 61 static void compileFiles(Compiler cCtx, Collection fullFileNames) 62 throws CompilerException { 63 64 if (cCtx.isInvokeCmd()) { 65 String 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 args = new ArrayList (); 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 Class c = Class.forName(COMPILER_CLASS_NAME); 83 Object compiler = c.newInstance(); 84 java.lang.reflect.Method compile = c.getMethod("compile", new Class [] {(new String [] {}).getClass()}); 85 int result = ((Integer ) compile.invoke(compiler, new Object [] {args.toArray(new String []{})})).intValue(); 86 if (result != 0) { 87 throw new CompilerException("Compilation of " + fullFileNames + " ended abnormally with code " 88 + result); 89 } 90 } catch (Exception e) { 91 throw new CompilerException("While compiling " + fullFileNames, e); 92 } 93 } else { 94 StringBuffer files = new StringBuffer (); 95 for (Iterator it = fullFileNames.iterator(); it.hasNext(); ) { 96 String fullFileName = (String ) it.next(); 97 files.append(fullFileName); 98 files.append(" "); 99 } 100 101 String command = cCtx.getCompiler() + " -d " + cCtx.getDestDir() + " " + files; 102 103 String 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 [] env = new String [] {"CLASSPATH=" + classpath}; 112 113 Process proc; 114 try { 115 proc = Runtime.getRuntime().exec(command, env); 116 } catch (IOException e) { 117 throw new CompilerException("While compiling " + fullFileNames, e); 118 } 119 120 Thread stdoutThread = new Thread (new RunnableStreamListener(new BufferedReader (new InputStreamReader (proc 121 .getInputStream())), System.out), "stdout listener for " + cCtx.getCompiler()); 122 stdoutThread.start(); 123 124 Thread stderrThread = new Thread (new RunnableStreamListener(new BufferedReader (new InputStreamReader (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 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 fileNames) { 140 for (Iterator it = fileNames.iterator(); it.hasNext(); ) { 141 deleteFile((String ) it.next()); 142 } 143 } 144 145 static void deleteFile(String fileName) { 146 if (fileName == null || fileName == "") return; 147 File f = new File (fileName); 148 if (f.exists()) { 149 f.delete(); 150 } 151 } 152 } 153 154 class RunnableStreamListener implements Runnable { 155 156 BufferedReader is; 157 158 PrintStream ps; 159 160 RunnableStreamListener(BufferedReader istream, PrintStream pstream) { 161 is = istream; 162 ps = pstream; 163 } 164 165 public void run() { 166 String line; 167 try { 168 while ((line = is.readLine()) != null) 169 ps.println(line); 170 } catch (IOException e) { 171 ps.println(e.toString()); 172 } 173 } 174 } 175 | Popular Tags |