1 22 package org.jboss.aop.ant; 23 24 25 import java.io.BufferedWriter ; 26 import java.io.File ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.DirectoryScanner; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.taskdefs.Java; 36 import org.apache.tools.ant.taskdefs.MatchingTask; 37 import org.apache.tools.ant.types.CommandlineJava; 38 import org.apache.tools.ant.types.Environment; 39 import org.apache.tools.ant.types.Path; 40 import org.apache.tools.ant.types.Reference; 41 42 48 public class AopC extends MatchingTask 49 { 50 final static String SYS_OPTIMIZED = "jboss.aop.optimized"; 51 final static String SYS_INSTRUMENTOR = "jboss.aop.instrumentor"; 52 53 private static String lSep = System.getProperty("line.separator"); 54 private int maxSrc = 1000; 55 56 private String instrumentor; 57 private String jvm; 58 private Path classpath; 59 private Path aoppath; 60 private Path aopclasspath; 61 private Path compilerClasspath; 62 private Path compileSourcepath; 63 private boolean verbose = false; 64 private boolean suppress = true; 65 private boolean report = false; 66 private boolean optimized = true; 67 private String maxmemory = null; 68 ArrayList sysproperties = new ArrayList (); 69 70 73 protected boolean failOnError = true; 74 75 78 public void setVerbose(boolean i) 79 { 80 verbose = i; 81 } 82 83 public boolean getVerbose() 84 { 85 return verbose; 86 } 87 88 public String getInstrumentor() 89 { 90 return instrumentor; 91 } 92 93 public void setInstrumentor(String instrumentor) 94 { 95 this.instrumentor = instrumentor; 96 } 97 98 101 public void setSuppress(boolean i) 102 { 103 suppress = i; 104 } 105 106 public boolean getSupress() 107 { 108 return suppress; 109 } 110 111 public void setReport(boolean i) 112 { 113 report = i; 114 } 115 116 public boolean getReport() 117 { 118 return report; 119 } 120 121 public String getJvm() 122 { 123 return jvm; 124 } 125 126 public void setJvm(String jvm) 127 { 128 this.jvm = jvm; 129 } 130 131 public void setOptimized(boolean optimized) 132 { 133 this.optimized = optimized; 134 } 135 136 public boolean getOptimized() 137 { 138 return optimized; 139 } 140 141 144 public void setMaxmemory(String maxmemory) 145 { 146 this.maxmemory = maxmemory; 147 } 148 149 public String getMaxmemory() 150 { 151 return maxmemory; 152 } 153 154 155 159 public void setFailonerror(boolean fail) 160 { 161 failOnError = fail; 162 } 163 164 167 public boolean getFailonerror() 168 { 169 return failOnError; 170 } 171 172 175 public void setClasspath(Path cp) 176 { 177 if (classpath == null) 178 { 179 classpath = cp; 180 } 181 else 182 { 183 classpath.append(cp); 184 } 185 } 186 187 public void setMaxSrc(int maxSrc) 188 { 189 this.maxSrc = maxSrc; 190 } 191 192 public int getMaxSrc() 193 { 194 return maxSrc; 195 } 196 197 200 public Path createClasspath() 201 { 202 if (classpath == null) 203 { 204 classpath = new Path(getProject()); 205 } 206 return classpath.createPath(); 207 } 208 209 212 public void setClasspathRef(Reference r) 213 { 214 createClasspath().setRefid(r); 215 } 216 217 public Path getClasspath() 218 { 219 return classpath; 220 } 221 222 225 public void setCompilerClasspathRef(Reference r) 226 { 227 if (compilerClasspath == null) compilerClasspath = new Path(getProject()); 228 compilerClasspath.setRefid(r); 229 } 230 231 234 public void setCompilerclasspath(Path cp) 235 { 236 if (compilerClasspath == null) 237 { 238 compilerClasspath = cp; 239 } 240 else 241 { 242 compilerClasspath.append(cp); 243 } 244 } 245 246 249 public Path getCompilerclasspath() 250 { 251 return compilerClasspath; 252 } 253 254 257 public Path createCompilerclasspath() 258 { 259 if (compilerClasspath == null) 260 { 261 compilerClasspath = new Path(getProject()); 262 } 263 return compilerClasspath.createPath(); 264 } 265 266 271 public Path createSrc() 272 { 273 if (compileSourcepath == null) 274 { 275 compileSourcepath = new Path(getProject()); 276 } 277 return compileSourcepath.createPath(); 278 } 279 280 public Path createAoppath() 281 { 282 if (aoppath == null) 283 { 284 aoppath = new Path(getProject()); 285 } 286 Path path = aoppath.createPath(); 288 return path; 289 } 290 291 public Path createAopclasspath() 292 { 293 if (aopclasspath == null) 294 { 295 aopclasspath = new Path(getProject()); 296 } 297 return aopclasspath.createPath(); 298 } 299 300 public void addSysproperty(Environment.Variable property) 301 { 302 sysproperties.add(property); 303 } 304 305 public void execute() 306 throws BuildException 307 { 308 CommandlineJava cmd = new CommandlineJava(); 309 310 if (verbose) 311 cmd.createArgument().setValue("-verbose"); 312 if (suppress) 313 cmd.createArgument().setValue("-suppress"); 314 if (!optimized) 315 cmd.createArgument().setValue("-noopt"); 316 if (report) 317 cmd.createArgument().setValue("-report"); 318 if (aoppath != null && aoppath.size() > 0) 319 { 320 cmd.createArgument().setValue("-aoppath"); 321 cmd.createArgument().setValue(aoppath.toString()); 322 } 323 if (aopclasspath != null && aopclasspath.size() > 0) 324 { 325 cmd.createArgument().setValue("-aopclasspath"); 326 cmd.createArgument().setValue(aopclasspath.toString()); 327 } 328 logAndAddFilesToCompile(cmd); 329 330 try 331 { 332 classpath.append(compilerClasspath); 335 Java java = (Java) (getProject().createTask("java")); 336 if (jvm != null && jvm.length() > 0) 337 { 338 java.setJvm(jvm); 339 } 340 if (getClasspath() != null) 341 { 342 getProject().log("using user supplied classpath: " 343 + getClasspath(), Project.MSG_DEBUG); 344 java.setClasspath(getClasspath() 345 .concatSystemClasspath("ignore")); 346 } 347 else 348 { 349 Path classpath = new Path(getProject()); 350 classpath = classpath.concatSystemClasspath("only"); 351 getProject().log("using system classpath: " + classpath, 352 Project.MSG_DEBUG); 353 java.setClasspath(classpath); 354 } 355 java.setDir(getProject().getBaseDir()); 356 java.setClassname("org.jboss.aop.standalone.Compiler"); 357 String args[] = cmd.getJavaCommand().getArguments(); 359 for (int i = 0; i < args.length; i++) 360 { 361 java.createArg().setValue(args[i]); 362 } 363 java.setFailonerror(getFailonerror()); 364 java.setFork(true); 367 java.setTaskName("aopc"); 368 369 Environment.Variable optimize = new Environment.Variable(); 370 optimize.setKey(SYS_OPTIMIZED); 371 optimize.setValue(String.valueOf(optimized)); 372 java.addSysproperty(optimize); 373 374 for (Iterator it = sysproperties.iterator() ; it.hasNext() ; ) 375 { 376 Environment.Variable var = (Environment.Variable)it.next(); 377 java.addSysproperty(var); 378 } 379 380 if (maxmemory != null) 381 { 382 java.setMaxmemory(maxmemory); 383 } 384 java.execute(); 385 } 386 catch (Exception ex) 387 { 388 if (ex instanceof BuildException) 389 { 390 throw (BuildException) ex; 391 } 392 else 393 { 394 throw new BuildException("Error running aopc compiler: ", 395 ex, getLocation()); 396 } 397 } 398 } 399 400 protected void logAndAddFilesToCompile(CommandlineJava cmd) 401 { 402 String [] list = compileSourcepath.list(); 405 ArrayList compilingFiles = new ArrayList (); 406 for (int i = 0; i < list.length; i++) 407 { 408 File srcDir = getProject().resolveFile(list[i]); 409 if (!srcDir.exists()) 410 { 411 throw new BuildException("srcdir \"" 412 + srcDir.getPath() 413 + "\" does not exist!", getLocation()); 414 } 415 416 DirectoryScanner ds = this.getDirectoryScanner(srcDir); 417 String [] files = ds.getIncludedFiles(); 418 for (int j = 0; j < files.length; j++) 419 { 420 if (files[j].endsWith(".class")) 421 { 422 File f = new File (srcDir, files[j]); 423 compilingFiles.add(f.getAbsolutePath()); 424 } 425 } 426 } 427 428 int length = 0; 429 for (int i = 0; i < compilingFiles.size(); i++) 430 { 431 length += ((String ) compilingFiles.get(i)).length() + 1; 433 434 if (length > maxSrc) 435 { 436 break; 437 } 438 } 439 440 if (length < maxSrc) 441 { 442 StringBuffer niceSourceList = new StringBuffer ("Files\n"); 443 for (int i = 0; i < compilingFiles.size(); i++) 444 { 445 String file = (String ) compilingFiles.get(i); 446 cmd.createArgument().setValue(file); 447 niceSourceList.append(" " + file + lSep); 448 } 449 450 } 452 else 453 { 454 BufferedWriter writer = null; 455 try 456 { 457 File sourceFiles = File.createTempFile("src", ".tmp"); 458 if (verbose) 459 { 460 System.out.println("[info] Total length of filenames to be compiled is greater than " 461 + maxSrc + ", listing files in --SOURCEPATH: " + sourceFiles.getAbsolutePath()); 462 } 463 464 sourceFiles.deleteOnExit(); 465 writer = new BufferedWriter (new FileWriter (sourceFiles)); 466 467 for (int i = 0; i < compilingFiles.size(); i++) 468 { 469 writer.write((String ) compilingFiles.get(i)); 470 writer.newLine(); 471 } 472 writer.flush(); 473 cmd.createArgument().setValue("--SOURCEPATH"); 474 cmd.createArgument().setValue(sourceFiles.getAbsolutePath()); 475 } 476 catch (IOException e) 477 { 478 if (writer != null) 479 { 480 try 481 { 482 writer.close(); 483 } 484 catch (IOException e1) 485 { 486 } 487 } 488 throw new RuntimeException (e); 489 } 490 } 491 } 492 493 } | Popular Tags |