Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AfterBeforeInvocationInterceptor |
|
| 5.0;5 | ||||
AfterBeforeInvocationInterceptor$InterceptedAnnotationProxy |
|
| 5.0;5 |
1 | package org.apache.onami.cache; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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 | 0 | 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 | 0 | InterceptedAnnotationProxy<A> annotationProxy = new InterceptedAnnotationProxy<A>( context.getCacheAnnotation() ); |
40 | ||
41 | 0 | if ( !annotationProxy.afterInvocation() ) |
42 | { | |
43 | 0 | hitCache( context ); |
44 | } | |
45 | ||
46 | final Object invocationResult; | |
47 | try | |
48 | { | |
49 | 0 | invocationResult = invocation.proceed(); |
50 | } | |
51 | 0 | catch ( Throwable t ) |
52 | { | |
53 | 0 | if ( annotationProxy.afterInvocation() ) |
54 | { | |
55 | // Exception is included | |
56 | 0 | if ( include( t, annotationProxy.include(), annotationProxy.exclude(), false ) ) |
57 | { | |
58 | 0 | hitCache( context ); |
59 | } | |
60 | } | |
61 | ||
62 | 0 | throw t; |
63 | 0 | } |
64 | ||
65 | 0 | if ( annotationProxy.afterInvocation() ) |
66 | { | |
67 | 0 | hitCache( context ); |
68 | } | |
69 | ||
70 | 0 | return invocationResult; |
71 | } | |
72 | ||
73 | protected abstract void hitCache( CacheInvocationContext<A> context ); | |
74 | ||
75 | 0 | private static final class InterceptedAnnotationProxy<A extends Annotation> |
76 | { | |
77 | ||
78 | private final A interceptedAnnotation; | |
79 | ||
80 | public InterceptedAnnotationProxy( A interceptedAnnotation ) | |
81 | 0 | { |
82 | 0 | this.interceptedAnnotation = interceptedAnnotation; |
83 | 0 | } |
84 | ||
85 | public boolean afterInvocation() | |
86 | { | |
87 | 0 | if ( CachePut.class.isInstance( interceptedAnnotation ) ) |
88 | { | |
89 | 0 | return ( (CachePut) interceptedAnnotation).afterInvocation(); |
90 | } | |
91 | 0 | else if ( CacheRemoveAll.class.isInstance( interceptedAnnotation ) ) |
92 | { | |
93 | 0 | return ( (CacheRemoveAll) interceptedAnnotation).afterInvocation(); |
94 | } | |
95 | 0 | else if ( CacheRemoveEntry.class.isInstance( interceptedAnnotation ) ) |
96 | { | |
97 | 0 | return ( (CacheRemoveEntry) interceptedAnnotation).afterInvocation(); |
98 | } | |
99 | ||
100 | // don't happens | |
101 | 0 | return false; |
102 | } | |
103 | ||
104 | public Class<? extends Throwable>[] include() | |
105 | { | |
106 | 0 | if ( CachePut.class.isInstance( interceptedAnnotation ) ) |
107 | { | |
108 | 0 | return ( (CachePut) interceptedAnnotation).cacheFor(); |
109 | } | |
110 | 0 | else if ( CacheRemoveAll.class.isInstance( interceptedAnnotation ) ) |
111 | { | |
112 | 0 | return ( (CacheRemoveAll) interceptedAnnotation).evictFor(); |
113 | } | |
114 | 0 | else if ( CacheRemoveEntry.class.isInstance( interceptedAnnotation ) ) |
115 | { | |
116 | 0 | return ( (CacheRemoveEntry) interceptedAnnotation).evictFor(); |
117 | } | |
118 | ||
119 | // don't happens | |
120 | 0 | return null; |
121 | } | |
122 | ||
123 | public Class<? extends Throwable>[] exclude() | |
124 | { | |
125 | 0 | if ( CachePut.class.isInstance( interceptedAnnotation ) ) |
126 | { | |
127 | 0 | return ( (CachePut) interceptedAnnotation).noCacheFor(); |
128 | } | |
129 | 0 | else if ( CacheRemoveAll.class.isInstance( interceptedAnnotation ) ) |
130 | { | |
131 | 0 | return ( (CacheRemoveAll) interceptedAnnotation).noEvictFor(); |
132 | } | |
133 | 0 | else if ( CacheRemoveEntry.class.isInstance( interceptedAnnotation ) ) |
134 | { | |
135 | 0 | return ( (CacheRemoveEntry) interceptedAnnotation).noEvictFor(); |
136 | } | |
137 | ||
138 | // don't happens | |
139 | 0 | return null; |
140 | } | |
141 | ||
142 | } | |
143 | ||
144 | } |