CachePutInterceptor.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.CachePut;
  25. import javax.cache.annotation.CacheResolver;
  26. import javax.cache.annotation.CacheResolverFactory;
  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. @CachePut
  33. @Interceptor
  34. @Priority(/*LIBRARY_BEFORE*/1000)
  35. public class CachePutInterceptor implements Serializable
  36. {
  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = -4327240193421847822L;
  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.getCachePutCacheName();

  48.         final CacheResolverFactory cacheResolverFactory = methodMeta.getCachePutResolverFactory();
  49.         final CacheKeyInvocationContext<CachePut> context = new CacheKeyInvocationContextImpl<>(
  50.                 ic, methodMeta.getCachePut(), cacheName, methodMeta);
  51.         final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
  52.         final Cache<Object, Object> cache = cacheResolver.resolveCache(context);

  53.         final GeneratedCacheKey cacheKey = methodMeta.getCachePutKeyGenerator().generateCacheKey(context);
  54.         final CachePut cachePut = methodMeta.getCachePut();
  55.         final boolean afterInvocation = methodMeta.isCachePutAfter();

  56.         if (!afterInvocation)
  57.         {
  58.             cache.put(cacheKey, context.getValueParameter());
  59.         }

  60.         final Object result;
  61.         try
  62.         {
  63.             result = ic.proceed();
  64.         }
  65.         catch (final Throwable t)
  66.         {
  67.             if (afterInvocation && helper.isIncluded(t.getClass(), cachePut.cacheFor(), cachePut.noCacheFor()))
  68.             {
  69.                 cache.put(cacheKey, context.getValueParameter());
  70.             }

  71.             throw t;
  72.         }

  73.         if (afterInvocation)
  74.         {
  75.             cache.put(cacheKey, context.getValueParameter());
  76.         }

  77.         return result;
  78.     }
  79. }