View Javadoc

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  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                  // Exception is included
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             // don't happens
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             // don't happens
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             // don't happens
139             return null;
140         }
141 
142     }
143 
144 }