1 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 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 |