001
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
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 }