Author: doogie
Date: Sat Jun 26 19:40:51 2010 New Revision: 958275 URL: http://svn.apache.org/viewvc?rev=958275&view=rev Log: Refactored instrument() so that the main body of work is done by a static helper class. Modified: ofbiz/trunk/framework/start/src/org/ofbiz/base/start/InstrumenterWorker.java Modified: ofbiz/trunk/framework/start/src/org/ofbiz/base/start/InstrumenterWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/start/src/org/ofbiz/base/start/InstrumenterWorker.java?rev=958275&r1=958274&r2=958275&view=diff ============================================================================== --- ofbiz/trunk/framework/start/src/org/ofbiz/base/start/InstrumenterWorker.java (original) +++ ofbiz/trunk/framework/start/src/org/ofbiz/base/start/InstrumenterWorker.java Sat Jun 26 19:40:51 2010 @@ -76,45 +76,7 @@ public final class InstrumenterWorker { for (File file: srcPaths) { String path = file.getPath(); if (path.matches(".*/ofbiz[^/]*\\.(jar|zip)")) { - System.err.println("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(); - file = zipTmp; - } catch (IOException e) { - zipTmp.delete(); - throw e; - } + file = new FileInstrumenter(instrumenter, file).instrument(); } result.add(file); } @@ -125,5 +87,59 @@ public final class InstrumenterWorker { return srcPaths; } } + + private static final class FileInstrumenter { + private final Instrumenter instrumenter; + private final File file; + private final String path; + + protected FileInstrumenter(Instrumenter instrumenter, File file) { + this.instrumenter = instrumenter; + this.file = file; + this.path = file.getPath(); + } + + protected File instrument() throws IOException { + System.err.println("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; + } + } + } } |
Free forum by Nabble | Edit this page |