01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *
19: */
20: package org.safehaus.asyncweb.service.resolver;
21:
22: import java.util.ArrayList;
23: import java.util.Iterator;
24: import java.util.List;
25:
26: import org.safehaus.asyncweb.common.HttpRequest;
27: import org.slf4j.Logger;
28: import org.slf4j.LoggerFactory;
29:
30: /**
31: * A <code>ServiceResolver</code> which applies a list of child
32: * resolvers in turn until a match is made or all children have
33: * been tried
34: *
35: * @author irvingd
36: *
37: */
38: public class CompositeResolver implements ServiceResolver {
39:
40: private static final Logger LOG = LoggerFactory
41: .getLogger(CompositeResolver.class);
42:
43: private List<ServiceResolver> resolvers = new ArrayList<ServiceResolver>();
44:
45: /**
46: * Requests all child resolvers to resolve the request until either
47: * a resolution is found, or all child resolvers have been tried.
48: *
49: * @param request The request to resolve
50: * @return The service name, or <code>null</code> if no resolution could
51: * be found
52: */
53: public String resolveService(HttpRequest request) {
54: for (int i = 0, size = resolvers.size(); i < size; ++i) {
55: ServiceResolver resolver = resolvers.get(i);
56: String name = resolver.resolveService(request);
57: if (name != null) {
58: return name;
59: }
60: }
61: return null;
62: }
63:
64: /**
65: * Adds a resolver. Resolvers are applied in the order they are
66: * added
67: *
68: * @param resolver The resolver
69: */
70: public void addResolver(ServiceResolver resolver) {
71: resolvers.add(resolver);
72: LOG.info("Added resolver: " + resolver.getClass());
73: }
74:
75: /**
76: * Sets the resolvers employed by this <code>CompositeResolver</code>
77: *
78: * @param resolvers The resolvers
79: */
80: public void setResolvers(List resolvers) {
81: this .resolvers.clear();
82: // Find bad types early
83: for (Iterator iter = resolvers.iterator(); iter.hasNext();) {
84: ServiceResolver resolver = (ServiceResolver) iter.next();
85:
86: addResolver(resolver);
87: }
88: }
89:
90: }
|