1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.jcs3.jcache.cdi;
20
21 import java.io.Serializable;
22
23 import javax.annotation.Priority;
24 import javax.cache.Cache;
25 import javax.cache.annotation.CacheKeyInvocationContext;
26 import javax.cache.annotation.CacheRemoveAll;
27 import javax.cache.annotation.CacheResolver;
28 import javax.cache.annotation.CacheResolverFactory;
29 import javax.inject.Inject;
30 import javax.interceptor.AroundInvoke;
31 import javax.interceptor.Interceptor;
32 import javax.interceptor.InvocationContext;
33
34 @CacheRemoveAll
35 @Interceptor
36 @Priority(1000)
37 public class CacheRemoveAllInterceptor implements Serializable
38 {
39
40
41
42 private static final long serialVersionUID = -51261182430571546L;
43 @Inject
44 private CDIJCacheHelper helper;
45
46 @AroundInvoke
47 public Object cache(final InvocationContext ic) throws Throwable
48 {
49 final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic);
50
51 final String cacheName = methodMeta.getCacheRemoveAllCacheName();
52
53 final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheRemoveAllResolverFactory();
54 final CacheKeyInvocationContext<CacheRemoveAll> context = new CacheKeyInvocationContextImpl<>(
55 ic, methodMeta.getCacheRemoveAll(), cacheName, methodMeta);
56 final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
57 final Cache<Object, Object> cache = cacheResolver.resolveCache(context);
58
59 final boolean afterInvocation = methodMeta.isCachePutAfter();
60 if (!afterInvocation)
61 {
62 cache.removeAll();
63 }
64
65 final Object result;
66 try
67 {
68 result = ic.proceed();
69 }
70 catch (final Throwable t)
71 {
72 if (afterInvocation && helper.isIncluded(t.getClass(), methodMeta.getCacheRemoveAll().evictFor(), methodMeta.getCacheRemoveAll().noEvictFor()))
73 {
74 cache.removeAll();
75 }
76 throw t;
77 }
78
79 if (afterInvocation)
80 {
81 cache.removeAll();
82 }
83
84 return result;
85 }
86 }