1 25 26 package org.objectweb.petals.jbi.routing; 27 28 import javax.jbi.JBIException; 29 import javax.jbi.messaging.MessageExchange; 30 31 import org.objectweb.fractal.fraclet.annotation.FractalComponent; 32 import org.objectweb.fractal.fraclet.annotation.Interface; 33 import org.objectweb.fractal.fraclet.annotation.LifeCycle; 34 import org.objectweb.fractal.fraclet.annotation.LifeCycleType; 35 import org.objectweb.fractal.fraclet.annotation.Monolog; 36 import org.objectweb.fractal.fraclet.annotation.Provides; 37 import org.objectweb.fractal.fraclet.annotation.Requires; 38 import org.objectweb.petals.jbi.component.context.ComponentContextImpl; 39 import org.objectweb.petals.jbi.component.lifecycle.ComponentLifeCycle; 40 import org.objectweb.petals.jbi.management.service.EndpointService; 41 import org.objectweb.petals.jbi.management.service.LifeCycleManagerService; 42 import org.objectweb.petals.jbi.messaging.MessageExchangeImpl; 43 import org.objectweb.petals.jbi.registry.AbstractEndpoint; 44 import org.objectweb.petals.jbi.registry.ConsumerEndpoint; 45 import org.objectweb.petals.jbi.registry.InternalEndpoint; 46 import org.objectweb.petals.jbi.transport.TransportException; 47 import org.objectweb.petals.jbi.transport.Transporter; 48 import org.objectweb.petals.util.LoggingUtil; 49 import org.objectweb.util.monolog.api.Logger; 50 51 60 @FractalComponent 61 @Provides(interfaces=@Interface(name="service",signature=org.objectweb.petals.jbi.routing.Router.class)) 62 public class RouterImpl implements Router { 63 64 @Requires(name="endpoint-service",signature=org.objectweb.petals.jbi.management.service.EndpointService.class) 65 protected EndpointService endpointService; 66 67 70 protected LoggingUtil log; 71 72 @Monolog(name="logger") 73 protected Logger logger; 74 75 @Requires(name="lifecyclemanager",signature=org.objectweb.petals.jbi.management.service.LifeCycleManagerService.class) 76 protected LifeCycleManagerService manager; 77 78 @Requires(name="transporter",signature=org.objectweb.petals.jbi.transport.Transporter.class) 79 protected Transporter transporter; 80 81 protected AddressResolver addressResolver; 82 83 87 96 public void send(ComponentContextImpl source, MessageExchangeImpl exchange, 97 long timeOut) throws RoutingException { 98 log.call(); 99 100 if (MessageExchange.Role.CONSUMER.equals(exchange.getRole())) { 101 sendToProvider(source, exchange, timeOut); 102 } else { 103 sendToConsumer(source, exchange, timeOut); 104 } 105 } 106 107 111 @LifeCycle(on=LifeCycleType.START) 112 protected void start() { 113 log = new LoggingUtil(logger); 114 log.start(); 115 addressResolver = new AddressResolver(endpointService); 116 log.end(); 117 } 118 119 @LifeCycle(on=LifeCycleType.STOP) 120 protected void stop() { 121 log.start(); 122 123 logger = null; 124 125 log.end(); 126 } 127 128 138 protected void sendToConsumer(ComponentContextImpl source, 139 MessageExchangeImpl exchange, long timeOut) throws RoutingException { 140 log.start(); 141 142 try { 143 AbstractEndpoint consumerEndpoint = exchange.getConsumerEndpoint(); 144 145 String containerName = consumerEndpoint.getContainerName(); 146 147 exchange.setRole(MessageExchange.Role.CONSUMER); 148 149 transporter.send(exchange, containerName, timeOut); 150 } catch (TransportException e) { 151 exchange.setRole(MessageExchange.Role.PROVIDER); 153 154 throw new RoutingException(e); 155 } 156 157 log.end(); 158 } 159 160 169 protected void sendToProvider(ComponentContextImpl source, 170 MessageExchangeImpl exchange, long timeOut) throws RoutingException { 171 log.start(); 172 173 addressResolver.resolveAddress(source, exchange); 175 176 try { 177 AbstractEndpoint providerEndpoint = (AbstractEndpoint) exchange 178 .getEndpoint(); 179 180 String containerName = providerEndpoint.getContainerName(); 181 182 exchange.setRole(MessageExchange.Role.PROVIDER); 183 184 transporter.send(exchange, containerName, timeOut); 185 } catch (TransportException e) { 186 exchange.setRole(MessageExchange.Role.CONSUMER); 188 189 throw new RoutingException(e); 191 } 192 193 log.end(); 194 } 195 196 199 public void receive(MessageExchangeImpl exchange) throws RoutingException { 200 log.start(); 201 202 String componentname = null; 205 206 if (MessageExchange.Role.PROVIDER.equals(exchange.getRole())) { 207 componentname = ((InternalEndpoint) exchange.getEndpoint()) 208 .getComponentName(); 209 } else { 210 componentname = ((ConsumerEndpoint) exchange.getConsumerEndpoint()) 211 .getComponentName(); 212 } 213 214 ComponentLifeCycle comp = manager.getComponentByName(componentname); 216 217 try { 219 comp.getComponentContext().getDeliveryChannelImpl().push(exchange); 220 } catch (JBIException e) { 221 throw new RoutingException(e); 222 } 223 log.end(); 224 } 225 } 226 | Popular Tags |