001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.jcs.jcache.cdi; 020 021import java.lang.annotation.Annotation; 022import java.util.List; 023import java.util.Set; 024 025import javax.cache.annotation.CacheInvocationContext; 026import javax.cache.annotation.CacheInvocationParameter; 027import javax.interceptor.InvocationContext; 028 029public class CacheInvocationContextImpl<A extends Annotation> extends CacheMethodDetailsImpl<A> implements CacheInvocationContext<A> 030{ 031 private static final Object[] EMPTY_ARGS = new Object[0]; 032 033 private CacheInvocationParameter[] parameters = null; 034 035 public CacheInvocationContextImpl(final InvocationContext delegate, final A cacheAnnotation, final String cacheName, 036 final CDIJCacheHelper.MethodMeta meta) 037 { 038 super(delegate, cacheAnnotation, cacheName, meta); 039 } 040 041 @Override 042 public Object getTarget() 043 { 044 return delegate.getTarget(); 045 } 046 047 @Override 048 public CacheInvocationParameter[] getAllParameters() 049 { 050 if (parameters == null) 051 { 052 parameters = doGetAllParameters(null); 053 } 054 return parameters; 055 } 056 057 @Override 058 public <T> T unwrap(final Class<T> cls) 059 { 060 if (cls.isAssignableFrom(getClass())) 061 { 062 return cls.cast(this); 063 } 064 throw new IllegalArgumentException(cls.getName()); 065 } 066 067 protected CacheInvocationParameter[] doGetAllParameters(final Integer[] indexes) 068 { 069 final Object[] parameters = delegate.getParameters(); 070 final Object[] args = parameters == null ? EMPTY_ARGS : parameters; 071 final Class<?>[] parameterTypes = meta.getParameterTypes(); 072 final List<Set<Annotation>> parameterAnnotations = meta.getParameterAnnotations(); 073 074 final CacheInvocationParameter[] parametersAsArray = new CacheInvocationParameter[indexes == null ? args.length : indexes.length]; 075 if (indexes == null) 076 { 077 for (int i = 0; i < args.length; i++) 078 { 079 parametersAsArray[i] = newCacheInvocationParameterImpl(parameterTypes[i], args[i], parameterAnnotations.get(i), i); 080 } 081 } 082 else 083 { 084 int outIdx = 0; 085 for (int idx = 0; idx < indexes.length; idx++) 086 { 087 final int i = indexes[idx]; 088 parametersAsArray[outIdx] = newCacheInvocationParameterImpl(parameterTypes[i], args[i], parameterAnnotations.get(i), i); 089 outIdx++; 090 } 091 } 092 return parametersAsArray; 093 } 094 095 private CacheInvocationParameterImpl newCacheInvocationParameterImpl(final Class<?> type, final Object arg, 096 final Set<Annotation> annotations, final int i) { 097 return new CacheInvocationParameterImpl(type, arg, annotations, i); 098 } 099}