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