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; 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 /** 041 * 042 */ 043 private static final long serialVersionUID = -4327240193421847822L; 044 @Inject 045 private CDIJCacheHelper helper; 046 047 @AroundInvoke 048 public Object cache(final InvocationContext ic) throws Throwable 049 { 050 final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic); 051 052 final String cacheName = methodMeta.getCachePutCacheName(); 053 054 final CacheResolverFactory cacheResolverFactory = methodMeta.getCachePutResolverFactory(); 055 final CacheKeyInvocationContext<CachePut> context = new CacheKeyInvocationContextImpl<>( 056 ic, methodMeta.getCachePut(), cacheName, methodMeta); 057 final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context); 058 final Cache<Object, Object> cache = cacheResolver.resolveCache(context); 059 060 final GeneratedCacheKey cacheKey = methodMeta.getCachePutKeyGenerator().generateCacheKey(context); 061 final CachePut cachePut = methodMeta.getCachePut(); 062 final boolean afterInvocation = methodMeta.isCachePutAfter(); 063 064 if (!afterInvocation) 065 { 066 cache.put(cacheKey, context.getValueParameter()); 067 } 068 069 final Object result; 070 try 071 { 072 result = ic.proceed(); 073 } 074 catch (final Throwable t) 075 { 076 if (afterInvocation && helper.isIncluded(t.getClass(), cachePut.cacheFor(), cachePut.noCacheFor())) 077 { 078 cache.put(cacheKey, context.getValueParameter()); 079 } 080 081 throw t; 082 } 083 084 if (afterInvocation) 085 { 086 cache.put(cacheKey, context.getValueParameter()); 087 } 088 089 return result; 090 } 091}