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