1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| package com.opensymphony.oscache.plugins.diskpersistence; |
6 |
| |
7 |
| import com.opensymphony.oscache.base.Config; |
8 |
| import com.opensymphony.oscache.base.persistence.PersistenceListener; |
9 |
| |
10 |
| import java.io.File; |
11 |
| import java.security.MessageDigest; |
12 |
| import java.security.NoSuchAlgorithmException; |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class HashDiskPersistenceListener extends AbstractDiskPersistenceListener { |
24 |
| |
25 |
| private static final int DIR_LEVELS = 3; |
26 |
| |
27 |
| public final static String HASH_ALGORITHM_KEY = "cache.persistence.disk.hash.algorithm"; |
28 |
| public final static String DEFAULT_HASH_ALGORITHM = "MD5"; |
29 |
| protected MessageDigest md = null; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
59
| public PersistenceListener configure(Config config) {
|
37 |
59
| try {
|
38 |
59
| if (config.getProperty(HashDiskPersistenceListener.HASH_ALGORITHM_KEY) != null) {
|
39 |
30
| try {
|
40 |
30
| md = MessageDigest.getInstance(config.getProperty(HashDiskPersistenceListener.HASH_ALGORITHM_KEY));
|
41 |
| } catch (NoSuchAlgorithmException e) { |
42 |
0
| md = MessageDigest.getInstance(HashDiskPersistenceListener.DEFAULT_HASH_ALGORITHM);
|
43 |
| } |
44 |
| } else { |
45 |
29
| md = MessageDigest.getInstance(HashDiskPersistenceListener.DEFAULT_HASH_ALGORITHM);
|
46 |
| } |
47 |
| } catch (NoSuchAlgorithmException e) { |
48 |
0
| e.printStackTrace();
|
49 |
0
| throw new RuntimeException("No hash algorithm available for disk persistence", e);
|
50 |
| } |
51 |
| |
52 |
59
| return super.configure(config);
|
53 |
| } |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
482
| protected synchronized char[] getCacheFileName(String key) {
|
63 |
482
| if ((key == null) || (key.length() == 0)) {
|
64 |
0
| throw new IllegalArgumentException("Invalid key '" + key + "' specified to getCacheFile.");
|
65 |
| } |
66 |
| |
67 |
482
| String hexDigest = byteArrayToHexString(md.digest(key.getBytes()));
|
68 |
| |
69 |
| |
70 |
482
| StringBuffer filename = new StringBuffer(hexDigest.length() + 2 * DIR_LEVELS);
|
71 |
482
| for (int i=0; i < DIR_LEVELS; i++) {
|
72 |
1446
| filename.append(hexDigest.charAt(i)).append(File.separator);
|
73 |
| } |
74 |
482
| filename.append(hexDigest);
|
75 |
| |
76 |
482
| return filename.toString().toCharArray();
|
77 |
| } |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
522
| static String byteArrayToHexString(byte[] in) {
|
86 |
522
| if ((in == null) || (in.length <= 0)) {
|
87 |
0
| return null;
|
88 |
| } |
89 |
| |
90 |
522
| StringBuffer out = new StringBuffer(in.length * 2);
|
91 |
| |
92 |
522
| for (int i = 0; i < in.length; i++) {
|
93 |
7767
| byte ch = (byte) (in[i] & 0xF0);
|
94 |
7767
| ch = (byte) (ch >>> 4);
|
95 |
| |
96 |
| |
97 |
7767
| ch = (byte) (ch & 0x0F);
|
98 |
| |
99 |
| |
100 |
7767
| out.append(PSEUDO[(int) ch]);
|
101 |
7767
| ch = (byte) (in[i] & 0x0F);
|
102 |
7767
| out.append(PSEUDO[(int) ch]);
|
103 |
| } |
104 |
| |
105 |
522
| return out.toString();
|
106 |
| } |
107 |
| |
108 |
| static final String[] PSEUDO = { |
109 |
| "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", |
110 |
| "E", "F" |
111 |
| }; |
112 |
| |
113 |
| } |