    001// Copyright 2009, 2010, 2011 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.
    014
    015package org.apache.tapestry5.ioc.internal.services;
    016
    017import org.apache.tapestry5.commons.ObjectCreator;
    018import org.apache.tapestry5.ioc.MethodAdviceReceiver;
    019import org.apache.tapestry5.ioc.annotations.NotLazy;
    020import org.apache.tapestry5.ioc.annotations.PreventServiceDecoration;
    021import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    022import org.apache.tapestry5.ioc.services.LazyAdvisor;
    023import org.apache.tapestry5.ioc.services.ThunkCreator;
    024import org.apache.tapestry5.plastic.MethodAdvice;
    025import org.apache.tapestry5.plastic.MethodInvocation;
    026
    027import java.lang.reflect.Method;
    028
    029@PreventServiceDecoration
    030public class LazyAdvisorImpl implements LazyAdvisor
    031{
    032    private final ThunkCreator thunkCreator;
    033
    034    public LazyAdvisorImpl(ThunkCreator thunkCreator)
    035    {
    036        this.thunkCreator = thunkCreator;
    037    }
    038
    039    @Override
    040    public void addLazyMethodInvocationAdvice(MethodAdviceReceiver methodAdviceReceiver)
    041    {
    042        for (Method m : methodAdviceReceiver.getInterface().getMethods())
    043        {
    044            if (filter(m))
    045                addAdvice(m, methodAdviceReceiver);
    046        }
    047    }
    048
    049    private void addAdvice(Method method, MethodAdviceReceiver receiver)
    050    {
    051        final Class thunkType = method.getReturnType();
    052
    053        final String description = String.format("<%s Thunk for %s>", thunkType.getName(),
    054                InternalUtils.asString(method));
    055
    056        MethodAdvice advice = new MethodAdvice()
    057        {
    058            /**
    059             * When the method is invoked, we don't immediately proceed. Instead, we return a thunk instance
    060             * that defers its behavior to the lazily invoked invocation.
    061             */
    062            @Override
    063            public void advise(final MethodInvocation invocation)
    064            {
    065                ObjectCreator deferred = new ObjectCreator()
    066                {
    067                    @Override
    068                    public Object createObject()
    069                    {
    070                        invocation.proceed();
    071
    072                        return invocation.getReturnValue();
    073                    }
    074                };
    075
    076                ObjectCreator cachingObjectCreator = new CachingObjectCreator(deferred);
    077
    078                Object thunk = thunkCreator.createThunk(thunkType, cachingObjectCreator, description);
    079
    080                invocation.setReturnValue(thunk);
    081            }
    082        };
    083
    084        receiver.adviseMethod(method, advice);
    085    }
    086
    087    private boolean filter(Method method)
    088    {
    089        if (method.getAnnotation(NotLazy.class) != null)
    090            return false;
    091
    092        if (!method.getReturnType().isInterface())
    093            return false;
    094
    095        for (Class extype : method.getExceptionTypes())
    096        {
    097            if (!RuntimeException.class.isAssignableFrom(extype))
    098                return false;
    099        }
    100
    101        return true;
    102    }
    103}