1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.jcs.jcache.cdi;
20
21 import java.lang.annotation.Annotation;
22 import java.util.List;
23 import java.util.Set;
24
25 import javax.cache.annotation.CacheInvocationContext;
26 import javax.cache.annotation.CacheInvocationParameter;
27 import javax.interceptor.InvocationContext;
28
29 public class CacheInvocationContextImpl<A extends Annotation> extends CacheMethodDetailsImpl<A> implements CacheInvocationContext<A>
30 {
31 private static final Object[] EMPTY_ARGS = new Object[0];
32
33 private CacheInvocationParameter[] parameters = null;
34
35 public CacheInvocationContextImpl(final InvocationContext delegate, final A cacheAnnotation, final String cacheName,
36 final CDIJCacheHelper.MethodMeta meta)
37 {
38 super(delegate, cacheAnnotation, cacheName, meta);
39 }
40
41 @Override
42 public Object getTarget()
43 {
44 return delegate.getTarget();
45 }
46
47 @Override
48 public CacheInvocationParameter[] getAllParameters()
49 {
50 if (parameters == null)
51 {
52 parameters = doGetAllParameters(null);
53 }
54 return parameters;
55 }
56
57 @Override
58 public <T> T unwrap(final Class<T> cls)
59 {
60 if (cls.isAssignableFrom(getClass()))
61 {
62 return cls.cast(this);
63 }
64 throw new IllegalArgumentException(cls.getName());
65 }
66
67 protected CacheInvocationParameter[] doGetAllParameters(final Integer[] indexes)
68 {
69 final Object[] parameters = delegate.getParameters();
70 final Object[] args = parameters == null ? EMPTY_ARGS : parameters;
71 final Class<?>[] parameterTypes = meta.getParameterTypes();
72 final List<Set<Annotation>> parameterAnnotations = meta.getParameterAnnotations();
73
74 final CacheInvocationParameter[] parametersAsArray = new CacheInvocationParameter[indexes == null ? args.length : indexes.length];
75 if (indexes == null)
76 {
77 for (int i = 0; i < args.length; i++)
78 {
79 parametersAsArray[i] = newCacheInvocationParameterImpl(parameterTypes[i], args[i], parameterAnnotations.get(i), i);
80 }
81 }
82 else
83 {
84 int outIdx = 0;
85 for (int idx = 0; idx < indexes.length; idx++)
86 {
87 final int i = indexes[idx];
88 parametersAsArray[outIdx] = newCacheInvocationParameterImpl(parameterTypes[i], args[i], parameterAnnotations.get(i), i);
89 outIdx++;
90 }
91 }
92 return parametersAsArray;
93 }
94
95 private CacheInvocationParameterImpl newCacheInvocationParameterImpl(final Class<?> type, final Object arg,
96 final Set<Annotation> annotations, final int i) {
97 return new CacheInvocationParameterImpl(type, arg, annotations, i);
98 }
99 }