1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.jcs.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 @Inject
40 private CDIJCacheHelper helper;
41
42 @AroundInvoke
43 public Object cache(final InvocationContext ic) throws Throwable
44 {
45 final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic);
46
47 final String cacheName = methodMeta.getCacheRemoveAllCacheName();
48
49 final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheRemoveAllResolverFactory();
50 final CacheKeyInvocationContext<CacheRemoveAll> context = new CacheKeyInvocationContextImpl<CacheRemoveAll>(
51 ic, methodMeta.getCacheRemoveAll(), cacheName, methodMeta);
52 final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
53 final Cache<Object, Object> cache = cacheResolver.resolveCache(context);
54
55 final boolean afterInvocation = methodMeta.isCachePutAfter();
56 if (!afterInvocation)
57 {
58 cache.removeAll();
59 }
60
61 final Object result;
62 try
63 {
64 result = ic.proceed();
65 }
66 catch (final Throwable t)
67 {
68 if (afterInvocation)
69 {
70 if (helper.isIncluded(t.getClass(), methodMeta.getCacheRemoveAll().evictFor(), methodMeta.getCacheRemoveAll().noEvictFor()))
71 {
72 cache.removeAll();
73 }
74 }
75 throw t;
76 }
77
78 if (afterInvocation)
79 {
80 cache.removeAll();
81 }
82
83 return result;
84 }
85 }