01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.tapestry.ioc.internal.services;
16:
17: import java.util.List;
18:
19: import org.apache.commons.logging.Log;
20: import org.apache.tapestry.ioc.annotations.InjectService;
21: import org.apache.tapestry.ioc.services.ClassFactory;
22: import org.apache.tapestry.ioc.services.DefaultImplementationBuilder;
23: import org.apache.tapestry.ioc.services.PipelineBuilder;
24:
25: public class PipelineBuilderImpl implements PipelineBuilder {
26: private final ClassFactory _classFactory;
27:
28: private final DefaultImplementationBuilder _defaultImplementationBuilder;
29:
30: public PipelineBuilderImpl(@InjectService("ClassFactory")
31: ClassFactory classFactory,
32:
33: DefaultImplementationBuilder defaultImplementationBuilder) {
34: _classFactory = classFactory;
35: _defaultImplementationBuilder = defaultImplementationBuilder;
36: }
37:
38: public <S, F> S build(Log log, Class<S> serviceInterface,
39: Class<F> filterInterface, List<F> filters) {
40: S terminator = _defaultImplementationBuilder
41: .createDefaultImplementation(serviceInterface);
42:
43: return build(log, serviceInterface, filterInterface, filters,
44: terminator);
45: }
46:
47: public <S, F> S build(Log log, Class<S> serviceInterface,
48: Class<F> filterInterface, List<F> filters, S terminator) {
49: if (filters.isEmpty())
50: return terminator;
51:
52: BridgeBuilder<S, F> bb = new BridgeBuilder<S, F>(log,
53: serviceInterface, filterInterface, _classFactory);
54:
55: // The first bridge will point to the terminator.
56: // Like service decorators, we work deepest (last)
57: // to shallowest (first)
58:
59: S next = terminator;
60: int count = filters.size();
61:
62: for (int i = count - 1; i >= 0; i--) {
63: F filter = filters.get(i);
64:
65: next = bb.instantiateBridge(next, filter);
66: }
67:
68: return next;
69: }
70:
71: }
|