KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > resolver > EndpointResolverSupport


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

17 package org.apache.servicemix.jbi.resolver;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.component.ComponentContext;
21 import javax.jbi.messaging.MessageExchange;
22 import javax.jbi.servicedesc.ServiceEndpoint;
23
24 /**
25  * A useful base class for {@link EndpointResolver} implementations.
26  *
27  * @version $Revision: 426415 $
28  */

29 public abstract class EndpointResolverSupport implements EndpointResolver {
30     private EndpointChooser chooser;
31     private boolean failIfUnavailable = true;
32
33     public ServiceEndpoint resolveEndpoint(ComponentContext context, MessageExchange exchange, EndpointFilter filter) throws JBIException {
34         ServiceEndpoint[] endpoints = resolveAvailableEndpoints(context, exchange);
35         if (endpoints.length > 0) {
36             endpoints = filterEndpoints(endpoints, exchange, filter);
37         }
38         if (endpoints.length == 0) {
39             if (failIfUnavailable) {
40                 throw createServiceUnavailableException();
41             }
42             else {
43                 return null;
44             }
45         }
46         if (endpoints.length == 1) {
47             return endpoints[0];
48         }
49         return getChooser().chooseEndpoint(endpoints, context, exchange);
50     }
51
52     public boolean isFailIfUnavailable() {
53         return failIfUnavailable;
54     }
55
56     public void setFailIfUnavailable(boolean failIfUnavailable) {
57         this.failIfUnavailable = failIfUnavailable;
58     }
59
60     public EndpointChooser getChooser() {
61         if (chooser == null) {
62             chooser = new FirstChoicePolicy();
63         }
64         return chooser;
65     }
66
67     public void setChooser(EndpointChooser chooser) {
68         this.chooser = chooser;
69     }
70
71     protected abstract JBIException createServiceUnavailableException();
72
73     protected ServiceEndpoint[] filterEndpoints(ServiceEndpoint[] endpoints, MessageExchange exchange, EndpointFilter filter) {
74         int matches = 0;
75         for (int i = 0; i < endpoints.length; i++) {
76             ServiceEndpoint endpoint = endpoints[i];
77             if (filter.evaluate(endpoint, exchange)) {
78                 matches++;
79             }
80             else {
81                 endpoints[i] = null;
82             }
83         }
84         if (matches == endpoints.length) {
85             return endpoints;
86         }
87         else {
88             ServiceEndpoint[] answer = new ServiceEndpoint[matches];
89             for (int i = 0, j = 0; i < endpoints.length; i++) {
90                 ServiceEndpoint endpoint = endpoints[i];
91                 if (endpoint != null) {
92                     answer[j++] = endpoints[i];
93                 }
94             }
95             return answer;
96         }
97     }
98 }
99
Popular Tags