1 17 package org.apache.servicemix.components.util; 18 19 import java.util.Iterator ; 20 import java.util.Set ; 21 22 import javax.jbi.messaging.ExchangeStatus; 23 import javax.jbi.messaging.InOut; 24 import javax.jbi.messaging.MessageExchange; 25 import javax.jbi.messaging.MessagingException; 26 import javax.jbi.messaging.NormalizedMessage; 27 import javax.xml.namespace.QName ; 28 29 import org.apache.servicemix.components.util.TransformComponentSupport; 30 import org.apache.servicemix.jbi.FaultException; 31 32 43 public class ChainedComponent extends TransformComponentSupport { 44 45 private QName [] services = new QName [0]; 46 47 protected boolean transform(MessageExchange exchange, 48 NormalizedMessage in, 49 NormalizedMessage out) throws MessagingException { 50 NormalizedMessage curIn = in; 51 MessageExchange curExchange = exchange; 52 for (int i = 0; i < services.length; i++) { 53 InOut mexchange = this.getDeliveryChannel() 54 .createExchangeFactoryForService(services[i]) 55 .createInOutExchange(); 56 copyProperties(curExchange, mexchange); 57 curIn = invokeService(mexchange, curIn, services[i]); 58 curExchange = mexchange; 59 } 60 getMessageTransformer().transform(exchange, curIn, out); 61 copyProperties(curExchange, exchange); 62 return true; 63 } 64 65 74 private NormalizedMessage invokeService(InOut exchange, 75 NormalizedMessage in, 76 QName service) throws MessagingException { 77 NormalizedMessage msg = exchange.createMessage(); 78 getMessageTransformer().transform(exchange, in, msg); 79 exchange.setMessage(msg, "in"); 80 boolean result = this.getDeliveryChannel().sendSync(exchange); 81 if (result) { 82 if (exchange.getStatus() == ExchangeStatus.ERROR) { 83 exchange.setStatus(ExchangeStatus.DONE); 84 getDeliveryChannel().send(exchange); 85 if (exchange.getError() != null) { 86 throw new MessagingException("Received error", exchange.getError()); 87 } else if (exchange.getFault() != null) { 88 throw new FaultException("Received fault", exchange, exchange.getFault()); 89 } else { 90 throw new MessagingException("Received unknown error"); 91 } 92 } else { 93 NormalizedMessage out = exchange.getOutMessage(); 94 exchange.setStatus(ExchangeStatus.DONE); 95 getDeliveryChannel().send(exchange); 96 return out; 97 } 98 } 99 throw new MessagingException("Could not invoke service: " + service); 100 } 101 102 109 private void copyProperties(MessageExchange in, MessageExchange out) { 110 Set propertyNames = in.getPropertyNames(); 111 for (Iterator iter = propertyNames.iterator(); iter.hasNext();) { 112 String name = (String ) iter.next(); 113 out.setProperty(name, in.getProperty(name)); 114 } 115 } 116 117 124 public void setServices(QName [] services) { 125 this.services = services; 126 } 127 128 } 129 | Popular Tags |