KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > chain > ChainFactory


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.lib.chain;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.hivemind.ServiceImplementationFactory;
23 import org.apache.hivemind.ServiceImplementationFactoryParameters;
24 import org.apache.hivemind.order.Orderer;
25
26 /**
27  * Builds a service implementation using {@link org.apache.hivemind.lib.chain.ChainBuilder}.
28  *
29  * @author Howard M. Lewis Ship
30  * @since 1.1
31  */

32 public class ChainFactory implements ServiceImplementationFactory
33 {
34     private ChainBuilder _chainBuilder;
35
36     public Object JavaDoc createCoreServiceImplementation(
37             ServiceImplementationFactoryParameters factoryParameters)
38     {
39         Map JavaDoc contributions = (Map JavaDoc) factoryParameters.getFirstParameter();
40
41         Orderer orderer = new Orderer(factoryParameters.getErrorLog(), "command");
42
43         Iterator JavaDoc i = contributions.values().iterator();
44         while (i.hasNext())
45         {
46             ChainContribution cc = (ChainContribution) i.next();
47             orderer.add(cc, cc.getId(), cc.getAfter(), cc.getBefore());
48         }
49
50         List JavaDoc ordered = orderer.getOrderedObjects();
51
52         List JavaDoc commands = new ArrayList JavaDoc(ordered.size());
53
54         i = ordered.iterator();
55         while (i.hasNext())
56         {
57             ChainContribution cc = (ChainContribution) i.next();
58
59             // TODO: Validate that command for each ChainContribution implements the
60
// service interface.
61

62             commands.add(cc.getCommand());
63         }
64
65         String JavaDoc toString = "<ChainImplementation for " + factoryParameters.getServiceId() + "("
66                 + factoryParameters.getServiceInterface().getName() + ")>";
67
68         return _chainBuilder.buildImplementation(
69                 factoryParameters.getServiceInterface(),
70                 commands,
71                 toString);
72     }
73
74     public void setChainBuilder(ChainBuilder chainBuilder)
75     {
76         _chainBuilder = chainBuilder;
77     }
78 }
Popular Tags