001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.cache;
016    
017    import com.liferay.portal.kernel.cache.Lifecycle;
018    import com.liferay.portal.kernel.cache.ThreadLocalCachable;
019    import com.liferay.portal.kernel.cache.ThreadLocalCache;
020    import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
021    import com.liferay.portal.kernel.util.MethodTargetClassKey;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
025    
026    import java.lang.annotation.Annotation;
027    
028    import org.aopalliance.intercept.MethodInvocation;
029    
030    /**
031     * @author Shuyang Zhou
032     * @author Brian Wing Shun Chan
033     */
034    public class ThreadLocalCacheAdvice
035            extends AnnotationChainableMethodAdvice<ThreadLocalCachable> {
036    
037            public void afterReturning(
038                            MethodInvocation methodInvocation, Object result)
039                    throws Throwable {
040    
041                    MethodTargetClassKey methodTargetClassKey = buildMethodTargetClassKey(
042                            methodInvocation);
043    
044                    ThreadLocalCachable threadLocalCachable = findAnnotation(
045                            methodTargetClassKey);
046    
047                    if (threadLocalCachable == _nullThreadLocalCacheable) {
048                            return;
049                    }
050    
051                    ThreadLocalCache<Object> threadLocalCache =
052                            ThreadLocalCacheManager.getThreadLocalCache(
053                                    threadLocalCachable.scope(), methodTargetClassKey.toString());
054    
055                    String cacheKey = _buildCacheKey(methodInvocation.getArguments());
056    
057                    if (result == null) {
058                            threadLocalCache.put(cacheKey, nullResult);
059                    }
060                    else {
061                            threadLocalCache.put(cacheKey, result);
062                    }
063            }
064    
065            public Object before(MethodInvocation methodInvocation) throws Throwable {
066                    MethodTargetClassKey methodTargetClassKey = buildMethodTargetClassKey(
067                            methodInvocation);
068    
069                    ThreadLocalCachable threadLocalCachable = findAnnotation(
070                            methodTargetClassKey);
071    
072                    if (threadLocalCachable == _nullThreadLocalCacheable) {
073                            return null;
074                    }
075    
076                    ThreadLocalCache<?> threadLocalCache =
077                            ThreadLocalCacheManager.getThreadLocalCache(
078                                    threadLocalCachable.scope(), methodTargetClassKey.toString());
079    
080                    String cacheKey = _buildCacheKey(methodInvocation.getArguments());
081    
082                    Object value = threadLocalCache.get(cacheKey);
083    
084                    if (value == nullResult) {
085                            return null;
086                    }
087    
088                    return value;
089            }
090    
091            public ThreadLocalCachable getNullAnnotation() {
092                    return _nullThreadLocalCacheable;
093            }
094    
095            private String _buildCacheKey(Object[] arguments) {
096                    StringBundler sb = new StringBundler(arguments.length * 2 - 1);
097    
098                    for (int i = 0; i < arguments.length; i++) {
099                            sb.append(String.valueOf(arguments[i]));
100    
101                            if ((i + 1) < arguments.length) {
102                                    sb.append(StringPool.POUND);
103                            }
104                    }
105    
106                    return sb.toString();
107            }
108    
109            private static ThreadLocalCachable _nullThreadLocalCacheable =
110                    new ThreadLocalCachable() {
111    
112                            public Class<? extends Annotation> annotationType() {
113                                    return ThreadLocalCachable.class;
114                            }
115    
116                            public Lifecycle scope() {
117                                    return null;
118                            }
119    
120                    };
121    
122    }