1 17 package org.apache.servicemix.components.util; 18 19 import javax.jbi.messaging.ExchangeStatus; 20 import javax.jbi.messaging.InOnly; 21 import javax.jbi.messaging.MessageExchange; 22 import javax.jbi.messaging.MessagingException; 23 import javax.jbi.messaging.NormalizedMessage; 24 import javax.xml.namespace.QName ; 25 26 import org.apache.servicemix.JbiConstants; 27 import org.apache.servicemix.MessageExchangeListener; 28 29 34 public abstract class TransformComponentSupport extends ComponentSupport implements MessageExchangeListener { 35 36 private boolean copyProperties = true; 37 private boolean copyAttachments = true; 38 39 protected TransformComponentSupport() { 40 } 41 42 protected TransformComponentSupport(QName service, String endpoint) { 43 super(service, endpoint); 44 } 45 46 public void onMessageExchange(MessageExchange exchange) { 47 if (exchange.getStatus() == ExchangeStatus.DONE) { 49 return; 50 } else if (exchange.getStatus() == ExchangeStatus.ERROR) { 52 return; 53 } 54 try { 55 InOnly outExchange = null; 56 NormalizedMessage in = getInMessage(exchange); 57 NormalizedMessage out; 58 if (isInAndOut(exchange)) { 59 out = exchange.createMessage(); 60 } else { 61 outExchange = getExchangeFactory().createInOnlyExchange(); 62 out = outExchange.createMessage(); 63 } 64 boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC)); 65 copyPropertiesAndAttachments(exchange, in, out); 66 if (transform(exchange, in, out)) { 67 if (isInAndOut(exchange)) { 68 exchange.setMessage(out, "out"); 69 if (txSync) { 70 getDeliveryChannel().sendSync(exchange); 71 } else { 72 getDeliveryChannel().send(exchange); 73 } 74 } 75 else { 76 outExchange.setMessage(out, "in"); 77 if (txSync) { 78 getDeliveryChannel().sendSync(outExchange); 79 } else { 80 getDeliveryChannel().send(outExchange); 81 } 82 exchange.setStatus(ExchangeStatus.DONE); 83 getDeliveryChannel().send(exchange); 84 } 85 } else { 86 exchange.setStatus(ExchangeStatus.DONE); 87 getDeliveryChannel().send(exchange); 88 } 89 } 90 catch (Exception e) { 91 try { 92 fail(exchange, e); 93 } catch (Exception e2) { 94 logger.warn("Unable to handle error: " + e2, e2); 95 if (logger.isDebugEnabled()) { 96 logger.debug("Original error: " + e, e); 97 } 98 } 99 } 100 } 101 102 103 106 109 protected abstract boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception ; 110 111 112 public boolean isCopyProperties() { 113 return copyProperties; 114 } 115 116 117 public void setCopyProperties(boolean copyProperties) { 118 this.copyProperties = copyProperties; 119 if (getMessageTransformer() instanceof CopyTransformer) { 120 ((CopyTransformer) getMessageTransformer()).setCopyProperties(copyProperties); 121 } 122 } 123 124 125 public boolean isCopyAttachments() { 126 return copyAttachments; 127 } 128 129 130 public void setCopyAttachments(boolean copyAttachments) { 131 this.copyAttachments = copyAttachments; 132 if (getMessageTransformer() instanceof CopyTransformer) { 133 ((CopyTransformer) getMessageTransformer()).setCopyAttachments(copyAttachments); 134 } 135 } 136 137 138 141 protected void copyPropertiesAndAttachments(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 142 if (isCopyProperties()) { 143 CopyTransformer.copyProperties(in, out); 144 } 145 if (isCopyAttachments()) { 146 CopyTransformer.copyAttachments(in, out); 147 } 148 } 149 } 150 | Popular Tags |