001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.components.util;
018
019 import javax.jbi.messaging.ExchangeStatus;
020 import javax.jbi.messaging.InOnly;
021 import javax.jbi.messaging.MessageExchange;
022 import javax.jbi.messaging.MessagingException;
023 import javax.jbi.messaging.NormalizedMessage;
024 import javax.xml.namespace.QName;
025
026 import org.apache.servicemix.JbiConstants;
027 import org.apache.servicemix.MessageExchangeListener;
028
029 /**
030 * A useful base class for a transform component.
031 *
032 * @version $Revision: 564374 $
033 */
034 public abstract class TransformComponentSupport extends ComponentSupport implements MessageExchangeListener {
035
036 private boolean copyProperties = true;
037 private boolean copyAttachments = true;
038
039 protected TransformComponentSupport() {
040 }
041
042 protected TransformComponentSupport(QName service, String endpoint) {
043 super(service, endpoint);
044 }
045
046 public void onMessageExchange(MessageExchange exchange) {
047 // Skip done exchanges
048 if (exchange.getStatus() == ExchangeStatus.DONE) {
049 return;
050 // Handle error exchanges
051 } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
052 return;
053 }
054 try {
055 InOnly outExchange = null;
056 NormalizedMessage in = getInMessage(exchange);
057 NormalizedMessage out;
058 if (isInAndOut(exchange)) {
059 out = exchange.createMessage();
060 } else {
061 outExchange = getExchangeFactory().createInOnlyExchange();
062 outExchange.setProperty(JbiConstants.SENDER_ENDPOINT, getService() + ":" + getEndpoint());
063 String processCorrelationId = (String)exchange.getProperty(JbiConstants.CORRELATION_ID);
064 if (processCorrelationId != null) {
065 outExchange.setProperty(JbiConstants.CORRELATION_ID, processCorrelationId);
066 }
067 out = outExchange.createMessage();
068 }
069 boolean txSync = exchange.isTransacted() && Boolean.TRUE.equals(exchange.getProperty(JbiConstants.SEND_SYNC));
070 copyPropertiesAndAttachments(exchange, in, out);
071 if (transform(exchange, in, out)) {
072 if (isInAndOut(exchange)) {
073 exchange.setMessage(out, "out");
074 if (txSync) {
075 getDeliveryChannel().sendSync(exchange);
076 } else {
077 getDeliveryChannel().send(exchange);
078 }
079 } else {
080 outExchange.setMessage(out, "in");
081 if (txSync) {
082 getDeliveryChannel().sendSync(outExchange);
083 } else {
084 getDeliveryChannel().send(outExchange);
085 }
086 exchange.setStatus(ExchangeStatus.DONE);
087 getDeliveryChannel().send(exchange);
088 }
089 } else {
090 exchange.setStatus(ExchangeStatus.DONE);
091 getDeliveryChannel().send(exchange);
092 }
093 } catch (Exception e) {
094 try {
095 fail(exchange, e);
096 } catch (Exception e2) {
097 logger.warn("Unable to handle error: " + e2, e2);
098 if (logger.isDebugEnabled()) {
099 logger.debug("Original error: " + e, e);
100 }
101 }
102 }
103 }
104
105
106 // Implementation methods
107 //-------------------------------------------------------------------------
108
109 /**
110 * Transforms the given out message
111 */
112 protected abstract boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception;
113
114
115 public boolean isCopyProperties() {
116 return copyProperties;
117 }
118
119
120 public void setCopyProperties(boolean copyProperties) {
121 this.copyProperties = copyProperties;
122 if (getMessageTransformer() instanceof CopyTransformer) {
123 ((CopyTransformer) getMessageTransformer()).setCopyProperties(copyProperties);
124 }
125 }
126
127
128 public boolean isCopyAttachments() {
129 return copyAttachments;
130 }
131
132
133 public void setCopyAttachments(boolean copyAttachments) {
134 this.copyAttachments = copyAttachments;
135 if (getMessageTransformer() instanceof CopyTransformer) {
136 ((CopyTransformer) getMessageTransformer()).setCopyAttachments(copyAttachments);
137 }
138 }
139
140
141 /**
142 * If enabled the properties and attachments are copied to the destination message
143 */
144 protected void copyPropertiesAndAttachments(MessageExchange exchange, NormalizedMessage in,
145 NormalizedMessage out) throws MessagingException {
146 if (isCopyProperties()) {
147 CopyTransformer.copyProperties(in, out);
148 }
149 if (isCopyAttachments()) {
150 CopyTransformer.copyAttachments(in, out);
151 }
152 }
153 }