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.jcs3.jcache.cdi;
20  
21  import java.io.Serializable;
22  import javax.annotation.Priority;
23  import javax.cache.Cache;
24  import javax.cache.annotation.CacheKeyInvocationContext;
25  import javax.cache.annotation.CacheResolver;
26  import javax.cache.annotation.CacheResolverFactory;
27  import javax.cache.annotation.CacheResult;
28  import javax.cache.annotation.GeneratedCacheKey;
29  import javax.inject.Inject;
30  import javax.interceptor.AroundInvoke;
31  import javax.interceptor.Interceptor;
32  import javax.interceptor.InvocationContext;
33  
34  @CacheResult
35  @Interceptor
36  @Priority(/*LIBRARY_BEFORE*/1000)
37  public class CacheResultInterceptor implements Serializable
38  {
39      /**
40       *
41       */
42      private static final long serialVersionUID = 3763867761251365670L;
43      @Inject
44      private CDIJCacheHelper helper;
45  
46      @AroundInvoke
47      public Object cache(final InvocationContext ic) throws Throwable
48      {
49          final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic);
50  
51          final String cacheName = methodMeta.getCacheResultCacheName();
52  
53          final CacheResult cacheResult = methodMeta.getCacheResult();
54          final CacheKeyInvocationContext<CacheResult> context = new CacheKeyInvocationContextImpl<>(
55                  ic, cacheResult, cacheName, methodMeta);
56  
57          final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheResultResolverFactory();
58          final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
59          final Cache<Object, Object> cache = cacheResolver.resolveCache(context);
60  
61          final GeneratedCacheKey cacheKey = methodMeta.getCacheResultKeyGenerator().generateCacheKey(context);
62  
63          Cache<Object, Object> exceptionCache = null; // lazily created
64  
65          Object result;
66          if (!cacheResult.skipGet())
67          {
68              result = cache.get(cacheKey);
69              if (result != null)
70              {
71                  return result;
72              }
73  
74  
75              if (!cacheResult.exceptionCacheName().isEmpty())
76              {
77                  exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
78                  final Object exception = exceptionCache.get(cacheKey);
79                  if (exception != null)
80                  {
81                      throw Throwable.class.cast(exception);
82                  }
83              }
84          }
85  
86          try
87          {
88              result = ic.proceed();
89              if (result != null)
90              {
91                  cache.put(cacheKey, result);
92              }
93  
94              return result;
95          }
96          catch (final Throwable t)
97          {
98              if (helper.isIncluded(t.getClass(), cacheResult.cachedExceptions(), cacheResult.nonCachedExceptions()))
99              {
100                 if (exceptionCache == null)
101                 {
102                     exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
103                 }
104                 exceptionCache.put(cacheKey, t);
105             }
106             throw t;
107         }
108     }
109 }