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.module;
015
016import java.lang.annotation.Annotation;
017import java.lang.reflect.Method;
018
019import javax.cache.annotation.CacheKeyGenerator;
020import javax.cache.annotation.CachePut;
021import javax.cache.annotation.CacheRemove;
022import javax.cache.annotation.CacheRemoveAll;
023import javax.cache.annotation.CacheResolverFactory;
024import javax.cache.annotation.CacheResult;
025
026import org.apache.tapestry5.ioc.MethodAdviceReceiver;
027import org.apache.tapestry5.ioc.ObjectLocator;
028import org.apache.tapestry5.ioc.ServiceBinder;
029import org.apache.tapestry5.ioc.annotations.Match;
030import org.apache.tapestry5.jcache.internal.CacheLookupUtil;
031import org.apache.tapestry5.jcache.internal.CacheMethodAdvice;
032import org.apache.tapestry5.jcache.internal.CachePutMethodAdvice;
033import org.apache.tapestry5.jcache.internal.CacheRemoveAllMethodAdvice;
034import org.apache.tapestry5.jcache.internal.CacheRemoveMethodAdvice;
035import org.apache.tapestry5.jcache.internal.CacheResultMethodAdvice;
036import org.jsr107.ri.annotations.CacheContextSource;
037import org.jsr107.ri.annotations.DefaultCacheKeyGenerator;
038import org.jsr107.ri.annotations.DefaultCacheResolverFactory;
039
040/**
041 * Tapestry-IoC module that 
042 */
043public final class JCacheModule
044{
045
046    private JCacheModule()
047    {
048    }
049
050    /**
051     * Declares some services.
052     * 
053     * @param binder
054     *            a {@link ServiceBinder}.
055     */
056    public static void bind(ServiceBinder binder)
057    {
058        binder.bind(CacheKeyGenerator.class, DefaultCacheKeyGenerator.class);
059        binder.bind(CacheResolverFactory.class, DefaultCacheResolverFactory.class);
060        binder.bind(CacheContextSource.class, CacheLookupUtil.class);
061    }
062
063    /**
064     * Applies the advice to the services.
065     * 
066     * @param receiver
067     *            a {@link MethodAdviceReceiver}.
068     * @param objectLocator
069     *            an {@link ObjectLocator}.
070     */
071    @Match("*")
072    public static void advise(MethodAdviceReceiver receiver, ObjectLocator objectLocator)
073    {
074        advise(CachePut.class, objectLocator.autobuild(CachePutMethodAdvice.class), receiver);
075        advise(CacheRemoveAll.class, objectLocator.autobuild(CacheRemoveAllMethodAdvice.class),
076                receiver);
077        advise(CacheRemove.class, objectLocator.autobuild(CacheRemoveMethodAdvice.class), receiver);
078        advise(CacheResult.class, objectLocator.autobuild(CacheResultMethodAdvice.class), receiver);
079    }
080
081    private static void advise(Class<? extends Annotation> annotationClass,
082            CacheMethodAdvice advice, MethodAdviceReceiver methodAdviceReceiver)
083    {
084        if (methodAdviceReceiver.getClassAnnotationProvider().getAnnotation(annotationClass) != null)
085        {
086            methodAdviceReceiver.adviseAllMethods(advice);
087        }
088        else
089        {
090            for (Method method : methodAdviceReceiver.getInterface().getMethods())
091            {
092                if (methodAdviceReceiver.getMethodAnnotation(method, annotationClass) != null)
093                {
094                    methodAdviceReceiver.adviseMethod(method, advice);
095                }
096            }
097        }
098    }
099
100}