    001// Copyright 2008, 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.services.PlasticProxyFactory;
    018import org.apache.tapestry5.ioc.AnnotationAccess;
    019import org.apache.tapestry5.ioc.annotations.PreventServiceDecoration;
    020import org.apache.tapestry5.ioc.internal.AnnotationAccessImpl;
    021import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    022import org.apache.tapestry5.ioc.services.AspectDecorator;
    023import org.apache.tapestry5.ioc.services.AspectInterceptorBuilder;
    024import org.apache.tapestry5.ioc.services.Builtin;
    025
    026import java.lang.reflect.Method;
    027
    028@PreventServiceDecoration
    029public class AspectDecoratorImpl implements AspectDecorator
    030{
    031    private final PlasticProxyFactory proxyFactory;
    032
    033    public AspectDecoratorImpl(@Builtin
    034                               PlasticProxyFactory proxyFactory)
    035    {
    036        this.proxyFactory = proxyFactory;
    037    }
    038
    039    @Override
    040    public <T> AspectInterceptorBuilder<T> createBuilder(Class<T> serviceInterface, final T delegate, String description)
    041    {
    042        return createBuilder(serviceInterface, delegate, new AnnotationAccessImpl(delegate.getClass()), description);
    043    }
    044
    045    @Override
    046    public <T> AspectInterceptorBuilder<T> createBuilder(final Class<T> serviceInterface, final T delegate,
    047                                                         AnnotationAccess annotationAccess, final String description)
    048    {
    049        assert serviceInterface != null;
    050        assert delegate != null;
    051        assert InternalUtils.isNonBlank(description);
    052
    053        // The inner class here prevents the needless creation of the AspectInterceptorBuilderImpl,
    054        // and the various Plastic related overhead, until there's some actual advice.
    055
    056        return new AbtractAspectInterceptorBuilder<T>(annotationAccess)
    057        {
    058            private AspectInterceptorBuilder<T> builder;
    059
    060            @Override
    061            public void adviseMethod(Method method, org.apache.tapestry5.plastic.MethodAdvice advice)
    062            {
    063                getBuilder().adviseMethod(method, advice);
    064            }
    065
    066            @Override
    067            public void adviseAllMethods(org.apache.tapestry5.plastic.MethodAdvice advice)
    068            {
    069                getBuilder().adviseAllMethods(advice);
    070            }
    071
    072            @Override
    073            public Class getInterface()
    074            {
    075                return serviceInterface;
    076            }
    077
    078            @Override
    079            public T build()
    080            {
    081                return builder == null ? delegate : builder.build();
    082            }
    083
    084            private AspectInterceptorBuilder<T> getBuilder()
    085            {
    086                if (builder == null)
    087                    builder = new AspectInterceptorBuilderImpl<T>(annotationAccess, proxyFactory, serviceInterface,
    088                            delegate, description);
    089
    090                return builder;
    091            }
    092        };
    093    }
    094}