    001// Copyright 2014 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007// http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014package org.apache.tapestry5.jcache.internal;
    015
    016import org.apache.tapestry5.plastic.MethodInvocation;
    017import org.jsr107.ri.annotations.AbstractCacheResultInterceptor;
    018import org.jsr107.ri.annotations.CacheContextSource;
    019import org.jsr107.ri.annotations.InterceptorType;
    020
    021public class CacheResultMethodAdvice extends AbstractCacheResultInterceptor<MethodInvocation>
    022        implements CacheMethodAdvice
    023{
    024
    025    private final CacheContextSource<MethodInvocation> cacheContextSource;
    026
    027    /** Single constructor of this class. */
    028    public CacheResultMethodAdvice(CacheContextSource<MethodInvocation> cacheContextSource)
    029    {
    030        this.cacheContextSource = cacheContextSource;
    031    }
    032
    033    @Override
    034    public InterceptorType getInterceptorType()
    035    {
    036        return InterceptorType.CACHE_RESULT;
    037    }
    038
    039    @Override
    040    public void advise(MethodInvocation invocation)
    041    {
    042        try
    043        {
    044            invocation.setReturnValue(this.cacheResult(cacheContextSource, invocation));
    045        }
    046        catch (Throwable e)
    047        {
    048            throw new RuntimeException(e);
    049        }
    050    }
    051
    052    @Override
    053    protected Object proceed(MethodInvocation invocation) throws Throwable
    054    {
    055        invocation.proceed();
    056        if (invocation.didThrowCheckedException())
    057        {
    058            invocation.rethrow();
    059        }
    060        return invocation.getReturnValue();
    061    }
    062
    063}