KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > pipeline > PipelineAssembler


1 // Copyright 2004, 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.pipeline;
16
17 import java.util.List JavaDoc;
18
19 import org.apache.hivemind.ErrorLog;
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.impl.BaseLocatable;
22 import org.apache.hivemind.lib.DefaultImplementationBuilder;
23 import org.apache.hivemind.order.Orderer;
24 import org.apache.hivemind.service.ClassFactory;
25
26 /**
27  * Used by the {@link org.apache.hivemind.lib.pipeline.PipelineFactory} to assemble the pipeline.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class PipelineAssembler extends BaseLocatable
32 {
33     /** @since 1.1 */
34     private ErrorLog _errorLog;
35
36     private String JavaDoc _serviceId;
37
38     private Class JavaDoc _serviceInterface;
39
40     private Class JavaDoc _filterInterface;
41
42     private ClassFactory _classFactory;
43
44     private DefaultImplementationBuilder _defaultBuilder;
45
46     private Orderer _orderer;
47
48     private Object JavaDoc _terminator;
49
50     private Location _terminatorLocation;
51
52     /**
53      * @param errorLog
54      * used for reporting recoverable errors
55      * @param serviceInterface
56      * the main interface
57      * @param filterInterface
58      * the interface for filters
59      * @param classFactory
60      * for creating new classes
61      * @param defaultBuilder
62      * used to provide a placeholder terminator if no real terminator is supplied
63      * @param servceId
64      * of the service being assembled
65      */

66     public PipelineAssembler(ErrorLog errorLog, String JavaDoc serviceId, Class JavaDoc serviceInterface,
67             Class JavaDoc filterInterface, ClassFactory classFactory,
68             DefaultImplementationBuilder defaultBuilder)
69     {
70         _errorLog = errorLog;
71         _serviceId = serviceId;
72         _serviceInterface = serviceInterface;
73         _filterInterface = filterInterface;
74         _classFactory = classFactory;
75         _defaultBuilder = defaultBuilder;
76
77         _orderer = new Orderer(_errorLog, "filter");
78
79     }
80
81     public void addFilter(String JavaDoc name, String JavaDoc prereqs, String JavaDoc postreqs, Object JavaDoc filter,
82             Location location)
83     {
84         if (!checkInterface(_filterInterface, filter, location))
85             return;
86
87         FilterHolder holder = new FilterHolder(filter, location);
88
89         _orderer.add(holder, name, prereqs, postreqs);
90     }
91
92     public void setTerminator(Object JavaDoc terminator, Location terminatorLocation)
93     {
94         if (_terminator != null)
95         {
96             _errorLog.error(PipelineMessages.duplicateTerminator(
97                     terminator,
98                     _serviceId,
99                     _terminator,
100                     _terminatorLocation), terminatorLocation, null);
101             return;
102         }
103
104         if (!checkInterface(_serviceInterface, terminator, terminatorLocation))
105             return;
106
107         _terminator = terminator;
108         _terminatorLocation = terminatorLocation;
109     }
110
111     // For testing
112

113     Object JavaDoc getTerminator()
114     {
115         return _terminator;
116     }
117
118     private boolean checkInterface(Class JavaDoc interfaceType, Object JavaDoc instance, Location location)
119     {
120         if (interfaceType.isAssignableFrom(instance.getClass()))
121             return true;
122
123         _errorLog.error(
124                 PipelineMessages.incorrectInterface(instance, interfaceType, _serviceId),
125                 location,
126                 null);
127
128         return false;
129     }
130
131     /**
132      * Returns an object that implements the service interface, and integrates any filters for the
133      * pipeline with the
134      */

135     public Object JavaDoc createPipeline()
136     {
137         List JavaDoc filterHolders = _orderer.getOrderedObjects();
138         int count = filterHolders.size();
139
140         BridgeBuilder bb = (count == 0) ? null : new BridgeBuilder(_errorLog, _serviceId,
141                 _serviceInterface, _filterInterface, _classFactory);
142
143         Object JavaDoc next = _terminator != null ? _terminator : _defaultBuilder
144                 .buildDefaultImplementation(_serviceInterface);
145
146         // Like service interceptors, we work deepest (last) to shallowest (first).
147

148         for (int i = count - 1; i >= 0; i--)
149         {
150             FilterHolder h = (FilterHolder) filterHolders.get(i);
151             Object JavaDoc filter = h.getFilter();
152
153             next = bb.instantiateBridge(next, filter);
154         }
155
156         return next;
157     }
158 }
Popular Tags