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.jcs3.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 /** 040 * 041 */ 042 private static final long serialVersionUID = -51261182430571546L; 043 @Inject 044 private CDIJCacheHelper helper; 045 046 @AroundInvoke 047 public Object cache(final InvocationContext ic) throws Throwable 048 { 049 final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic); 050 051 final String cacheName = methodMeta.getCacheRemoveAllCacheName(); 052 053 final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheRemoveAllResolverFactory(); 054 final CacheKeyInvocationContext<CacheRemoveAll> context = new CacheKeyInvocationContextImpl<>( 055 ic, methodMeta.getCacheRemoveAll(), cacheName, methodMeta); 056 final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context); 057 final Cache<Object, Object> cache = cacheResolver.resolveCache(context); 058 059 final boolean afterInvocation = methodMeta.isCachePutAfter(); 060 if (!afterInvocation) 061 { 062 cache.removeAll(); 063 } 064 065 final Object result; 066 try 067 { 068 result = ic.proceed(); 069 } 070 catch (final Throwable t) 071 { 072 if (afterInvocation && helper.isIncluded(t.getClass(), methodMeta.getCacheRemoveAll().evictFor(), methodMeta.getCacheRemoveAll().noEvictFor())) 073 { 074 cache.removeAll(); 075 } 076 throw t; 077 } 078 079 if (afterInvocation) 080 { 081 cache.removeAll(); 082 } 083 084 return result; 085 } 086}