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.io.Serializable; 022import java.lang.reflect.Method; 023import javax.annotation.Priority; 024import javax.cache.Cache; 025import javax.cache.annotation.CacheKeyInvocationContext; 026import javax.cache.annotation.CacheResolver; 027import javax.cache.annotation.CacheResolverFactory; 028import javax.cache.annotation.CacheResult; 029import javax.cache.annotation.GeneratedCacheKey; 030import javax.inject.Inject; 031import javax.interceptor.AroundInvoke; 032import javax.interceptor.Interceptor; 033import javax.interceptor.InvocationContext; 034 035@CacheResult 036@Interceptor 037@Priority(/*LIBRARY_BEFORE*/1000) 038public class CacheResultInterceptor implements Serializable 039{ 040 @Inject 041 private CDIJCacheHelper helper; 042 043 @AroundInvoke 044 public Object cache(final InvocationContext ic) throws Throwable 045 { 046 final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic); 047 048 final String cacheName = methodMeta.getCacheResultCacheName(); 049 050 final CacheResult cacheResult = methodMeta.getCacheResult(); 051 final CacheKeyInvocationContext<CacheResult> context = new CacheKeyInvocationContextImpl<CacheResult>( 052 ic, cacheResult, cacheName, methodMeta); 053 054 final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheResultResolverFactory(); 055 final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context); 056 final Cache<Object, Object> cache = cacheResolver.resolveCache(context); 057 058 final GeneratedCacheKey cacheKey = methodMeta.getCacheResultKeyGenerator().generateCacheKey(context); 059 060 Cache<Object, Object> exceptionCache = null; // lazily created 061 062 Object result; 063 if (!cacheResult.skipGet()) 064 { 065 result = cache.get(cacheKey); 066 if (result != null) 067 { 068 return result; 069 } 070 071 072 if (!cacheResult.exceptionCacheName().isEmpty()) 073 { 074 exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context); 075 final Object exception = exceptionCache.get(cacheKey); 076 if (exception != null) 077 { 078 throw Throwable.class.cast(exception); 079 } 080 } 081 } 082 083 try 084 { 085 result = ic.proceed(); 086 if (result != null) 087 { 088 cache.put(cacheKey, result); 089 } 090 091 return result; 092 } 093 catch (final Throwable t) 094 { 095 if (helper.isIncluded(t.getClass(), cacheResult.cachedExceptions(), cacheResult.nonCachedExceptions())) 096 { 097 if (exceptionCache == null) 098 { 099 exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context); 100 } 101 exceptionCache.put(cacheKey, t); 102 } 103 throw t; 104 } 105 } 106}