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
035: ComponentSupport implements MessageExchangeListener {
036:
037: private boolean copyProperties = true;
038: private boolean copyAttachments = true;
039:
040: protected TransformComponentSupport() {
041: }
042:
043: protected TransformComponentSupport(QName service, String endpoint) {
044: super (service, endpoint);
045: }
046:
047: public void onMessageExchange(MessageExchange exchange) {
048: // Skip done exchanges
049: if (exchange.getStatus() == ExchangeStatus.DONE) {
050: return;
051: // Handle error exchanges
052: } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
053: return;
054: }
055: try {
056: InOnly outExchange = null;
057: NormalizedMessage in = getInMessage(exchange);
058: NormalizedMessage out;
059: if (isInAndOut(exchange)) {
060: out = exchange.createMessage();
061: } else {
062: outExchange = getExchangeFactory()
063: .createInOnlyExchange();
064: outExchange.setProperty(JbiConstants.SENDER_ENDPOINT,
065: getService() + ":" + getEndpoint());
066: String processCorrelationId = (String) exchange
067: .getProperty(JbiConstants.CORRELATION_ID);
068: if (processCorrelationId != null) {
069: outExchange.setProperty(
070: JbiConstants.CORRELATION_ID,
071: processCorrelationId);
072: }
073: out = outExchange.createMessage();
074: }
075: boolean txSync = exchange.isTransacted()
076: && Boolean.TRUE.equals(exchange
077: .getProperty(JbiConstants.SEND_SYNC));
078: copyPropertiesAndAttachments(exchange, in, out);
079: if (transform(exchange, in, out)) {
080: if (isInAndOut(exchange)) {
081: exchange.setMessage(out, "out");
082: if (txSync) {
083: getDeliveryChannel().sendSync(exchange);
084: } else {
085: getDeliveryChannel().send(exchange);
086: }
087: } else {
088: outExchange.setMessage(out, "in");
089: if (txSync) {
090: getDeliveryChannel().sendSync(outExchange);
091: } else {
092: getDeliveryChannel().send(outExchange);
093: }
094: exchange.setStatus(ExchangeStatus.DONE);
095: getDeliveryChannel().send(exchange);
096: }
097: } else {
098: exchange.setStatus(ExchangeStatus.DONE);
099: getDeliveryChannel().send(exchange);
100: }
101: } catch (Exception e) {
102: try {
103: fail(exchange, e);
104: } catch (Exception e2) {
105: logger.warn("Unable to handle error: " + e2, e2);
106: if (logger.isDebugEnabled()) {
107: logger.debug("Original error: " + e, e);
108: }
109: }
110: }
111: }
112:
113: // Implementation methods
114: //-------------------------------------------------------------------------
115:
116: /**
117: * Transforms the given out message
118: */
119: protected abstract boolean transform(MessageExchange exchange,
120: NormalizedMessage in, NormalizedMessage out)
121: throws Exception;
122:
123: public boolean isCopyProperties() {
124: return copyProperties;
125: }
126:
127: public void setCopyProperties(boolean copyProperties) {
128: this .copyProperties = copyProperties;
129: if (getMessageTransformer() instanceof CopyTransformer) {
130: ((CopyTransformer) getMessageTransformer())
131: .setCopyProperties(copyProperties);
132: }
133: }
134:
135: public boolean isCopyAttachments() {
136: return copyAttachments;
137: }
138:
139: public void setCopyAttachments(boolean copyAttachments) {
140: this .copyAttachments = copyAttachments;
141: if (getMessageTransformer() instanceof CopyTransformer) {
142: ((CopyTransformer) getMessageTransformer())
143: .setCopyAttachments(copyAttachments);
144: }
145: }
146:
147: /**
148: * If enabled the properties and attachments are copied to the destination message
149: */
150: protected void copyPropertiesAndAttachments(
151: MessageExchange exchange, NormalizedMessage in,
152: NormalizedMessage out) throws MessagingException {
153: if (isCopyProperties()) {
154: CopyTransformer.copyProperties(in, out);
155: }
156: if (isCopyAttachments()) {
157: CopyTransformer.copyAttachments(in, out);
158: }
159: }
160: }
|