001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.jcs.jcache.cdi;
020
021import javax.cache.Cache;
022import javax.cache.CacheManager;
023import javax.cache.Caching;
024import javax.cache.annotation.CacheMethodDetails;
025import javax.cache.annotation.CacheResolver;
026import javax.cache.annotation.CacheResolverFactory;
027import javax.cache.annotation.CacheResult;
028import javax.cache.configuration.MutableConfiguration;
029import javax.cache.spi.CachingProvider;
030import java.lang.annotation.Annotation;
031
032public class CacheResolverFactoryImpl implements CacheResolverFactory
033{
034    private final CacheManager cacheManager;
035    private final CachingProvider provider;
036
037    public CacheResolverFactoryImpl()
038    {
039        provider = Caching.getCachingProvider();
040        cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader());
041    }
042
043    @Override
044    public CacheResolver getCacheResolver(CacheMethodDetails<? extends Annotation> cacheMethodDetails)
045    {
046        return findCacheResolver(cacheMethodDetails.getCacheName());
047    }
048
049    @Override
050    public CacheResolver getExceptionCacheResolver(final CacheMethodDetails<CacheResult> cacheMethodDetails)
051    {
052        final String exceptionCacheName = cacheMethodDetails.getCacheAnnotation().exceptionCacheName();
053        if (exceptionCacheName == null || exceptionCacheName.isEmpty())
054        {
055            throw new IllegalArgumentException("CacheResult.exceptionCacheName() not specified");
056        }
057        return findCacheResolver(exceptionCacheName);
058    }
059
060    private CacheResolver findCacheResolver(String exceptionCacheName)
061    {
062        Cache<?, ?> cache = cacheManager.getCache(exceptionCacheName);
063        if (cache == null)
064        {
065            cache = createCache(exceptionCacheName);
066        }
067        return new CacheResolverImpl(cache);
068    }
069
070    private Cache<?, ?> createCache(final String exceptionCacheName)
071    {
072        cacheManager.createCache(exceptionCacheName, new MutableConfiguration<Object, Object>().setStoreByValue(false));
073        return cacheManager.getCache(exceptionCacheName);
074    }
075
076    public void release()
077    {
078        cacheManager.close();
079        provider.close();
080    }
081}