CacheRemoveAllInterceptor.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.CacheRemoveAll;
  25. import javax.cache.annotation.CacheResolver;
  26. import javax.cache.annotation.CacheResolverFactory;
  27. import javax.inject.Inject;
  28. import javax.interceptor.AroundInvoke;
  29. import javax.interceptor.Interceptor;
  30. import javax.interceptor.InvocationContext;

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

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

  46.         final String cacheName = methodMeta.getCacheRemoveAllCacheName();

  47.         final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheRemoveAllResolverFactory();
  48.         final CacheKeyInvocationContext<CacheRemoveAll> context = new CacheKeyInvocationContextImpl<>(
  49.                 ic, methodMeta.getCacheRemoveAll(), cacheName, methodMeta);
  50.         final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
  51.         final Cache<Object, Object> cache = cacheResolver.resolveCache(context);

  52.         final boolean afterInvocation = methodMeta.isCachePutAfter();
  53.         if (!afterInvocation)
  54.         {
  55.             cache.removeAll();
  56.         }

  57.         final Object result;
  58.         try
  59.         {
  60.             result = ic.proceed();
  61.         }
  62.         catch (final Throwable t)
  63.         {
  64.             if (afterInvocation && helper.isIncluded(t.getClass(), methodMeta.getCacheRemoveAll().evictFor(), methodMeta.getCacheRemoveAll().noEvictFor()))
  65.             {
  66.                 cache.removeAll();
  67.             }
  68.             throw t;
  69.         }

  70.         if (afterInvocation)
  71.         {
  72.             cache.removeAll();
  73.         }

  74.         return result;
  75.     }
  76. }