KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > proxy > ClassBytecodeRepository


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.proxy;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9 /**
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 JavaDoc nameToBytecodeMap = new HashMap JavaDoc();
22
23   public static void storeBytecode(final byte[] bytes, final String JavaDoc name) {
24     synchronized (soleInstance.nameToBytecodeMap) {
25       soleInstance.nameToBytecodeMap.put(name, bytes);
26       // File dir = new File(ClassBytecodeRepository.BYTECODE_DIR);
27
// dir.mkdirs();
28
// String fileName = (ClassBytecodeRepository.BYTECODE_DIR + File.separator + name).replace('.', '_');
29
// try {
30
// FileOutputStream os = new FileOutputStream(fileName);
31
// os.write(bytes);
32
// os.close();
33
// } catch (Exception e) {
34
// throw new WrappedRuntimeException(e);
35
// }
36
}
37   }
38
39   public static byte[] findBytecodeBy(final String JavaDoc name) {
40     synchronized (soleInstance.nameToBytecodeMap) {
41       Object JavaDoc bytesOrNull = soleInstance.nameToBytecodeMap.get(name);
42       if (bytesOrNull == null) {
43         return EMPTY_BYTE_ARRAY;
44       } else {
45         return (byte[]) bytesOrNull;
46       }
47
48       // String fileName = ClassBytecodeRepository.BYTECODE_DIR + File.separator + proxyName.replace('.', '_');
49
// File file = new File(fileName);
50
// byte[] bytes = new byte[] {};
51
// try {
52
// InputStream is = new FileInputStream(file);
53
// long length = file.length();
54
// if (length > Integer.MAX_VALUE) { throw new RuntimeException("file to large to fit into a byte array"); }
55
// bytes = new byte[(int) length];
56
// int offset = 0;
57
// int numRead = 0;
58
// while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
59
// offset += numRead;
60
// }
61
// if (offset < bytes.length) { throw new RuntimeException("could not completely read file " + file.getName()); }
62
// is.close();
63
// } catch (Exception ignore) {
64
// }
65
// return bytes;
66
}
67   }
68 }
69
Popular Tags