Source Code Cross Referenced for HashDiskPersistenceListener.java in  » Cache » OSCache » com » opensymphony » oscache » plugins » diskpersistence » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Cache » OSCache » com.opensymphony.oscache.plugins.diskpersistence 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2007 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.oscache.plugins.diskpersistence;
006:
007:        import com.opensymphony.oscache.base.Config;
008:        import com.opensymphony.oscache.base.persistence.PersistenceListener;
009:
010:        import java.io.File;
011:        import java.security.MessageDigest;
012:        import java.security.NoSuchAlgorithmException;
013:
014:        /**
015:         * Persists cache data to disk. Provides a hash of the standard key name as the file name.
016:         *
017:         * A configurable hash algorithm is used to create a digest of the cache key for the
018:         * disk filename. This is to allow for more sane filenames for objects which dont generate
019:         * friendly cache keys.
020:         *
021:         * @author <a href="mailto:jparrott@soe.sony.com">Jason Parrott</a>
022:         */
023:        public class HashDiskPersistenceListener extends
024:                AbstractDiskPersistenceListener {
025:
026:            private static final int DIR_LEVELS = 3;
027:
028:            public final static String HASH_ALGORITHM_KEY = "cache.persistence.disk.hash.algorithm";
029:            public final static String DEFAULT_HASH_ALGORITHM = "MD5";
030:            protected MessageDigest md = null;
031:
032:            /**
033:             * Initializes the <tt>HashDiskPersistenceListener</tt>. Namely this involves only setting up the
034:             * message digester to hash the key values.
035:             * @see com.opensymphony.oscache.base.persistence.PersistenceListener#configure(com.opensymphony.oscache.base.Config)
036:             */
037:            public PersistenceListener configure(Config config) {
038:                try {
039:                    if (config
040:                            .getProperty(HashDiskPersistenceListener.HASH_ALGORITHM_KEY) != null) {
041:                        try {
042:                            md = MessageDigest
043:                                    .getInstance(config
044:                                            .getProperty(HashDiskPersistenceListener.HASH_ALGORITHM_KEY));
045:                        } catch (NoSuchAlgorithmException e) {
046:                            md = MessageDigest
047:                                    .getInstance(HashDiskPersistenceListener.DEFAULT_HASH_ALGORITHM);
048:                        }
049:                    } else {
050:                        md = MessageDigest
051:                                .getInstance(HashDiskPersistenceListener.DEFAULT_HASH_ALGORITHM);
052:                    }
053:                } catch (NoSuchAlgorithmException e) {
054:                    e.printStackTrace();
055:                    throw new RuntimeException(
056:                            "No hash algorithm available for disk persistence",
057:                            e);
058:                }
059:
060:                return super .configure(config);
061:            }
062:
063:            /**
064:             * Generates a file name for the given cache key. In this case the file name is attempted to be
065:             * generated from the hash of the standard key name. Cache algorithm is configured via the
066:             * <em>cache.persistence.disk.hash.algorithm</em> configuration variable.
067:             * @param key cache entry key
068:             * @return char[] file name
069:             */
070:            protected synchronized char[] getCacheFileName(String key) {
071:                if ((key == null) || (key.length() == 0)) {
072:                    throw new IllegalArgumentException("Invalid key '" + key
073:                            + "' specified to getCacheFile.");
074:                }
075:
076:                String hexDigest = byteArrayToHexString(md.digest(key
077:                        .getBytes()));
078:
079:                // CACHE-249: Performance improvement for large disk persistence usage
080:                StringBuffer filename = new StringBuffer(hexDigest.length() + 2
081:                        * DIR_LEVELS);
082:                for (int i = 0; i < DIR_LEVELS; i++) {
083:                    filename.append(hexDigest.charAt(i)).append(File.separator);
084:                }
085:                filename.append(hexDigest);
086:
087:                return filename.toString().toCharArray();
088:            }
089:
090:            /**
091:             * Nibble conversion. Thanks to our friends at:
092:             * http://www.devx.com/tips/Tip/13540
093:             * @param in the byte array to convert
094:             * @return a java.lang.String based version of they byte array
095:             */
096:            static String byteArrayToHexString(byte[] in) {
097:                if ((in == null) || (in.length <= 0)) {
098:                    return null;
099:                }
100:
101:                StringBuffer out = new StringBuffer(in.length * 2);
102:
103:                for (int i = 0; i < in.length; i++) {
104:                    byte ch = (byte) (in[i] & 0xF0); // Strip off high nibble
105:                    ch = (byte) (ch >>> 4);
106:
107:                    // shift the bits down
108:                    ch = (byte) (ch & 0x0F);
109:
110:                    //	 must do this is high order bit is on!
111:                    out.append(PSEUDO[(int) ch]); // convert the nibble to a String Character
112:                    ch = (byte) (in[i] & 0x0F); // Strip off low nibble 
113:                    out.append(PSEUDO[(int) ch]); // convert the nibble to a String Character
114:                }
115:
116:                return out.toString();
117:            }
118:
119:            static final String[] PSEUDO = { "0", "1", "2", "3", "4", "5", "6",
120:                    "7", "8", "9", "A", "B", "C", "D", "E", "F" };
121:
122:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.