CacheInvocationContextImpl.java

  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. import java.lang.annotation.Annotation;
  21. import java.util.List;
  22. import java.util.Set;

  23. import javax.cache.annotation.CacheInvocationContext;
  24. import javax.cache.annotation.CacheInvocationParameter;
  25. import javax.interceptor.InvocationContext;

  26. public class CacheInvocationContextImpl<A extends Annotation> extends CacheMethodDetailsImpl<A> implements CacheInvocationContext<A>
  27. {
  28.     private static final Object[] EMPTY_ARGS = new Object[0];

  29.     private CacheInvocationParameter[] parameters;

  30.     public CacheInvocationContextImpl(final InvocationContext delegate, final A cacheAnnotation, final String cacheName,
  31.                                       final CDIJCacheHelper.MethodMeta meta)
  32.     {
  33.         super(delegate, cacheAnnotation, cacheName, meta);
  34.     }

  35.     @Override
  36.     public Object getTarget()
  37.     {
  38.         return delegate.getTarget();
  39.     }

  40.     @Override
  41.     public CacheInvocationParameter[] getAllParameters()
  42.     {
  43.         if (parameters == null)
  44.         {
  45.             parameters = doGetAllParameters(null);
  46.         }
  47.         return parameters;
  48.     }

  49.     @Override
  50.     public <T> T unwrap(final Class<T> cls)
  51.     {
  52.         if (cls.isAssignableFrom(getClass()))
  53.         {
  54.             return cls.cast(this);
  55.         }
  56.         throw new IllegalArgumentException(cls.getName());
  57.     }

  58.     protected CacheInvocationParameter[] doGetAllParameters(final Integer[] indexes)
  59.     {
  60.         final Object[] parameters = delegate.getParameters();
  61.         final Object[] args = parameters == null ? EMPTY_ARGS : parameters;
  62.         final Class<?>[] parameterTypes = meta.getParameterTypes();
  63.         final List<Set<Annotation>> parameterAnnotations = meta.getParameterAnnotations();

  64.         final CacheInvocationParameter[] parametersAsArray = new CacheInvocationParameter[indexes == null ? args.length : indexes.length];
  65.         if (indexes == null)
  66.         {
  67.             for (int i = 0; i < args.length; i++)
  68.             {
  69.                 parametersAsArray[i] = newCacheInvocationParameterImpl(parameterTypes[i], args[i], parameterAnnotations.get(i), i);
  70.             }
  71.         }
  72.         else
  73.         {
  74.             int outIdx = 0;
  75.             for (final Integer i : indexes) {
  76.                 parametersAsArray[outIdx] = newCacheInvocationParameterImpl(parameterTypes[i], args[i], parameterAnnotations.get(i), i);
  77.                 outIdx++;
  78.             }
  79.         }
  80.         return parametersAsArray;
  81.     }

  82.     private static CacheInvocationParameterImpl newCacheInvocationParameterImpl(final Class<?> type, final Object arg,
  83.                                                                          final Set<Annotation> annotations, final int i) {
  84.         return new CacheInvocationParameterImpl(type, arg, annotations, i);
  85.     }
  86. }