1 package org.apache.onami.cache;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.annotation.Annotation;
23
24 import javax.cache.annotation.CacheInvocationContext;
25 import javax.cache.annotation.CachePut;
26 import javax.cache.annotation.CacheRemoveAll;
27 import javax.cache.annotation.CacheRemoveEntry;
28
29 import org.aopalliance.intercept.MethodInvocation;
30
31 abstract class AfterBeforeInvocationInterceptor<A extends Annotation>
32 extends CacheInterceptor<A>
33 {
34
35 @Override
36 protected final Object invoke( CacheInvocationContext<A> context, MethodInvocation invocation )
37 throws Throwable
38 {
39 InterceptedAnnotationProxy<A> annotationProxy = new InterceptedAnnotationProxy<A>( context.getCacheAnnotation() );
40
41 if ( !annotationProxy.afterInvocation() )
42 {
43 hitCache( context );
44 }
45
46 final Object invocationResult;
47 try
48 {
49 invocationResult = invocation.proceed();
50 }
51 catch ( Throwable t )
52 {
53 if ( annotationProxy.afterInvocation() )
54 {
55
56 if ( include( t, annotationProxy.include(), annotationProxy.exclude(), false ) )
57 {
58 hitCache( context );
59 }
60 }
61
62 throw t;
63 }
64
65 if ( annotationProxy.afterInvocation() )
66 {
67 hitCache( context );
68 }
69
70 return invocationResult;
71 }
72
73 protected abstract void hitCache( CacheInvocationContext<A> context );
74
75 private static final class InterceptedAnnotationProxy<A extends Annotation>
76 {
77
78 private final A interceptedAnnotation;
79
80 public InterceptedAnnotationProxy( A interceptedAnnotation )
81 {
82 this.interceptedAnnotation = interceptedAnnotation;
83 }
84
85 public boolean afterInvocation()
86 {
87 if ( CachePut.class.isInstance( interceptedAnnotation ) )
88 {
89 return ( (CachePut) interceptedAnnotation).afterInvocation();
90 }
91 else if ( CacheRemoveAll.class.isInstance( interceptedAnnotation ) )
92 {
93 return ( (CacheRemoveAll) interceptedAnnotation).afterInvocation();
94 }
95 else if ( CacheRemoveEntry.class.isInstance( interceptedAnnotation ) )
96 {
97 return ( (CacheRemoveEntry) interceptedAnnotation).afterInvocation();
98 }
99
100
101 return false;
102 }
103
104 public Class<? extends Throwable>[] include()
105 {
106 if ( CachePut.class.isInstance( interceptedAnnotation ) )
107 {
108 return ( (CachePut) interceptedAnnotation).cacheFor();
109 }
110 else if ( CacheRemoveAll.class.isInstance( interceptedAnnotation ) )
111 {
112 return ( (CacheRemoveAll) interceptedAnnotation).evictFor();
113 }
114 else if ( CacheRemoveEntry.class.isInstance( interceptedAnnotation ) )
115 {
116 return ( (CacheRemoveEntry) interceptedAnnotation).evictFor();
117 }
118
119
120 return null;
121 }
122
123 public Class<? extends Throwable>[] exclude()
124 {
125 if ( CachePut.class.isInstance( interceptedAnnotation ) )
126 {
127 return ( (CachePut) interceptedAnnotation).noCacheFor();
128 }
129 else if ( CacheRemoveAll.class.isInstance( interceptedAnnotation ) )
130 {
131 return ( (CacheRemoveAll) interceptedAnnotation).noEvictFor();
132 }
133 else if ( CacheRemoveEntry.class.isInstance( interceptedAnnotation ) )
134 {
135 return ( (CacheRemoveEntry) interceptedAnnotation).noEvictFor();
136 }
137
138
139 return null;
140 }
141
142 }
143
144 }