View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.jcs.jcache.cdi;
20  
21  import java.io.Serializable;
22  import java.lang.reflect.Method;
23  import javax.annotation.Priority;
24  import javax.cache.Cache;
25  import javax.cache.annotation.CacheKeyInvocationContext;
26  import javax.cache.annotation.CacheResolver;
27  import javax.cache.annotation.CacheResolverFactory;
28  import javax.cache.annotation.CacheResult;
29  import javax.cache.annotation.GeneratedCacheKey;
30  import javax.inject.Inject;
31  import javax.interceptor.AroundInvoke;
32  import javax.interceptor.Interceptor;
33  import javax.interceptor.InvocationContext;
34  
35  @CacheResult
36  @Interceptor
37  @Priority(/*LIBRARY_BEFORE*/1000)
38  public class CacheResultInterceptor implements Serializable
39  {
40      @Inject
41      private CDIJCacheHelper helper;
42  
43      @AroundInvoke
44      public Object cache(final InvocationContext ic) throws Throwable
45      {
46          final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic);
47  
48          final String cacheName = methodMeta.getCacheResultCacheName();
49  
50          final CacheResult cacheResult = methodMeta.getCacheResult();
51          final CacheKeyInvocationContext<CacheResult> context = new CacheKeyInvocationContextImpl<CacheResult>(
52                  ic, cacheResult, cacheName, methodMeta);
53  
54          final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheResultResolverFactory();
55          final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
56          final Cache<Object, Object> cache = cacheResolver.resolveCache(context);
57  
58          final GeneratedCacheKey cacheKey = methodMeta.getCacheResultKeyGenerator().generateCacheKey(context);
59  
60          Cache<Object, Object> exceptionCache = null; // lazily created
61  
62          Object result;
63          if (!cacheResult.skipGet())
64          {
65              result = cache.get(cacheKey);
66              if (result != null)
67              {
68                  return result;
69              }
70  
71  
72              if (!cacheResult.exceptionCacheName().isEmpty())
73              {
74                  exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
75                  final Object exception = exceptionCache.get(cacheKey);
76                  if (exception != null)
77                  {
78                      throw Throwable.class.cast(exception);
79                  }
80              }
81          }
82  
83          try
84          {
85              result = ic.proceed();
86              if (result != null)
87              {
88                  cache.put(cacheKey, result);
89              }
90  
91              return result;
92          }
93          catch (final Throwable t)
94          {
95              if (helper.isIncluded(t.getClass(), cacheResult.cachedExceptions(), cacheResult.nonCachedExceptions()))
96              {
97                  if (exceptionCache == null)
98                  {
99                      exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
100                 }
101                 exceptionCache.put(cacheKey, t);
102             }
103             throw t;
104         }
105     }
106 }