001    // Copyright 2006, 2007, 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    
015    package org.apache.tapestry5.ioc.internal.services;
016    
017    import java.util.List;
018    
019    import org.apache.tapestry5.ioc.services.Builtin;
020    import org.apache.tapestry5.ioc.services.DefaultImplementationBuilder;
021    import org.apache.tapestry5.ioc.services.PipelineBuilder;
022    import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
023    import org.slf4j.Logger;
024    
025    public class PipelineBuilderImpl implements PipelineBuilder
026    {
027    
028        private final PlasticProxyFactory proxyFactory;
029    
030        private final DefaultImplementationBuilder defaultImplementationBuilder;
031    
032        public PipelineBuilderImpl(@Builtin
033        PlasticProxyFactory proxyFactory,
034    
035        DefaultImplementationBuilder defaultImplementationBuilder)
036        {
037            this.proxyFactory = proxyFactory;
038            this.defaultImplementationBuilder = defaultImplementationBuilder;
039        }
040    
041        public <S, F> S build(Logger logger, Class<S> serviceInterface, Class<F> filterInterface, List<F> filters)
042        {
043            S terminator = defaultImplementationBuilder.createDefaultImplementation(serviceInterface);
044    
045            return build(logger, serviceInterface, filterInterface, filters, terminator);
046        }
047    
048        public <S, F> S build(Logger logger, Class<S> serviceInterface, Class<F> filterInterface, List<F> filters,
049                S terminator)
050        {
051            if (filters.isEmpty())
052                return terminator;
053    
054            BridgeBuilder<S, F> bb = new BridgeBuilder<S, F>(logger, serviceInterface, filterInterface, proxyFactory);
055    
056            // The first bridge will point to the terminator.
057            // Like service decorators, we work deepest (last)
058            // to shallowest (first)
059    
060            S next = terminator;
061            int count = filters.size();
062    
063            for (int i = count - 1; i >= 0; i--)
064            {
065                F filter = filters.get(i);
066    
067                next = bb.instantiateBridge(next, filter);
068            }
069    
070            return next;
071        }
072    
073    }