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 @Override
059 public String getCacheKey(String key) {
060 if ((_maxLength > -1) && (key.length() < _maxLength)) {
061 return key;
062 }
063
064 try {
065 _messageDigest.update(_charsetEncoder.encode(CharBuffer.wrap(key)));
066
067 byte[] bytes = _messageDigest.digest();
068
069 return encodeCacheKey(bytes);
070 }
071 catch (Exception e) {
072 _log.error(e, e);
073
074 return key;
075 }
076 }
077
078 @Override
079 public String getCacheKey(String[] keys) {
080 return getCacheKey(new StringBundler(keys));
081 }
082
083 @Override
084 public String getCacheKey(StringBundler sb) {
085 if ((_maxLength > -1) && (sb.length() < _maxLength)) {
086 return sb.toString();
087 }
088
089 try {
090 String[] array = sb.getStrings();
091
092 for (int i = 0; i < sb.index(); i++) {
093 String key = array[i];
094
095 _messageDigest.update(
096 _charsetEncoder.encode(CharBuffer.wrap(key)));
097 }
098
099 byte[] bytes = _messageDigest.digest();
100
101 return encodeCacheKey(bytes);
102 }
103 catch (Exception e) {
104 _log.error(e, e);
105
106 return sb.toString();
107 }
108 }
109
110 @Override
111 public boolean isCallingGetCacheKeyThreadSafe() {
112 return _CALLING_GET_CACHE_KEY_THREAD_SAFE;
113 }
114
115 public void setMaxLength(int maxLength) {
116 _maxLength = maxLength;
117 }
118
119 protected String encodeCacheKey(byte[] bytes) {
120 for (int i = 0; i < bytes.length; i++) {
121 int value = bytes[i] & 0xff;
122
123 _encodeBuffer[i * 2] = _HEX_CHARACTERS[value >> 4];
124 _encodeBuffer[i * 2 + 1] = _HEX_CHARACTERS[value & 0xf];
125 }
126
127 return new String(_encodeBuffer);
128 }
129
130 private static final String _ALGORITHM_MD5 = "MD5";
131
132 private static final boolean _CALLING_GET_CACHE_KEY_THREAD_SAFE = false;
133
134 private static final char[] _HEX_CHARACTERS = {
135 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
136 'e', 'f'
137 };
138
139 private static Log _log = LogFactoryUtil.getLog(
140 JavaMD5CacheKeyGenerator.class);
141
142 private CharsetEncoder _charsetEncoder;
143 private char[] _encodeBuffer = new char[32];
144 private int _maxLength = -1;
145 private MessageDigest _messageDigest;
146
147 }