001    // Copyright 2005 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.hivemind.lib.chain;
016    
017    import java.util.ArrayList;
018    import java.util.Iterator;
019    import java.util.List;
020    
021    import org.apache.hivemind.ServiceImplementationFactory;
022    import org.apache.hivemind.ServiceImplementationFactoryParameters;
023    import org.apache.hivemind.order.Orderer;
024    
025    /**
026     * Builds a service implementation using {@link org.apache.hivemind.lib.chain.ChainBuilder}.
027     * 
028     * @author Howard M. Lewis Ship
029     * @since 1.1
030     */
031    public class ChainFactory implements ServiceImplementationFactory
032    {
033        private ChainBuilder _chainBuilder;
034    
035        public Object createCoreServiceImplementation(
036                ServiceImplementationFactoryParameters factoryParameters)
037        {
038            List contributions = (List) factoryParameters.getFirstParameter();
039    
040            Orderer orderer = new Orderer(factoryParameters.getErrorLog(), "command");
041    
042            Iterator i = contributions.iterator();
043            while (i.hasNext())
044            {
045                ChainContribution cc = (ChainContribution) i.next();
046                orderer.add(cc, cc.getId(), cc.getAfter(), cc.getBefore());
047            }
048    
049            List ordered = orderer.getOrderedObjects();
050    
051            List commands = new ArrayList(ordered.size());
052    
053            i = ordered.iterator();
054            while (i.hasNext())
055            {
056                ChainContribution cc = (ChainContribution) i.next();
057    
058                // TODO: Validate that command for each ChainContribution implements the
059                // service interface.
060    
061                commands.add(cc.getCommand());
062            }
063    
064            String toString = "<ChainImplementation for " + factoryParameters.getServiceId() + "("
065                    + factoryParameters.getServiceInterface().getName() + ")>";
066    
067            return _chainBuilder.buildImplementation(
068                    factoryParameters.getServiceInterface(),
069                    commands,
070                    toString);
071        }
072    
073        public void setChainBuilder(ChainBuilder chainBuilder)
074        {
075            _chainBuilder = chainBuilder;
076        }
077    }