01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.aspectwerkz.proxy;
05:
06: import java.util.HashMap;
07: import java.util.Map;
08:
09: /**
10: * TODO document class
11: *
12: * @author Jonas Bonér
13: */
14: public class ClassBytecodeRepository {
15: // private static final String BYTECODE_DIR = "_pbc";
16: private static final byte[] EMPTY_BYTE_ARRAY = new byte[] {};
17:
18: private static final ClassBytecodeRepository soleInstance = new ClassBytecodeRepository();
19:
20: // DSO Shared Root
21: private final Map nameToBytecodeMap = new HashMap();
22:
23: public static void storeBytecode(final byte[] bytes,
24: final String name) {
25: synchronized (soleInstance.nameToBytecodeMap) {
26: soleInstance.nameToBytecodeMap.put(name, bytes);
27: // File dir = new File(ClassBytecodeRepository.BYTECODE_DIR);
28: // dir.mkdirs();
29: // String fileName = (ClassBytecodeRepository.BYTECODE_DIR + File.separator + name).replace('.', '_');
30: // try {
31: // FileOutputStream os = new FileOutputStream(fileName);
32: // os.write(bytes);
33: // os.close();
34: // } catch (Exception e) {
35: // throw new WrappedRuntimeException(e);
36: // }
37: }
38: }
39:
40: public static byte[] findBytecodeBy(final String name) {
41: synchronized (soleInstance.nameToBytecodeMap) {
42: Object bytesOrNull = soleInstance.nameToBytecodeMap
43: .get(name);
44: if (bytesOrNull == null) {
45: return EMPTY_BYTE_ARRAY;
46: } else {
47: return (byte[]) bytesOrNull;
48: }
49:
50: // String fileName = ClassBytecodeRepository.BYTECODE_DIR + File.separator + proxyName.replace('.', '_');
51: // File file = new File(fileName);
52: // byte[] bytes = new byte[] {};
53: // try {
54: // InputStream is = new FileInputStream(file);
55: // long length = file.length();
56: // if (length > Integer.MAX_VALUE) { throw new RuntimeException("file to large to fit into a byte array"); }
57: // bytes = new byte[(int) length];
58: // int offset = 0;
59: // int numRead = 0;
60: // while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
61: // offset += numRead;
62: // }
63: // if (offset < bytes.length) { throw new RuntimeException("could not completely read file " + file.getName()); }
64: // is.close();
65: // } catch (Exception ignore) {
66: // }
67: // return bytes;
68: }
69: }
70: }
|