01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: InstrumentationUtils.java 3704 2007-03-20 10:46:55Z gbevin $
07: */
08: package com.uwyn.rife.tools;
09:
10: import java.io.ByteArrayInputStream;
11: import java.io.File;
12:
13: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
14:
15: public abstract class InstrumentationUtils {
16: public static final String PROPERTY_RIFE_INSTRUMENTATION_DUMP = "rife.instrumentation.dump";
17:
18: public static void dumpClassBytes(String type, String classname,
19: byte[] bytes) {
20: boolean write_to_disk = (System
21: .getProperty(PROPERTY_RIFE_INSTRUMENTATION_DUMP) != null);
22: if (write_to_disk) {
23: String user_home = System.getProperty("user.home");
24: String file_out_name = user_home + File.separatorChar
25: + "rife_instrumentation_" + type
26: + File.separatorChar
27: + classname.replace('.', File.separatorChar)
28: + ".class";
29: String dir_out_name = file_out_name.substring(0,
30: file_out_name.lastIndexOf(File.separatorChar));
31: // ensure that all the parent dirs are present
32: new File(dir_out_name).mkdirs();
33: File file_out = new File(file_out_name);
34: try {
35: System.out.println("Dumping " + type + " resource: "
36: + file_out.getAbsolutePath());
37: FileUtils.copy(new ByteArrayInputStream(bytes),
38: file_out);
39: } catch (FileUtilsErrorException e) {
40: e.printStackTrace();
41: }
42: }
43: }
44: }
|