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.camel.component.jbi;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.servicemix.jbi.resolver.URIResolver;
022    
023    import javax.jbi.component.ComponentContext;
024    import javax.jbi.messaging.DeliveryChannel;
025    import javax.jbi.messaging.MessageExchange;
026    import javax.jbi.messaging.MessageExchangeFactory;
027    import javax.jbi.messaging.MessagingException;
028    
029    /**
030     * A @{link Processor} which takes a Camel {@link Exchange} and invokes it into JBI using the straight JBI API
031     *
032     * @version $Revision: 563665 $
033     */
034    public class ToJbiProcessor implements Processor {
035        private JbiBinding binding;
036        private ComponentContext componentContext;
037        private String destinationUri;
038    
039        public ToJbiProcessor(JbiBinding binding, ComponentContext componentContext, String destinationUri) {
040            this.binding = binding;
041            this.componentContext = componentContext;
042            this.destinationUri = destinationUri;
043        }
044    
045        public void process(Exchange exchange) {
046            try {
047                DeliveryChannel deliveryChannel = componentContext.getDeliveryChannel();
048                MessageExchangeFactory exchangeFactory = deliveryChannel.createExchangeFactory();
049                MessageExchange messageExchange = binding.makeJbiMessageExchange(exchange, exchangeFactory);
050    
051                URIResolver.configureExchange(messageExchange, componentContext, destinationUri);
052                deliveryChannel.sendSync(messageExchange);
053            }
054            catch (MessagingException e) {
055                throw new JbiException(e);
056            }
057        }
058    }