1 22 package org.jboss.aop.standalone; 23 24 import java.io.BufferedReader ; 25 import java.io.DataInputStream ; 26 import java.io.File ; 27 import java.io.FileFilter ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.FileReader ; 31 import java.io.IOException ; 32 import java.lang.reflect.Field ; 33 import java.net.URI ; 34 import java.net.URL ; 35 import java.net.URLClassLoader ; 36 import java.net.URLDecoder ; 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.StringTokenizer ; 41 42 import javassist.bytecode.ClassFile; 43 44 import org.jboss.aop.AspectManager; 45 import org.jboss.aop.Deployment; 46 import org.jboss.aop.instrument.TransformationException; 47 48 54 public class Compiler 55 { 56 private FileFilter classFileFilter = new FileFilter () 57 { 58 public boolean accept(File pathname) 59 { 60 return pathname.getName().endsWith(".class"); 61 } 62 }; 63 64 private FileFilter directoryFilter = new FileFilter () 65 { 66 public boolean accept(File pathname) 67 { 68 return pathname.isDirectory(); 69 } 70 }; 71 72 public boolean verbose = false; 73 public boolean suppress = true; 74 public boolean optimized = true; 75 76 public boolean isJarFile(File src) 77 { 78 return (src.isFile() 79 && (src.getName().toLowerCase().endsWith(".jar") 80 || src.getName().toLowerCase().endsWith(".zip")) 81 ); 82 } 83 84 public static void main(String [] args) throws Exception 85 { 86 long start = System.currentTimeMillis(); 87 Compiler c = new Compiler (); 88 try 89 { 90 c.compile(args); 91 } 92 catch (Exception e) 93 { 94 if (c.verbose) throw e; 95 96 if (e instanceof TransformationException) 97 { 98 System.exit(1); 99 } 100 throw e; 101 } 102 System.out.println("Build Successful: " + (System.currentTimeMillis() -start) + " ms"); 103 } 104 105 public void usage() 106 { 107 System.err.println("Usage: aopc [-cp <classpath>] [-classpath <classpath>] [-report] [-noopt] [-verbose] [-aoppath <xml files>] <dir>+"); 108 } 109 110 public static URLClassLoader loader; 115 116 public void compile(String [] args) throws Exception 117 { 118 if (args.length == 0) 119 { 120 usage(); 121 System.exit(1); 122 return; 123 } 124 ArrayList paths = new ArrayList (); 125 ArrayList files = new ArrayList (); 126 boolean report = false; 127 for (int i = 0; i < args.length; i++) 128 { 129 if (args[i].equals("-verbose")) 130 { 131 verbose = true; 132 continue; 133 } 134 else if (args[i].equals("-suppress")) 135 { 136 suppress = true; 137 continue; 138 } 139 else if (args[i].equals("-noopt")) 140 { 141 optimized = false; 142 continue; 143 } 144 else if (args[i].equals("-report")) 145 { 146 report = true; 147 continue; 148 } 149 else if (args[i].equals("-cp") || args[i].equals("-classpath")) 150 { 151 if (i + 1 > args.length - 1) 152 { 153 usage(); 154 System.exit(1); 155 return; 156 } 157 i++; 158 StringTokenizer tokenizer = new StringTokenizer (args[i], File.pathSeparator); 159 while (tokenizer.hasMoreTokens()) 160 { 161 String cpath = tokenizer.nextToken(); 162 File f = new File (cpath); 163 paths.add(f.toURL()); 164 } 165 continue; 166 } 167 else if (args[i].equals("-aoppath")) 168 { 169 System.setProperty("jboss.aop.path", args[++i]); 170 continue; 171 } 172 else if (args[i].equals("-aopclasspath")) 173 { 174 System.setProperty("jboss.aop.class.path", args[++i]); 175 continue; 176 } 177 else if (args[i].equals("--SOURCEPATH")) 178 { 179 addFilesFromSourcePathFile(files, args[++i]); 180 continue; 181 } 182 File f = new File (args[i]).getCanonicalFile(); 183 files.add(f); 184 } 185 186 187 URL [] urls = (URL []) paths.toArray(new URL [paths.size()]); 188 loader = new URLClassLoader (urls, Thread.currentThread().getContextClassLoader()); 189 190 Thread.currentThread().setContextClassLoader(loader); 191 192 Deployment.searchClasspath = true; AspectManager.verbose = verbose; 194 AspectManager.suppressReferenceErrors = suppress; 195 AspectManager.optimize = optimized; 196 AspectManager.instance(); 197 198 if (report) 199 { 200 for (int i = 0; i < files.size(); i++) 201 { 202 File f = (File ) files.get(i); 203 loadFile(f); 204 } 205 FileOutputStream reportFile = new FileOutputStream ("aop-report.xml"); 206 reportFile.write(XmlReport.toXml().getBytes()); 207 reportFile.close(); 208 } 209 else 210 { 211 for (int i = 0 ; i < files.size() ; i++) 213 { 214 File f = (File )files.get(i); 215 if (f.isDirectory()) 216 { 217 addDirectory(f); 218 } 219 else if (classFileFilter.accept(f)) 220 { 221 addFile(f); 222 } 223 else 224 { 225 if (verbose) System.out.println("[aopc] " + f + " is neither a java class or a directory"); 226 } 227 } 228 229 for (Iterator it = classesToCompile.keySet().iterator() ; it.hasNext() ; ) 231 { 232 String className = (String )it.next(); 233 CompilerClassInfo info = (CompilerClassInfo)classesToCompile.get(className); 234 compileFile(info); 235 } 236 } 237 } 238 239 private HashMap classesToCompile = new HashMap (); 240 241 private void addDirectory(File dir) throws Exception 242 { 243 File [] directories = dir.listFiles(directoryFilter); 244 File [] classFiles = dir.listFiles(classFileFilter); 245 for (int i = 0; i < classFiles.length; i++) 246 { 247 addFile(classFiles[i]); 248 } 249 for (int i = 0; i < directories.length; i++) 250 { 251 addDirectory(directories[i]); 252 } 253 254 } 255 256 private void addFile(File file)throws Exception 257 { 258 ClassFile cf = createClassFile(file); 259 String className = cf.getName(); 260 String superClassName = cf.getSuperclass(); 261 CompilerClassInfo info = new CompilerClassInfo(file, className, superClassName); 262 classesToCompile.put(className, info); 263 } 264 265 private ClassFile createClassFile(final File file) throws Exception { 266 DataInputStream is = new DataInputStream (new FileInputStream (file)); 267 ClassFile cf = new ClassFile(is); 268 is.close(); 269 return cf; 270 } 271 272 private void addFilesFromSourcePathFile(ArrayList files, String sourcePathFile) 273 { 274 BufferedReader reader = null; 275 276 try 277 { 278 reader = new BufferedReader (new FileReader (new File (sourcePathFile).getCanonicalFile())); 279 280 String fileName = reader.readLine(); 281 while (fileName != null) 282 { 283 files.add(new File (fileName).getCanonicalFile()); 284 fileName = reader.readLine(); 285 } 286 } 287 catch (Exception e) 288 { 289 try 290 { 291 reader.close(); 292 } 293 catch (IOException e1) 294 { 295 } 296 throw new RuntimeException (e); 297 } 298 } 299 300 public void loadFile(File file) throws Exception 301 { 302 DataInputStream is = new DataInputStream (new FileInputStream (file)); 303 ClassFile cf = new ClassFile(is); 304 is.close(); 305 Class clazz = loader.loadClass(cf.getName()); 306 if (org.jboss.aop.Advised.class.isAssignableFrom(clazz)) 307 { 308 Field f = clazz.getDeclaredField("aop$classAdvisor$aop"); 309 f.setAccessible(true); 310 f.get(null); 311 } 312 } 313 314 public void compileFile(CompilerClassInfo info) throws Exception 315 { 316 if (info.isCompiled()) 317 { 318 return; 319 } 320 321 if (info.getSuperClassName() != null) 322 { 323 CompilerClassInfo superInfo = (CompilerClassInfo)classesToCompile.get(info.getSuperClassName()); 324 if (superInfo != null) 325 { 326 compileFile(superInfo); 327 } 328 } 329 URL classUrl = loader.getResource(info.getClassName().replace('.', '/') + ".class"); 331 if (classUrl == null) 332 { 333 System.out.println("[warning] Unable to find " + info.getFile() + " within classpath. Make sure all transforming classes are within classpath."); 334 return; 335 } 336 337 File classUrlFile = new File (URLDecoder.decode(classUrl.getFile(), "UTF-8")); 338 File infoFile = new File (URLDecoder.decode(info.getFile().toString(), "UTF-8")); 339 340 if (!classUrlFile.equals(infoFile)) 341 { 342 System.out.println("[warning] Trying to compile " + info.getFile() + " and found it also within " + classUrl.getFile() + " will not proceed. "); 343 return; 344 } 345 byte[] bytes = AspectManager.instance().transform(loader, info.getClassName(), null, null, null); 346 if (bytes == null) 347 { 348 if (verbose) System.out.println("[no comp needed] " + info.getFile()); 349 return; 350 } 351 FileOutputStream os = new FileOutputStream (infoFile); 352 os.write(bytes); 353 os.close(); 354 info.setCompiled(true); 355 if (verbose) System.out.println("[compiled] " + info.getFile()); 356 } 357 358 private class CompilerClassInfo 359 { 360 File file; 361 String className; 362 String superClassName; 363 boolean compiled; 364 365 CompilerClassInfo(File file, String className, String superClassName) 366 { 367 this.file = file; 368 this.className = className; 369 this.superClassName = superClassName; 370 } 371 372 public File getFile() 373 { 374 return file; 375 } 376 377 public boolean isCompiled() 378 { 379 return compiled; 380 } 381 382 public void setCompiled(boolean compiled) 383 { 384 this.compiled = compiled; 385 } 386 387 public String getClassName() 388 { 389 return className; 390 } 391 392 public String getSuperClassName() 393 { 394 return superClassName; 395 } 396 397 398 } 399 } 400 | Popular Tags |