001
014
015 package com.liferay.portal.cache.key;
016
017 import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.nio.charset.CharsetEncoderUtil;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023
024 import java.nio.CharBuffer;
025 import java.nio.charset.CharsetEncoder;
026
027 import java.security.MessageDigest;
028 import java.security.NoSuchAlgorithmException;
029
030
034 public class JavaMD5CacheKeyGenerator extends BaseCacheKeyGenerator {
035
036 public JavaMD5CacheKeyGenerator() throws NoSuchAlgorithmException {
037 this(-1);
038 }
039
040 public JavaMD5CacheKeyGenerator(int maxLength)
041 throws NoSuchAlgorithmException {
042
043 _maxLength = maxLength;
044 _messageDigest = MessageDigest.getInstance(_ALGORITHM_MD5);
045 _charsetEncoder = CharsetEncoderUtil.getCharsetEncoder(StringPool.UTF8);
046 }
047
048 @Override
049 public CacheKeyGenerator clone() {
050 try {
051 return new JavaMD5CacheKeyGenerator(_maxLength);
052 }
053 catch (NoSuchAlgorithmException nsae) {
054 throw new IllegalStateException(nsae.getMessage(), nsae);
055 }
056 }
057
058 public String getCacheKey(String key) {
059 if ((_maxLength > -1) && (key.length() < _maxLength)) {
060 return key;
061 }
062
063 try {
064 _messageDigest.update(_charsetEncoder.encode(CharBuffer.wrap(key)));
065
066 byte[] bytes = _messageDigest.digest();
067
068 return encodeCacheKey(bytes);
069 }
070 catch (Exception e) {
071 _log.error(e, e);
072
073 return key;
074 }
075 }
076
077 public String getCacheKey(String[] keys) {
078 return getCacheKey(new StringBundler(keys));
079 }
080
081 public String getCacheKey(StringBundler sb) {
082 if ((_maxLength > -1) && (sb.length() < _maxLength)) {
083 return sb.toString();
084 }
085
086 try {
087 for (int i = 0; i < sb.index(); i++) {
088 String key = sb.stringAt(i);
089
090 _messageDigest.update(
091 _charsetEncoder.encode(CharBuffer.wrap(key)));
092 }
093
094 byte[] bytes = _messageDigest.digest();
095
096 return encodeCacheKey(bytes);
097 }
098 catch (Exception e) {
099 _log.error(e, e);
100
101 return sb.toString();
102 }
103 }
104
105 public void setMaxLength(int maxLength) {
106 _maxLength = maxLength;
107 }
108
109 protected String encodeCacheKey(byte[] bytes) {
110 for (int i = 0; i < bytes.length; i++) {
111 int value = bytes[i] & 0xff;
112
113 _encodeBuffer[i * 2] = _HEX_CHARACTERS[value >> 4];
114 _encodeBuffer[i * 2 + 1] = _HEX_CHARACTERS[value & 0xf];
115 }
116
117 return new String(_encodeBuffer);
118 }
119
120 private static final String _ALGORITHM_MD5 = "MD5";
121
122 private static final char[] _HEX_CHARACTERS = {
123 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
124 'e', 'f'
125 };
126
127 private static Log _log = LogFactoryUtil.getLog(
128 JavaMD5CacheKeyGenerator.class);
129
130 private CharsetEncoder _charsetEncoder;
131 private char[] _encodeBuffer = new char[32];
132 private int _maxLength = -1;
133 private MessageDigest _messageDigest;
134
135 }