CacheResultInterceptor.java

  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. import java.io.Serializable;
  21. import javax.annotation.Priority;
  22. import javax.cache.Cache;
  23. import javax.cache.annotation.CacheKeyInvocationContext;
  24. import javax.cache.annotation.CacheResolver;
  25. import javax.cache.annotation.CacheResolverFactory;
  26. import javax.cache.annotation.CacheResult;
  27. import javax.cache.annotation.GeneratedCacheKey;
  28. import javax.inject.Inject;
  29. import javax.interceptor.AroundInvoke;
  30. import javax.interceptor.Interceptor;
  31. import javax.interceptor.InvocationContext;

  32. @CacheResult
  33. @Interceptor
  34. @Priority(/*LIBRARY_BEFORE*/1000)
  35. public class CacheResultInterceptor implements Serializable
  36. {
  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = 3763867761251365670L;
  41.     @Inject
  42.     private CDIJCacheHelper helper;

  43.     @AroundInvoke
  44.     public Object cache(final InvocationContext ic) throws Throwable
  45.     {
  46.         final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic);

  47.         final String cacheName = methodMeta.getCacheResultCacheName();

  48.         final CacheResult cacheResult = methodMeta.getCacheResult();
  49.         final CacheKeyInvocationContext<CacheResult> context = new CacheKeyInvocationContextImpl<>(
  50.                 ic, cacheResult, cacheName, methodMeta);

  51.         final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheResultResolverFactory();
  52.         final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
  53.         final Cache<Object, Object> cache = cacheResolver.resolveCache(context);

  54.         final GeneratedCacheKey cacheKey = methodMeta.getCacheResultKeyGenerator().generateCacheKey(context);

  55.         Cache<Object, Object> exceptionCache = null; // lazily created

  56.         Object result;
  57.         if (!cacheResult.skipGet())
  58.         {
  59.             result = cache.get(cacheKey);
  60.             if (result != null)
  61.             {
  62.                 return result;
  63.             }


  64.             if (!cacheResult.exceptionCacheName().isEmpty())
  65.             {
  66.                 exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
  67.                 final Object exception = exceptionCache.get(cacheKey);
  68.                 if (exception != null)
  69.                 {
  70.                     throw Throwable.class.cast(exception);
  71.                 }
  72.             }
  73.         }

  74.         try
  75.         {
  76.             result = ic.proceed();
  77.             if (result != null)
  78.             {
  79.                 cache.put(cacheKey, result);
  80.             }

  81.             return result;
  82.         }
  83.         catch (final Throwable t)
  84.         {
  85.             if (helper.isIncluded(t.getClass(), cacheResult.cachedExceptions(), cacheResult.nonCachedExceptions()))
  86.             {
  87.                 if (exceptionCache == null)
  88.                 {
  89.                     exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
  90.                 }
  91.                 exceptionCache.put(cacheKey, t);
  92.             }
  93.             throw t;
  94.         }
  95.     }
  96. }