001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.cache.key;
016    
017    import com.liferay.portal.kernel.cache.Lifecycle;
018    import com.liferay.portal.kernel.cache.ThreadLocalCache;
019    import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    /**
025     * @author Michael C. Han
026     * @author Shuyang Zhou
027     */
028    public class CacheKeyGeneratorUtil {
029    
030            public static CacheKeyGenerator getCacheKeyGenerator() {
031                    return getCacheKeyGenerator(null);
032            }
033    
034            public static CacheKeyGenerator getCacheKeyGenerator(String cacheName) {
035                    ThreadLocalCache<CacheKeyGenerator> threadLocalCacheKeyGenerators =
036                            ThreadLocalCacheManager.getThreadLocalCache(
037                                    Lifecycle.ETERNAL, CacheKeyGeneratorUtil.class.getName());
038    
039                    CacheKeyGenerator cacheKeyGenerator = threadLocalCacheKeyGenerators.get(
040                            cacheName);
041    
042                    if (cacheKeyGenerator != null) {
043                            return cacheKeyGenerator;
044                    }
045    
046                    cacheKeyGenerator = _cacheKeyGenerators.get(cacheName);
047    
048                    if (cacheKeyGenerator == null) {
049                            cacheKeyGenerator = _defaultCacheKeyGenerator;
050                    }
051    
052                    cacheKeyGenerator = cacheKeyGenerator.clone();
053    
054                    threadLocalCacheKeyGenerators.put(cacheName, cacheKeyGenerator);
055    
056                    return cacheKeyGenerator;
057            }
058    
059            public void setCacheKeyGenerators(
060                    Map<String, CacheKeyGenerator> cacheKeyGenerators) {
061    
062                    _cacheKeyGenerators = cacheKeyGenerators;
063            }
064    
065            public void setDefaultCacheKeyGenerator(
066                    CacheKeyGenerator defaultCacheKeyGenerator) {
067    
068                    _defaultCacheKeyGenerator = defaultCacheKeyGenerator;
069            }
070    
071            private static Map<String, CacheKeyGenerator> _cacheKeyGenerators =
072                    new HashMap<String, CacheKeyGenerator>();
073            private static CacheKeyGenerator _defaultCacheKeyGenerator;
074    
075    }