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 java.io.Serializable; 022 023import javax.annotation.Priority; 024import javax.cache.Cache; 025import javax.cache.annotation.CacheKeyInvocationContext; 026import javax.cache.annotation.CachePut; 027import javax.cache.annotation.CacheResolver; 028import javax.cache.annotation.CacheResolverFactory; 029import javax.cache.annotation.GeneratedCacheKey; 030import javax.inject.Inject; 031import javax.interceptor.AroundInvoke; 032import javax.interceptor.Interceptor; 033import javax.interceptor.InvocationContext; 034 035@CachePut 036@Interceptor 037@Priority(/*LIBRARY_BEFORE*/1000) 038public class CachePutInterceptor implements Serializable 039{ 040 @Inject 041 private CDIJCacheHelper helper; 042 043 @AroundInvoke 044 public Object cache(final InvocationContext ic) throws Throwable 045 { 046 final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic); 047 048 final String cacheName = methodMeta.getCachePutCacheName(); 049 050 final CacheResolverFactory cacheResolverFactory = methodMeta.getCachePutResolverFactory(); 051 final CacheKeyInvocationContext<CachePut> context = new CacheKeyInvocationContextImpl<CachePut>( 052 ic, methodMeta.getCachePut(), cacheName, methodMeta); 053 final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context); 054 final Cache<Object, Object> cache = cacheResolver.resolveCache(context); 055 056 final GeneratedCacheKey cacheKey = methodMeta.getCachePutKeyGenerator().generateCacheKey(context); 057 final CachePut cachePut = methodMeta.getCachePut(); 058 final boolean afterInvocation = methodMeta.isCachePutAfter(); 059 060 if (!afterInvocation) 061 { 062 cache.put(cacheKey, context.getValueParameter()); 063 } 064 065 final Object result; 066 try 067 { 068 result = ic.proceed(); 069 } 070 catch (final Throwable t) 071 { 072 if (afterInvocation) 073 { 074 if (helper.isIncluded(t.getClass(), cachePut.cacheFor(), cachePut.noCacheFor())) 075 { 076 cache.put(cacheKey, context.getValueParameter()); 077 } 078 } 079 080 throw t; 081 } 082 083 if (afterInvocation) 084 { 085 cache.put(cacheKey, context.getValueParameter()); 086 } 087 088 return result; 089 } 090}