KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > plugins > diskpersistence > HashDiskPersistenceListener


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
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.security.MessageDigest JavaDoc;
11 import java.security.NoSuchAlgorithmException JavaDoc;
12
13 /**
14  * Persists cache data to disk. Provides a hash of the standard key name as the file name.
15  *
16  * A configurable hash algorithm is used to create a digest of the cache key for the
17  * disk filename. This is to allow for more sane filenames for objects which dont generate
18  * friendly cache keys.
19  *
20  * @author <a HREF="mailto:jparrott@soe.sony.com">Jason Parrott</a>
21  */

22 public class HashDiskPersistenceListener extends AbstractDiskPersistenceListener {
23     public final static String JavaDoc HASH_ALGORITHM_KEY = "cache.persistence.disk.hash.algorithm";
24     public final static String JavaDoc DEFAULT_HASH_ALGORITHM = "MD5";
25     protected MessageDigest JavaDoc md = null;
26
27     /**
28      * Initializes the <tt>HashDiskPersistenceListener</tt>. Namely this involves only setting up the
29      * message digester to hash the key values.
30      * @see com.opensymphony.oscache.base.persistence.PersistenceListener#configure(com.opensymphony.oscache.base.Config)
31      */

32     public PersistenceListener configure(Config config) {
33         try {
34             if (config.getProperty(HashDiskPersistenceListener.HASH_ALGORITHM_KEY) != null) {
35                 try {
36                     md = MessageDigest.getInstance(config.getProperty(HashDiskPersistenceListener.HASH_ALGORITHM_KEY));
37                 } catch (NoSuchAlgorithmException JavaDoc e) {
38                     md = MessageDigest.getInstance(HashDiskPersistenceListener.DEFAULT_HASH_ALGORITHM);
39                 }
40             } else {
41                 md = MessageDigest.getInstance(HashDiskPersistenceListener.DEFAULT_HASH_ALGORITHM);
42             }
43         } catch (NoSuchAlgorithmException JavaDoc e) {
44             e.printStackTrace();
45             throw new RuntimeException JavaDoc("No hash algorithm available for disk persistence", e);
46         }
47
48         return super.configure(config);
49     }
50
51     /**
52      * Generates a file name for the given cache key. In this case the file name is attempted to be
53      * generated from the hash of the standard key name. Cache algorithm is configured via the
54      * <em>cache.persistence.disk.hash.algorithm</em> configuration variable.
55      * @param key cache entry key
56      * @return char[] file name
57      */

58     protected synchronized char[] getCacheFileName(String JavaDoc key) {
59         if ((key == null) || (key.length() == 0)) {
60             throw new IllegalArgumentException JavaDoc("Invalid key '" + key + "' specified to getCacheFile.");
61         }
62
63         byte[] digest = md.digest(key.getBytes());
64
65         return byteArrayToHexString(digest).toCharArray();
66     }
67
68     /**
69      * Nibble conversion. Thanks to our friends at:
70      * http://www.devx.com/tips/Tip/13540
71      * @param in the byte array to convert
72      * @return a java.lang.String based version of they byte array
73      */

74     static String JavaDoc byteArrayToHexString(byte[] in) {
75         if ((in == null) || (in.length <= 0)) {
76             return null;
77         }
78
79         StringBuffer JavaDoc out = new StringBuffer JavaDoc(in.length * 2);
80         
81         for (int i = 0; i < in.length; i++) {
82             byte ch = (byte) (in[i] & 0xF0); // Strip off high nibble
83
ch = (byte) (ch >>> 4);
84
85             // shift the bits down
86
ch = (byte) (ch & 0x0F);
87
88             // must do this is high order bit is on!
89
out.append(PSEUDO[(int) ch]); // convert the nibble to a String Character
90
ch = (byte) (in[i] & 0x0F); // Strip off low nibble
91
out.append(PSEUDO[(int) ch]); // convert the nibble to a String Character
92
i++;
93         }
94
95         return out.toString();
96     }
97     
98     static final String JavaDoc[] PSEUDO = {
99             "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
100             "E", "F"
101         };
102
103 }
104
Popular Tags