1 16 package org.apache.cocoon.components.language.programming.java; 17 18 import java.io.BufferedInputStream ; 19 import java.io.BufferedReader ; 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.StringTokenizer ; 27 28 import org.apache.avalon.framework.service.ServiceException; 29 import org.apache.avalon.framework.service.ServiceManager; 30 import org.apache.avalon.framework.service.Serviceable; 31 import org.apache.cocoon.components.language.programming.CompilerError; 32 import org.apache.cocoon.components.thread.RunnableManager; 33 import EDU.oswego.cs.dl.util.concurrent.CountDown; 34 35 42 43 public class Jikes extends AbstractJavaCompiler implements Serviceable { 44 45 static final int OUTPUT_BUFFER_SIZE = 1024; 46 static final int BUFFER_SIZE = 512; 47 48 private ServiceManager m_serviceManager; 49 50 private static class StreamPumper implements Runnable { 51 52 private BufferedInputStream stream; 53 private boolean endOfStream = false; 54 private int SLEEP_TIME = 5; 55 private OutputStream out; 56 private CountDown m_done; 57 58 public StreamPumper(BufferedInputStream is, OutputStream out, CountDown done) { 59 this.stream = is; 60 this.out = out; 61 m_done = done; 62 } 63 64 public void pumpStream() throws IOException { 65 byte[] buf = new byte[BUFFER_SIZE]; 66 if (!endOfStream) { 67 int bytesRead = stream.read(buf, 0, BUFFER_SIZE); 68 69 if (bytesRead > 0) { 70 out.write(buf, 0, bytesRead); 71 } else if (bytesRead == -1) { 72 endOfStream = true; 73 } 74 } 75 } 76 77 public void run() { 78 try { 79 while (!endOfStream) { 80 pumpStream(); 81 Thread.sleep(SLEEP_TIME); 82 } 83 } catch (Exception e) { 84 } 86 m_done.release(); } 88 } 89 90 93 public void service( ServiceManager serviceManager ) 94 throws ServiceException 95 { 96 m_serviceManager = serviceManager; 97 } 98 99 105 protected String [] toStringArray(List arguments) { 106 int i; 107 108 for (i = 0; i < arguments.size(); i++) { 109 String arg = (String ) arguments.get(i); 110 if (arg.equals("-sourcepath")) { 111 arguments.remove(i); 113 arguments.remove(i); 114 break; 115 } 116 } 117 118 String [] args = new String [arguments.size() + 1]; 119 for (i = 0; i < arguments.size(); i++) { 120 args[i] = (String ) arguments.get(i); 121 } 122 123 args[i] = file; 124 125 return args; 126 } 127 128 131 public boolean compile() throws IOException { 132 133 List args = new ArrayList (); 134 args.add("jikes"); 136 args.add("+E"); 138 args.add("-nowarn"); 141 142 int exitValue; 143 ByteArrayOutputStream tmpErr = new ByteArrayOutputStream (OUTPUT_BUFFER_SIZE); 144 145 try { 146 Process p = Runtime.getRuntime().exec(toStringArray(fillArguments(args))); 147 148 BufferedInputStream compilerErr = new BufferedInputStream (p.getErrorStream()); 149 150 RunnableManager runnableManager = null; 151 try 152 { 153 runnableManager = (RunnableManager)m_serviceManager.lookup( RunnableManager.ROLE ); 154 } 155 catch( final ServiceException se ) 156 { 157 getLogger().error( "Cannot get RunnableManager", se ); 158 throw new IOException ( "Cannot get RunnableManager" ); 159 } 160 161 final CountDown done = new CountDown( 1 ); 162 StreamPumper errPumper = new StreamPumper(compilerErr, tmpErr, done); 163 runnableManager.execute( errPumper ); 164 m_serviceManager.release( runnableManager ); 165 166 p.waitFor(); 167 exitValue = p.exitValue(); 168 169 done.acquire(); compilerErr.close(); 171 172 p.destroy(); 173 174 tmpErr.close(); 175 this.errors = new ByteArrayInputStream (tmpErr.toByteArray()); 176 177 } catch (InterruptedException somethingHappened) { 178 getLogger().debug("Jikes.compile():SomethingHappened", somethingHappened); 179 return false; 180 } 181 182 return ((exitValue == 0) && (tmpErr.size() == 0)); 187 } 188 189 197 protected List parseStream(BufferedReader input) throws IOException { 198 List errors = null; 199 String line = null; 200 StringBuffer buffer = null; 201 202 while (true) { 203 buffer = new StringBuffer (); 206 if (line == null) line = input.readLine(); 208 if (line == null) return errors; 209 buffer.append(line); 210 211 while (true) { 213 line = input.readLine(); 214 if (line == null) 216 break; 217 if (line.length() > 0 && line.charAt(0) != ' ') 219 break; 220 buffer.append('\n'); 221 buffer.append(line); 222 } 223 224 if (errors == null) errors = new ArrayList (); 226 227 errors.add(parseError(buffer.toString())); 229 } 230 } 231 232 238 private CompilerError parseError(String error) { 239 StringTokenizer tokens = new StringTokenizer (error, ":"); 240 String file = tokens.nextToken(); 241 if (file.length() == 1) file = new StringBuffer (file).append(":").append(tokens.nextToken()).toString(); 242 StringBuffer message = new StringBuffer (); 243 String type = ""; 244 int startline = 0; 245 int startcolumn = 0; 246 int endline = 0; 247 int endcolumn = 0; 248 249 try { 250 startline = Integer.parseInt(tokens.nextToken()); 251 startcolumn = Integer.parseInt(tokens.nextToken()); 252 endline = Integer.parseInt(tokens.nextToken()); 253 endcolumn = Integer.parseInt(tokens.nextToken()); 254 } catch (Exception e) { 255 message.append("Please ensure that you have your JDK's rt.jar listed in your classpath. Jikes needs it to operate."); 257 type="error"; 258 getLogger().error(message.toString(), e); 259 } 260 261 if ("".equals(message.toString())) { 262 type = tokens.nextToken().trim().toLowerCase(); 263 message.append(tokens.nextToken("\n").substring(1).trim()); 264 265 while (tokens.hasMoreTokens()) 266 message.append("\n").append(tokens.nextToken()); 267 } 268 269 return new CompilerError(file, type.equals("error"), startline, startcolumn, endline, endcolumn, message.toString()); 270 } 271 272 public String toString() { 273 return "IBM Jikes Compiler"; 274 } 275 } 276 | Popular Tags |