001// Copyright 2009, 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;
016
017import java.util.List;
018
019import org.apache.tapestry5.commons.ObjectCreator;
020import org.apache.tapestry5.ioc.ServiceAdvisor;
021import org.apache.tapestry5.ioc.def.ServiceDef3;
022import org.apache.tapestry5.ioc.services.AspectDecorator;
023import org.apache.tapestry5.ioc.services.AspectInterceptorBuilder;
024
025/**
026 * Equivalent of {@link org.apache.tapestry5.ioc.internal.InterceptorStackBuilder}, but works using an
027 * {@link org.apache.tapestry5.ioc.services.AspectInterceptorBuilder} that receives advice from
028 * {@link org.apache.tapestry5.ioc.ServiceAdvisor}s.
029 * 
030 * @since 5.1.0.0
031 */
032public class AdvisorStackBuilder implements ObjectCreator
033{
034    private final ServiceDef3 serviceDef;
035
036    private final ObjectCreator delegate;
037
038    private final AspectDecorator aspectDecorator;
039
040    private final InternalRegistry registry;
041
042    /**
043     * @param serviceDef
044     *            the service that is ultimately being constructed
045     * @param delegate
046     *            responsible for creating the object to be decorated
047     * @param aspectDecorator
048     *            used to create the {@link org.apache.tapestry5.ioc.services.AspectInterceptorBuilder} passed to each
049     *            {@link org.apache.tapestry5.ioc.ServiceAdvisor}
050     * @param registry
051     */
052    public AdvisorStackBuilder(ServiceDef3 serviceDef, ObjectCreator delegate, AspectDecorator aspectDecorator,
053            InternalRegistry registry)
054    {
055        this.serviceDef = serviceDef;
056        this.delegate = delegate;
057        this.registry = registry;
058        this.aspectDecorator = aspectDecorator;
059    }
060
061    @Override
062    public Object createObject()
063    {
064        Object service = delegate.createObject();
065
066        List<ServiceAdvisor> advisors = registry.findAdvisorsForService(serviceDef);
067
068        if (advisors.isEmpty())
069            return service;
070
071        final AspectInterceptorBuilder builder = aspectDecorator.createBuilder(serviceDef.getServiceInterface(),
072                service, serviceDef, String.format("<AspectProxy for %s(%s)>", serviceDef.getServiceId(), serviceDef
073                        .getServiceInterface().getName()));
074
075        for (final ServiceAdvisor advisor : advisors)
076        {
077            registry.run("Invoking " + advisor, new Runnable()
078            {
079                @Override
080                public void run()
081                {
082                    advisor.advise(builder);
083                }
084            });
085        }
086
087        return builder.build();
088    }
089}