001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.jbi.resolver;
018:
019: import javax.jbi.JBIException;
020: import javax.jbi.component.ComponentContext;
021: import javax.jbi.messaging.MessageExchange;
022: import javax.jbi.servicedesc.ServiceEndpoint;
023:
024: /**
025: * A useful base class for {@link EndpointResolver} implementations.
026: *
027: * @version $Revision: 564607 $
028: */
029: public abstract class EndpointResolverSupport implements
030: EndpointResolver {
031: private EndpointChooser chooser;
032: private boolean failIfUnavailable = true;
033:
034: public ServiceEndpoint resolveEndpoint(ComponentContext context,
035: MessageExchange exchange, EndpointFilter filter)
036: throws JBIException {
037: ServiceEndpoint[] endpoints = resolveAvailableEndpoints(
038: context, exchange);
039: if (endpoints == null) {
040: return null;
041: }
042: if (endpoints.length > 0) {
043: endpoints = filterEndpoints(endpoints, exchange, filter);
044: }
045: if (endpoints.length == 0) {
046: if (failIfUnavailable) {
047: throw createServiceUnavailableException();
048: } else {
049: return null;
050: }
051: }
052: if (endpoints.length == 1) {
053: return endpoints[0];
054: }
055: return getChooser()
056: .chooseEndpoint(endpoints, context, exchange);
057: }
058:
059: public boolean isFailIfUnavailable() {
060: return failIfUnavailable;
061: }
062:
063: public void setFailIfUnavailable(boolean failIfUnavailable) {
064: this .failIfUnavailable = failIfUnavailable;
065: }
066:
067: public EndpointChooser getChooser() {
068: if (chooser == null) {
069: chooser = new FirstChoicePolicy();
070: }
071: return chooser;
072: }
073:
074: public void setChooser(EndpointChooser chooser) {
075: this .chooser = chooser;
076: }
077:
078: protected abstract JBIException createServiceUnavailableException();
079:
080: protected ServiceEndpoint[] filterEndpoints(
081: ServiceEndpoint[] endpoints, MessageExchange exchange,
082: EndpointFilter filter) {
083: int matches = 0;
084: for (int i = 0; i < endpoints.length; i++) {
085: ServiceEndpoint endpoint = endpoints[i];
086: if (filter.evaluate(endpoint, exchange)) {
087: matches++;
088: } else {
089: endpoints[i] = null;
090: }
091: }
092: if (matches == endpoints.length) {
093: return endpoints;
094: } else {
095: ServiceEndpoint[] answer = new ServiceEndpoint[matches];
096: int j = 0;
097: for (int i = 0; i < endpoints.length; i++) {
098: ServiceEndpoint endpoint = endpoints[i];
099: if (endpoint != null) {
100: answer[j++] = endpoints[i];
101: }
102: }
103: return answer;
104: }
105: }
106: }
|