CacheResolverFactoryImpl.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 javax.cache.Cache;
  21. import javax.cache.CacheManager;
  22. import javax.cache.Caching;
  23. import javax.cache.annotation.CacheMethodDetails;
  24. import javax.cache.annotation.CacheResolver;
  25. import javax.cache.annotation.CacheResolverFactory;
  26. import javax.cache.annotation.CacheResult;
  27. import javax.cache.configuration.MutableConfiguration;
  28. import javax.cache.spi.CachingProvider;
  29. import java.lang.annotation.Annotation;

  30. public class CacheResolverFactoryImpl implements CacheResolverFactory
  31. {
  32.     private final CacheManager cacheManager;
  33.     private final CachingProvider provider;

  34.     public CacheResolverFactoryImpl()
  35.     {
  36.         provider = Caching.getCachingProvider();
  37.         cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader());
  38.     }

  39.     @Override
  40.     public CacheResolver getCacheResolver(final CacheMethodDetails<? extends Annotation> cacheMethodDetails)
  41.     {
  42.         return findCacheResolver(cacheMethodDetails.getCacheName());
  43.     }

  44.     @Override
  45.     public CacheResolver getExceptionCacheResolver(final CacheMethodDetails<CacheResult> cacheMethodDetails)
  46.     {
  47.         final String exceptionCacheName = cacheMethodDetails.getCacheAnnotation().exceptionCacheName();
  48.         if (exceptionCacheName == null || exceptionCacheName.isEmpty())
  49.         {
  50.             throw new IllegalArgumentException("CacheResult.exceptionCacheName() not specified");
  51.         }
  52.         return findCacheResolver(exceptionCacheName);
  53.     }

  54.     private CacheResolver findCacheResolver(final String exceptionCacheName)
  55.     {
  56.         Cache<?, ?> cache = cacheManager.getCache(exceptionCacheName);
  57.         if (cache == null)
  58.         {
  59.             cache = createCache(exceptionCacheName);
  60.         }
  61.         return new CacheResolverImpl(cache);
  62.     }

  63.     private Cache<?, ?> createCache(final String exceptionCacheName)
  64.     {
  65.         cacheManager.createCache(exceptionCacheName, new MutableConfiguration<>().setStoreByValue(false));
  66.         return cacheManager.getCache(exceptionCacheName);
  67.     }

  68.     public void release()
  69.     {
  70.         cacheManager.close();
  71.         provider.close();
  72.     }
  73. }