1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.cache.key;
16  
17  import com.liferay.portal.kernel.cache.Lifecycle;
18  import com.liferay.portal.kernel.cache.ThreadLocalCache;
19  import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  /**
25   * <a href="CacheKeyGeneratorUtil.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Michael C. Han
28   * @author Shuyang Zhou
29   */
30  public class CacheKeyGeneratorUtil {
31  
32      public static CacheKeyGenerator getCacheKeyGenerator() {
33          return getCacheKeyGenerator(null);
34      }
35  
36      public static CacheKeyGenerator getCacheKeyGenerator(String cacheName) {
37          ThreadLocalCache<CacheKeyGenerator> threadLocalCacheKeyGenerators =
38              ThreadLocalCacheManager.getThreadLocalCache(
39                  Lifecycle.ETERNAL, CacheKeyGeneratorUtil.class.getName());
40  
41          CacheKeyGenerator cacheKeyGenerator = threadLocalCacheKeyGenerators.get(
42              cacheName);
43  
44          if (cacheKeyGenerator != null) {
45              return cacheKeyGenerator;
46          }
47  
48          cacheKeyGenerator = _cacheKeyGenerators.get(cacheName);
49  
50          if (cacheKeyGenerator == null) {
51              cacheKeyGenerator = _defaultCacheKeyGenerator;
52          }
53  
54          cacheKeyGenerator = cacheKeyGenerator.clone();
55  
56          threadLocalCacheKeyGenerators.put(cacheName, cacheKeyGenerator);
57  
58          return cacheKeyGenerator;
59      }
60  
61      public void setCacheKeyGenerators(
62          Map<String, CacheKeyGenerator> cacheKeyGenerators) {
63  
64          _cacheKeyGenerators = cacheKeyGenerators;
65      }
66  
67      public void setDefaultCacheKeyGenerator(
68          CacheKeyGenerator defaultCacheKeyGenerator) {
69  
70          _defaultCacheKeyGenerator = defaultCacheKeyGenerator;
71      }
72  
73      private static Map<String, CacheKeyGenerator> _cacheKeyGenerators =
74          new HashMap<String, CacheKeyGenerator>();
75      private static CacheKeyGenerator _defaultCacheKeyGenerator;
76  
77  }