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; 022 023import javax.annotation.Priority; 024import javax.cache.Cache; 025import javax.cache.annotation.CacheKeyInvocationContext; 026import javax.cache.annotation.CacheRemove; 027import javax.cache.annotation.CacheResolver; 028import javax.cache.annotation.CacheResolverFactory; 029import javax.cache.annotation.GeneratedCacheKey; 030import javax.inject.Inject; 031import javax.interceptor.AroundInvoke; 032import javax.interceptor.Interceptor; 033import javax.interceptor.InvocationContext; 034 035@CacheRemove 036@Interceptor 037@Priority(/*LIBRARY_BEFORE*/1000) 038public class CacheRemoveInterceptor 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.getCacheRemoveCacheName(); 049 050 final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheRemoveResolverFactory(); 051 final CacheKeyInvocationContext<CacheRemove> context = new CacheKeyInvocationContextImpl<CacheRemove>( 052 ic, methodMeta.getCacheRemove(), cacheName, methodMeta); 053 final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context); 054 final Cache<Object, Object> cache = cacheResolver.resolveCache(context); 055 056 final GeneratedCacheKey cacheKey = methodMeta.getCacheRemoveKeyGenerator().generateCacheKey(context); 057 final CacheRemove cacheRemove = methodMeta.getCacheRemove(); 058 final boolean afterInvocation = methodMeta.isCacheRemoveAfter(); 059 060 if (!afterInvocation) 061 { 062 cache.remove(cacheKey); 063 } 064 065 final Object result; 066 try 067 { 068 result = ic.proceed(); 069 } 070 catch (final Throwable t) 071 { 072 if (afterInvocation) 073 { 074 if (helper.isIncluded(t.getClass(), cacheRemove.evictFor(), cacheRemove.noEvictFor())) 075 { 076 cache.remove(cacheKey); 077 } 078 } 079 080 throw t; 081 } 082 083 if (afterInvocation) 084 { 085 cache.remove(cacheKey); 086 } 087 088 return result; 089 } 090}