| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.jcoverage; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileFilter; |
| 37 | | | import java.io.IOException; |
| 38 | | | import java.util.ArrayList; |
| 39 | | | import java.util.Iterator; |
| 40 | | | import java.util.List; |
| 41 | | | import java.util.logging.Level; |
| 42 | | | import java.util.logging.Logger; |
| 43 | | | |
| 44 | | | import net.sourceforge.cobertura.instrument.Main; |
| 45 | | | |
| 46 | | | import org.jcoderz.commons.util.FileUtils; |
| 47 | | | import org.jcoderz.commons.util.JarUtils; |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | @author |
| 53 | | | |
| 54 | | | public final class Instrumenter |
| 55 | | | { |
| 56 | 0 | | private static final String CLASSNAME = Instrumenter.class.getName(); |
| 57 | | | |
| 58 | 0 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 59 | | | |
| 60 | | | private final ConfigurationParameters mConfig; |
| 61 | | | |
| 62 | 0 | | private final File mBaseTempDir |
| 63 | | | = new File(System.getProperty("java.io.tmpdir")); |
| 64 | | | |
| 65 | | | |
| 66 | | | |
| 67 | | | @param |
| 68 | | | |
| 69 | | | public Instrumenter (ConfigurationParameters config) |
| 70 | 0 | | { |
| 71 | 0 | | mConfig = config; |
| 72 | 0 | | } |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | @param |
| 77 | | | |
| 78 | | | public static void main (String[] args) |
| 79 | | (1)(2) | throws Exception |
| 80 | | | { |
| 81 | 0 | | Main.main(args); |
| 82 | | | |
| 83 | 0 | | logger.info("Instrumentation of '" + args[0] + "' done."); |
| 84 | 0 | | } |
| 85 | | | |
| 86 | | (3) | private static void parseArguments (String[] args) |
| 87 | | | { |
| 88 | | | |
| 89 | | | try |
| 90 | | | { |
| 91 | 0 | | for (int i = 0; i < args.length; ) |
| 92 | | | { |
| 93 | 0 | | logger.info("Parsing argument '" |
| 94 | | | + args[i] + "' = '" + args[i + 1] + "'"); |
| 95 | | | |
| 96 | 0 | (4) | if (args[i].equals("-jcoverage")) |
| 97 | | | { |
| 98 | | (5) | |
| 99 | | | } |
| 100 | 0 | (6) | else if (args[i].equals("-log4j")) |
| 101 | | | { |
| 102 | | (7) | |
| 103 | | | } |
| 104 | 0 | (8) | else if (args[i].equals("-archive")) |
| 105 | | | { |
| 106 | | (9) | |
| 107 | | | } |
| 108 | 0 | (10) | else if (args[i].equals("-loglevel")) |
| 109 | | | { |
| 110 | 0 | | Logger.getLogger("").setLevel(Level.parse(args[i + 1])); |
| 111 | | | } |
| 112 | | | else |
| 113 | | | { |
| 114 | 0 | | throw new IllegalArgumentException("Invalid argument '" |
| 115 | | | + args[i] + "'"); |
| 116 | | | } |
| 117 | | | |
| 118 | 0 | | ++i; |
| 119 | 0 | | ++i; |
| 120 | | | } |
| 121 | | | } |
| 122 | 0 | | catch (IndexOutOfBoundsException e) |
| 123 | | | { |
| 124 | 0 | | final IllegalArgumentException ex = new IllegalArgumentException( |
| 125 | | | "Missing value for " + args[args.length - 1]); |
| 126 | 0 | | ex.initCause(e); |
| 127 | 0 | | throw ex; |
| 128 | 0 | | } |
| 129 | | | |
| 130 | | (11) | |
| 131 | | (12) | |
| 132 | | (13) | |
| 133 | | | |
| 134 | 0 | | } |
| 135 | | | |
| 136 | | | |
| 137 | | | |
| 138 | | | @param |
| 139 | | | @throws |
| 140 | | | |
| 141 | | (14) | private static void checkFileExists (File f) |
| 142 | | | { |
| 143 | 0 | | if (!f.exists()) |
| 144 | | | { |
| 145 | 0 | | throw new IllegalArgumentException("The file '" |
| 146 | | | + f.getAbsolutePath() + "' does not exists."); |
| 147 | | | } |
| 148 | 0 | | } |
| 149 | | | |
| 150 | | | |
| 151 | | | |
| 152 | | | @throws |
| 153 | | | |
| 154 | | | public void instrument () |
| 155 | | | throws IOException |
| 156 | | | { |
| 157 | 0 | | instrument(mConfig.getArchive()); |
| 158 | 0 | | } |
| 159 | | | |
| 160 | | | |
| 161 | | | <code></code> |
| 162 | | | @param |
| 163 | | | @throws |
| 164 | | | |
| 165 | | | public void instrument (File f) |
| 166 | | | throws IOException |
| 167 | | | { |
| 168 | 0 | | if (isArchive(f)) |
| 169 | | | { |
| 170 | 0 | | final File tempDir |
| 171 | | | = FileUtils.createTempDir(mBaseTempDir, f.getName()); |
| 172 | 0 | | JarUtils.extractJarArchive(tempDir, f); |
| 173 | | | |
| 174 | 0 | | instrument(tempDir); |
| 175 | | | |
| 176 | 0 | | if (!f.delete()) |
| 177 | | | { |
| 178 | 0 | | throw new IOException("Cannot remove file " + f); |
| 179 | | | } |
| 180 | | | |
| 181 | 0 | | JarUtils.createJarArchive(tempDir, f); |
| 182 | 0 | | FileUtils.rmdir(tempDir); |
| 183 | 0 | | logger.info("Creating new jar " + f); |
| 184 | 0 | | } |
| 185 | 0 | | else if (f.isDirectory()) |
| 186 | | | { |
| 187 | 0 | | instrumentClasses(f); |
| 188 | 0 | | findNestedArchives(f); |
| 189 | | | } |
| 190 | 0 | | } |
| 191 | | | |
| 192 | | | |
| 193 | | | @param |
| 194 | | | |
| 195 | | | private void instrumentClasses (File f) |
| 196 | | | throws IOException |
| 197 | | | { |
| 198 | 0 | | final List classPath = new ArrayList(); |
| 199 | 0 | | findClassPath(f, classPath); |
| 200 | 0 | | final List classFiles = new ArrayList(); |
| 201 | 0 | | for (final Iterator pathEntrys = classPath.iterator(); |
| 202 | 0 | | pathEntrys.hasNext();) |
| 203 | | | { |
| 204 | 0 | | final File originDir = (File) pathEntrys.next(); |
| 205 | 0 | | classFiles.clear(); |
| 206 | 0 | | findClassFiles(originDir, originDir, classFiles); |
| 207 | | | |
| 208 | 0 | | final File tmpDir = FileUtils.createTempDir( |
| 209 | | | mBaseTempDir, "instrumented-classes"); |
| 210 | | | |
| 211 | 0 | | instrument(classFiles, originDir, tmpDir); |
| 212 | | | |
| 213 | | | |
| 214 | 0 | | FileUtils.copySlashStar(tmpDir, originDir); |
| 215 | | | |
| 216 | | | |
| 217 | | | |
| 218 | | | |
| 219 | | | |
| 220 | | | |
| 221 | | | |
| 222 | | | |
| 223 | | | |
| 224 | | | |
| 225 | | | |
| 226 | | | |
| 227 | | | |
| 228 | | | |
| 229 | | | |
| 230 | | | |
| 231 | 0 | | JarUtils.extractJarArchive(originDir, mConfig.getJcoverage()); |
| 232 | 0 | | JarUtils.extractJarArchive(originDir, mConfig.getLog4j()); |
| 233 | | | |
| 234 | 0 | | FileUtils.rmdir(tmpDir); |
| 235 | 0 | | } |
| 236 | 0 | | } |
| 237 | | | |
| 238 | | | |
| 239 | | | |
| 240 | | | |
| 241 | | | @param |
| 242 | | | @param |
| 243 | | | @param |
| 244 | | | @throws |
| 245 | | | |
| 246 | | | private void instrument ( |
| 247 | | | List classFiles, File classpath, File destinationDir) |
| 248 | | | throws IOException |
| 249 | | | { |
| 250 | | | |
| 251 | 0 | | final List args = new ArrayList(); |
| 252 | 0 | | args.add("-d"); |
| 253 | 0 | | args.add(destinationDir.getCanonicalPath()); |
| 254 | 0 | | args.add("-ignore"); |
| 255 | 0 | | args.add("org.apache.log4j.*"); |
| 256 | 0 | | args.add("-basedir"); |
| 257 | 0 | | args.add(classpath.getCanonicalPath()); |
| 258 | 0 | | args.addAll(classFiles); |
| 259 | | | |
| 260 | 0 | | logger.finest("Instrument with the following args: " + args); |
| 261 | | | |
| 262 | 0 | (15)(16) | Main.main((String[]) args.toArray(new String[0])); |
| 263 | 0 | | } |
| 264 | | | |
| 265 | | | |
| 266 | | | @param |
| 267 | | | @param |
| 268 | | | @param |
| 269 | | | |
| 270 | | | private void findClassFiles (File basePath, File file, List classFiles) |
| 271 | | | throws IOException |
| 272 | | | { |
| 273 | 0 | | if (file.isFile()) |
| 274 | | | { |
| 275 | 0 | | final String className = FileUtils.getRelativePath(basePath, file); |
| 276 | 0 | | if (className.endsWith(".class") && className.indexOf('$') == -1 |
| 277 | | | && className.matches(".*org.jcoderz.*")) |
| 278 | | | { |
| 279 | | | |
| 280 | | | |
| 281 | | | |
| 282 | | | |
| 283 | | | |
| 284 | | | |
| 285 | | | |
| 286 | | | |
| 287 | | | |
| 288 | | | |
| 289 | | | |
| 290 | | | |
| 291 | 0 | | classFiles.add(className); |
| 292 | | | } |
| 293 | 0 | | } |
| 294 | 0 | | else if (file.isDirectory()) |
| 295 | | | { |
| 296 | 0 | | final File [] files = file.listFiles(); |
| 297 | | | |
| 298 | 0 | | for (int i = 0; files != null && i < files.length; i++) |
| 299 | | | { |
| 300 | 0 | | findClassFiles(basePath, files[i], classFiles); |
| 301 | | | } |
| 302 | | | } |
| 303 | 0 | | } |
| 304 | | | |
| 305 | | | |
| 306 | | | @param |
| 307 | | | @param |
| 308 | | | |
| 309 | | | private void findClassPath (File f, List classPath) |
| 310 | | | { |
| 311 | 0 | | final File [] files = f.listFiles(new FileFilter() |
| 312 | 0 | (17) | { |
| 313 | | | public boolean accept (File pathname) |
| 314 | | | { |
| 315 | 0 | | return pathname.isDirectory(); |
| 316 | | | } |
| 317 | | | }); |
| 318 | | | |
| 319 | 0 | | boolean found = false; |
| 320 | 0 | | for (int i = 0; !found && i < files.length; i++) |
| 321 | | | { |
| 322 | 0 | (18) | if (files[i].getName().equals("com") |
| 323 | | | || files[i].getName().equals("junit")) |
| 324 | | | { |
| 325 | 0 | | classPath.add(files[i].getParentFile()); |
| 326 | 0 | | found = true; |
| 327 | | | } |
| 328 | | | } |
| 329 | 0 | | if (!found) |
| 330 | | | { |
| 331 | 0 | | for (int i = 0; i < files.length; i++) |
| 332 | | | { |
| 333 | 0 | | findClassPath(files[i], classPath); |
| 334 | | | } |
| 335 | | | } |
| 336 | 0 | | } |
| 337 | | | |
| 338 | | | |
| 339 | | | @param |
| 340 | | | |
| 341 | | | private void findNestedArchives (File file) |
| 342 | | | throws IOException |
| 343 | | | { |
| 344 | 0 | | if (file == null) |
| 345 | | | { |
| 346 | | | |
| 347 | | | } |
| 348 | 0 | | else if (file.isDirectory()) |
| 349 | | | { |
| 350 | 0 | | final File [] files = file.listFiles(); |
| 351 | 0 | | for (int i = 0; i < files.length; i++) |
| 352 | | | { |
| 353 | 0 | | findNestedArchives(files[i]); |
| 354 | | | } |
| 355 | 0 | | } |
| 356 | | | else |
| 357 | | | { |
| 358 | 0 | | if (isArchive(file)) |
| 359 | | | { |
| 360 | 0 | | instrument(file); |
| 361 | | | } |
| 362 | | | } |
| 363 | 0 | | } |
| 364 | | | |
| 365 | | | private boolean isArchive (File file) |
| 366 | | | throws IOException |
| 367 | | | { |
| 368 | 0 | | return file.getCanonicalPath().matches(".*\\.[jerw]ar"); |
| 369 | | | } |
| 370 | | | |
| 371 | 0 | | private static class ConfigurationParameters |
| 372 | | | { |
| 373 | 0 | | private File mArchive = null; |
| 374 | 0 | | private File mLog4j = null; |
| 375 | 0 | | private File mJcoverage = null; |
| 376 | | | |
| 377 | | | |
| 378 | | | |
| 379 | | | @return |
| 380 | | | |
| 381 | | | public final File getArchive () |
| 382 | | | { |
| 383 | 0 | | return mArchive; |
| 384 | | | } |
| 385 | | | |
| 386 | | | |
| 387 | | | <code></code> |
| 388 | | | @param |
| 389 | | | |
| 390 | | | public final void setArchive (File archive) |
| 391 | | | { |
| 392 | 0 | | mArchive = archive; |
| 393 | 0 | | } |
| 394 | | | |
| 395 | | | |
| 396 | | | |
| 397 | | | @return |
| 398 | | | |
| 399 | | | public final File getJcoverage () |
| 400 | | | { |
| 401 | 0 | | return mJcoverage; |
| 402 | | | } |
| 403 | | | |
| 404 | | | |
| 405 | | | <code></code> |
| 406 | | | @param |
| 407 | | | |
| 408 | | | public final void setJcoverage (File jcoverage) |
| 409 | | | { |
| 410 | 0 | | mJcoverage = jcoverage; |
| 411 | 0 | | } |
| 412 | | | |
| 413 | | | |
| 414 | | | |
| 415 | | | @return |
| 416 | | | |
| 417 | | | public final File getLog4j () |
| 418 | | | { |
| 419 | 0 | | return mLog4j; |
| 420 | | | } |
| 421 | | | |
| 422 | | | |
| 423 | | | <code></code> |
| 424 | | | @param |
| 425 | | | |
| 426 | | | public final void setLog4j (File log4j) |
| 427 | | | { |
| 428 | 0 | | mLog4j = log4j; |
| 429 | 0 | | } |
| 430 | | | |
| 431 | | | } |
| 432 | | | |
| 433 | | | } |