001    /**
002     * Copyright (c) 2000-2013 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    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    /**
026     * @author Michael C. Han
027     * @author Shuyang Zhou
028     */
029    public class CacheKeyGeneratorUtil {
030    
031            public static CacheKeyGenerator getCacheKeyGenerator() {
032                    return getCacheKeyGenerator(null);
033            }
034    
035            public static CacheKeyGenerator getCacheKeyGenerator(String cacheName) {
036                    PortalRuntimePermission.checkGetBeanProperty(
037                            CacheKeyGeneratorUtil.class);
038    
039                    ThreadLocalCache<CacheKeyGenerator> threadLocalCacheKeyGenerators =
040                            ThreadLocalCacheManager.getThreadLocalCache(
041                                    Lifecycle.ETERNAL, CacheKeyGeneratorUtil.class.getName());
042    
043                    CacheKeyGenerator cacheKeyGenerator = threadLocalCacheKeyGenerators.get(
044                            cacheName);
045    
046                    if (cacheKeyGenerator != null) {
047                            return cacheKeyGenerator;
048                    }
049    
050                    cacheKeyGenerator = _cacheKeyGenerators.get(cacheName);
051    
052                    if (cacheKeyGenerator == null) {
053                            cacheKeyGenerator = _defaultCacheKeyGenerator;
054                    }
055    
056                    cacheKeyGenerator = cacheKeyGenerator.clone();
057    
058                    threadLocalCacheKeyGenerators.put(cacheName, cacheKeyGenerator);
059    
060                    return cacheKeyGenerator;
061            }
062    
063            public void setCacheKeyGenerators(
064                    Map<String, CacheKeyGenerator> cacheKeyGenerators) {
065    
066                    PortalRuntimePermission.checkSetBeanProperty(getClass());
067    
068                    _cacheKeyGenerators = cacheKeyGenerators;
069            }
070    
071            public void setDefaultCacheKeyGenerator(
072                    CacheKeyGenerator defaultCacheKeyGenerator) {
073    
074                    PortalRuntimePermission.checkSetBeanProperty(getClass());
075    
076                    _defaultCacheKeyGenerator = defaultCacheKeyGenerator;
077            }
078    
079            private static Map<String, CacheKeyGenerator> _cacheKeyGenerators =
080                    new HashMap<String, CacheKeyGenerator>();
081            private static CacheKeyGenerator _defaultCacheKeyGenerator;
082    
083    }