1 25 26 package org.objectweb.petals.jbi.routing; 27 28 import javax.jbi.component.Component; 29 import javax.jbi.messaging.MessageExchange; 30 import javax.jbi.servicedesc.ServiceEndpoint; 31 import javax.xml.namespace.QName ; 32 33 import org.objectweb.petals.jbi.component.context.ComponentContextImpl; 34 import org.objectweb.petals.jbi.management.service.EndpointService; 35 import org.objectweb.petals.jbi.messaging.MessageExchangeImpl; 36 import org.objectweb.petals.jbi.registry.AbstractEndpoint; 37 import org.objectweb.petals.jbi.registry.InternalEndpoint; 38 import org.objectweb.petals.util.SystemUtil; 39 40 50 public class AddressResolver { 51 52 protected EndpointService endpointService; 53 54 public AddressResolver(EndpointService endpointService) { 55 this.endpointService = endpointService; 56 } 57 58 74 public void resolveAddress(ComponentContextImpl source, 75 MessageExchangeImpl exchange) throws RoutingException { 76 ServiceEndpoint givenEndpoint = exchange.getEndpoint(); 77 QName givenServiceName = exchange.getService(); 78 QName givenInterfaceName = exchange.getInterfaceName(); 79 80 ServiceEndpoint[] endpoints = null; 84 ServiceEndpoint targetedEndpoint = null; 85 86 if (givenEndpoint != null) { 87 if (givenEndpoint instanceof AbstractEndpoint 90 && ((AbstractEndpoint) givenEndpoint).getType() == AbstractEndpoint.EndpointType.INTERNAL) { 91 targetedEndpoint = givenEndpoint; 94 } else { 95 targetedEndpoint = resolveLinkedAddressForEndpoint( 96 givenEndpoint.getServiceName(), givenEndpoint 97 .getEndpointName()); 98 99 if (targetedEndpoint == null) { 102 targetedEndpoint = endpointService.getEndpoint( 103 givenEndpoint.getServiceName(), givenEndpoint 104 .getEndpointName()); 105 if (targetedEndpoint == null) { 107 throw new RoutingException("The specified endpoint (" 108 + givenEndpoint 109 + ") does not match a registered endpoint."); 110 } 111 } 112 } 113 } else if (givenServiceName != null) { 114 endpoints = endpointService 116 .getInternalEndpointsForService(givenServiceName); 117 118 targetedEndpoint = electEndpoint(endpoints, source, exchange); 121 122 if (targetedEndpoint == null) { 123 throw new RoutingException( 124 "No endpoint found for the specified service : " 125 + givenServiceName + "."); 126 } 127 } else if (givenInterfaceName != null) { 128 targetedEndpoint = resolveLinkedAddressForInterface(givenInterfaceName); 130 131 if (targetedEndpoint == null) { 132 endpoints = endpointService 134 .getInternalEndpointsForInterface(givenInterfaceName); 135 136 targetedEndpoint = electEndpoint(endpoints, source, exchange); 139 140 if (targetedEndpoint == null) { 141 throw new RoutingException( 142 "No endpoint found for the specified interface : " 143 + givenInterfaceName + "."); 144 } 145 } 146 } 147 148 exchange.setEndpoint(targetedEndpoint); 151 } 152 153 159 protected ServiceEndpoint resolveLinkedAddressForInterface( 160 QName interfaceName) { 161 ServiceEndpoint[] internalEndpoints = endpointService 162 .getInternalEndpointsForInterface(interfaceName); 163 if (internalEndpoints.length > 0) { 164 return internalEndpoints[0]; 165 } 166 return null; 167 } 168 169 177 protected ServiceEndpoint resolveLinkedAddressForEndpoint( 178 QName serviceName, String endpointName) throws RoutingException { 179 return endpointService.getEndpoint(serviceName, endpointName); 180 } 181 182 203 protected ServiceEndpoint electEndpoint(ServiceEndpoint[] endpoints, 204 ComponentContextImpl ctxSource, MessageExchange exchange) { 205 206 ServiceEndpoint result = null; 207 ServiceEndpoint inactiveEndpoint = null; 208 209 if (endpoints != null && endpoints.length > 0) { 210 Component consumer = ctxSource.getComponent(); 211 212 for (int i = 0; i < endpoints.length && result == null; i++) { 213 InternalEndpoint providerEP = (InternalEndpoint) endpoints[i]; 214 215 if (endpointService.isContainerStarted(providerEP)) { 217 if (SystemUtil.isExchangeValidation()) { 219 if (consumer.isExchangeWithProviderOkay(providerEP, 221 exchange) 222 && providerEP.getEndpointService() 223 .isExchangeWithConsumerOkayForComponent( 224 providerEP, exchange)) { 225 result = providerEP; 226 } 227 } else { 228 result = providerEP; 229 } 230 } 231 else { 234 inactiveEndpoint = providerEP; 235 } 236 } 237 if (result == null) { 240 result = inactiveEndpoint; 241 } 242 } 243 return result; 244 } 245 } 246 | Popular Tags |