View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.jcs3.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;
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 (final Integer i : indexes) {
86                  parametersAsArray[outIdx] = newCacheInvocationParameterImpl(parameterTypes[i], args[i], parameterAnnotations.get(i), i);
87                  outIdx++;
88              }
89          }
90          return parametersAsArray;
91      }
92  
93      private static CacheInvocationParameterImpl newCacheInvocationParameterImpl(final Class<?> type, final Object arg,
94                                                                           final Set<Annotation> annotations, final int i) {
95          return new CacheInvocationParameterImpl(type, arg, annotations, i);
96      }
97  }