public File call() throws IOException { System.err.println(Thread.currentThread() + ":instrumenting " + path); String prefix = path.substring(0, path.length() - 4); int slash = prefix.lastIndexOf("/"); if (slash != -1) prefix = prefix.substring(slash + 1); prefix += "-"; File zipTmp = File.createTempFile("instrumented-" + prefix, path.substring(path.length() - 4)); try { zipTmp.deleteOnExit(); ZipInputStream zin = new ZipInputStream(new FileInputStream(file)); ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zipTmp)); ZipEntry entry; while ((entry = zin.getNextEntry()) != null) { InputStream in; long size; if (entry.getName().endsWith(".class")) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy(zin, baos); byte[] bytes = instrumenter.instrumentClass(baos.toByteArray()); size = bytes.length; in = new ByteArrayInputStream(bytes); } else { in = zin; size = entry.getSize(); } ZipEntry newEntry = new ZipEntry(entry); newEntry.setSize(size); newEntry.setCompressedSize(-1); zout.putNextEntry(newEntry); copy(in, zout); if (entry.getName().endsWith(".class")) { in.close(); } } zout.close(); return zipTmp; } catch (IOException e) { zipTmp.delete(); throw e; } }
public static List<File> instrument( List<File> srcPaths, String instrumenterFileName, String instrumenterClassName) { if (instrumenterFileName == null) return srcPaths; if (instrumenterClassName == null) return srcPaths; Instrumenter instrumenter; try { List<URL> tmpUrls = new ArrayList<URL>(); for (File file : srcPaths) { tmpUrls.add(file.toURI().toURL()); } ClassLoader tmpLoader = new URLClassLoader( tmpUrls.toArray(new URL[tmpUrls.size()]), InstrumenterWorker.class.getClassLoader()); instrumenter = (Instrumenter) tmpLoader.loadClass(instrumenterClassName).newInstance(); } catch (IllegalAccessException e) { e.printStackTrace(); return srcPaths; } catch (InstantiationException e) { e.printStackTrace(); return srcPaths; } catch (IOException e) { e.printStackTrace(); return srcPaths; } catch (ClassNotFoundException e) { return srcPaths; } ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor( ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors()); try { File instrumenterFile = new File(instrumenterFileName); instrumenterFile.delete(); instrumenter.open(instrumenterFile, true); List<Future<File>> futures = new ArrayList<Future<File>>(); for (File file : srcPaths) { String path = file.getPath(); if (path.matches(".*/ofbiz[^/]*\\.(jar|zip)")) { futures.add(executor.submit(new FileInstrumenter(instrumenter, file))); } else { futures.add(new ConstantFutureFile(file)); } } List<File> result = new ArrayList<File>(futures.size()); for (Future<File> future : futures) { result.add(future.get()); } instrumenter.close(); return result; } catch (ExecutionException e) { e.printStackTrace(); return srcPaths; } catch (InterruptedException e) { e.printStackTrace(); return srcPaths; } catch (IOException e) { e.printStackTrace(); return srcPaths; } finally { executor.shutdown(); } }