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.eip.support.resequence;
018    
019    import java.net.URI;
020    
021    import javax.jbi.management.DeploymentException;
022    import javax.jbi.messaging.InOnly;
023    import javax.jbi.messaging.MessageExchange;
024    import javax.jbi.messaging.MessagingException;
025    import javax.jbi.messaging.NormalizedMessage;
026    import javax.jbi.messaging.RobustInOnly;
027    
028    import org.apache.servicemix.common.util.MessageUtil;
029    import org.apache.servicemix.eip.EIPEndpoint;
030    import org.apache.servicemix.eip.support.ExchangeTarget;
031    import org.apache.servicemix.jbi.transformer.CopyTransformer;
032    import org.apache.servicemix.jbi.transformer.MessageTransformer;
033    
034    public abstract class ResequencerBase extends EIPEndpoint {
035    
036        private MessageTransformer messageCopier = new CopyTransformer();
037    
038        private ExchangeTarget target;
039        
040        public MessageTransformer getMessageCopier() {
041            return messageCopier;
042        }
043    
044        public ExchangeTarget getTarget() {
045            return target;
046        }
047    
048        public void setTarget(ExchangeTarget target) {
049            this.target = target;
050        }
051        
052        @Override
053        public void validate() throws DeploymentException {
054            super.validate();
055            if (target == null) {
056                throw new IllegalArgumentException("target must be set to a valid ExchangeTarget");
057            }
058        }
059        
060        public void validateMessageExchange(MessageExchange exchange) throws MessagingException {
061            if ((exchange instanceof InOnly) || (exchange instanceof RobustInOnly)) {
062                return;
063            }
064            fail(exchange, new UnsupportedOperationException("Use an InOnly or RobustInOnly MEP"));
065        }
066    
067        protected MessageExchange createTargetExchange(NormalizedMessage message, URI exchangePattern) throws MessagingException {
068            MessageExchange targetExchange = getExchangeFactory().createExchange(exchangePattern);
069            target.configureTarget(targetExchange, getContext());
070            MessageUtil.transferToIn(message, targetExchange);
071            return targetExchange;
072        }
073        
074    }