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.CacheRemoveAll; 027import javax.cache.annotation.CacheResolver; 028import javax.cache.annotation.CacheResolverFactory; 029import javax.inject.Inject; 030import javax.interceptor.AroundInvoke; 031import javax.interceptor.Interceptor; 032import javax.interceptor.InvocationContext; 033 034@CacheRemoveAll 035@Interceptor 036@Priority(/*LIBRARY_BEFORE*/1000) 037public class CacheRemoveAllInterceptor implements Serializable 038{ 039 @Inject 040 private CDIJCacheHelper helper; 041 042 @AroundInvoke 043 public Object cache(final InvocationContext ic) throws Throwable 044 { 045 final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic); 046 047 final String cacheName = methodMeta.getCacheRemoveAllCacheName(); 048 049 final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheRemoveAllResolverFactory(); 050 final CacheKeyInvocationContext<CacheRemoveAll> context = new CacheKeyInvocationContextImpl<CacheRemoveAll>( 051 ic, methodMeta.getCacheRemoveAll(), cacheName, methodMeta); 052 final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context); 053 final Cache<Object, Object> cache = cacheResolver.resolveCache(context); 054 055 final boolean afterInvocation = methodMeta.isCachePutAfter(); 056 if (!afterInvocation) 057 { 058 cache.removeAll(); 059 } 060 061 final Object result; 062 try 063 { 064 result = ic.proceed(); 065 } 066 catch (final Throwable t) 067 { 068 if (afterInvocation) 069 { 070 if (helper.isIncluded(t.getClass(), methodMeta.getCacheRemoveAll().evictFor(), methodMeta.getCacheRemoveAll().noEvictFor())) 071 { 072 cache.removeAll(); 073 } 074 } 075 throw t; 076 } 077 078 if (afterInvocation) 079 { 080 cache.removeAll(); 081 } 082 083 return result; 084 } 085}