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.spring.aop;
016    
017    import java.lang.annotation.Annotation;
018    
019    import java.util.Map;
020    import java.util.concurrent.ConcurrentHashMap;
021    
022    import org.aopalliance.intercept.MethodInvocation;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class ServiceMethodAnnotationCache {
028    
029            public static <T> T get(
030                    MethodInvocation methodInvocation,
031                    Class<? extends Annotation> annotationType, T defaultValue) {
032    
033                    Annotation[] annotations = _annotations.get(methodInvocation);
034    
035                    if (annotations == _nullAnnotations) {
036                            return defaultValue;
037                    }
038    
039                    if (annotations == null) {
040                            return null;
041                    }
042    
043                    for (Annotation annotation : annotations) {
044                            if (annotation.annotationType() == annotationType) {
045                                    return (T)annotation;
046                            }
047                    }
048    
049                    return defaultValue;
050            }
051    
052            public static void put(
053                    MethodInvocation methodInvocation, Annotation[] annotations) {
054    
055                    if ((annotations == null) || (annotations.length == 0)) {
056                            annotations = _nullAnnotations;
057                    }
058    
059                    if (methodInvocation instanceof ServiceBeanMethodInvocation) {
060                            ServiceBeanMethodInvocation serviceBeanMethodInvocation =
061                                    (ServiceBeanMethodInvocation)methodInvocation;
062    
063                            methodInvocation = serviceBeanMethodInvocation.toCacheKeyModel();
064                    }
065    
066                    _annotations.put(methodInvocation, annotations);
067            }
068    
069            private static Map<MethodInvocation, Annotation[]> _annotations =
070                    new ConcurrentHashMap<MethodInvocation, Annotation[]>();
071            private static Annotation[] _nullAnnotations = new Annotation[0];
072    
073    }