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.jcs3.jcache.cdi;
020
021import java.io.Serializable;
022import javax.annotation.Priority;
023import javax.cache.Cache;
024import javax.cache.annotation.CacheKeyInvocationContext;
025import javax.cache.annotation.CacheResolver;
026import javax.cache.annotation.CacheResolverFactory;
027import javax.cache.annotation.CacheResult;
028import javax.cache.annotation.GeneratedCacheKey;
029import javax.inject.Inject;
030import javax.interceptor.AroundInvoke;
031import javax.interceptor.Interceptor;
032import javax.interceptor.InvocationContext;
033
034@CacheResult
035@Interceptor
036@Priority(/*LIBRARY_BEFORE*/1000)
037public class CacheResultInterceptor implements Serializable
038{
039    /**
040     *
041     */
042    private static final long serialVersionUID = 3763867761251365670L;
043    @Inject
044    private CDIJCacheHelper helper;
045
046    @AroundInvoke
047    public Object cache(final InvocationContext ic) throws Throwable
048    {
049        final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic);
050
051        final String cacheName = methodMeta.getCacheResultCacheName();
052
053        final CacheResult cacheResult = methodMeta.getCacheResult();
054        final CacheKeyInvocationContext<CacheResult> context = new CacheKeyInvocationContextImpl<>(
055                ic, cacheResult, cacheName, methodMeta);
056
057        final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheResultResolverFactory();
058        final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
059        final Cache<Object, Object> cache = cacheResolver.resolveCache(context);
060
061        final GeneratedCacheKey cacheKey = methodMeta.getCacheResultKeyGenerator().generateCacheKey(context);
062
063        Cache<Object, Object> exceptionCache = null; // lazily created
064
065        Object result;
066        if (!cacheResult.skipGet())
067        {
068            result = cache.get(cacheKey);
069            if (result != null)
070            {
071                return result;
072            }
073
074
075            if (!cacheResult.exceptionCacheName().isEmpty())
076            {
077                exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
078                final Object exception = exceptionCache.get(cacheKey);
079                if (exception != null)
080                {
081                    throw Throwable.class.cast(exception);
082                }
083            }
084        }
085
086        try
087        {
088            result = ic.proceed();
089            if (result != null)
090            {
091                cache.put(cacheKey, result);
092            }
093
094            return result;
095        }
096        catch (final Throwable t)
097        {
098            if (helper.isIncluded(t.getClass(), cacheResult.cachedExceptions(), cacheResult.nonCachedExceptions()))
099            {
100                if (exceptionCache == null)
101                {
102                    exceptionCache = cacheResolverFactory.getExceptionCacheResolver(context).resolveCache(context);
103                }
104                exceptionCache.put(cacheKey, t);
105            }
106            throw t;
107        }
108    }
109}