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.common;
018    
019    import javax.jbi.messaging.DeliveryChannel;
020    import javax.jbi.messaging.ExchangeStatus;
021    import javax.jbi.messaging.MessageExchange;
022    import javax.jbi.messaging.MessageExchangeFactory;
023    import javax.jbi.messaging.MessagingException;
024    import javax.jbi.messaging.MessageExchange.Role;
025    import javax.jbi.servicedesc.ServiceEndpoint;
026    import javax.xml.namespace.QName;
027    
028    /**
029     * This class is a wrapper around an existing DeliveryChannel
030     * that will be given to service engine endpoints so that
031     * they are able to send messages and to interact with the
032     * JBI container.
033     * 
034     * @author gnodet
035     */
036    public class EndpointDeliveryChannel implements DeliveryChannel {
037    
038        private final DeliveryChannel channel;
039        private final Endpoint endpoint;
040        
041        public EndpointDeliveryChannel(Endpoint endpoint) throws MessagingException {
042            this.endpoint = endpoint;
043            this.channel = endpoint.getServiceUnit().getComponent().getComponentContext().getDeliveryChannel();
044        }
045    
046        public MessageExchange accept() throws MessagingException {
047            throw new UnsupportedOperationException();
048        }
049    
050        public MessageExchange accept(long timeout) throws MessagingException {
051            throw new UnsupportedOperationException();
052        }
053    
054        public void close() throws MessagingException {
055            throw new UnsupportedOperationException();
056        }
057    
058        public MessageExchangeFactory createExchangeFactory() {
059            return channel.createExchangeFactory();
060        }
061    
062        public MessageExchangeFactory createExchangeFactory(QName interfaceName) {
063            return channel.createExchangeFactory(interfaceName);
064        }
065    
066        public MessageExchangeFactory createExchangeFactory(ServiceEndpoint endpoint) {
067            return channel.createExchangeFactory(endpoint);
068        }
069    
070        public MessageExchangeFactory createExchangeFactoryForService(QName serviceName) {
071            return channel.createExchangeFactoryForService(serviceName);
072        }
073    
074        public void send(MessageExchange exchange) throws MessagingException {
075            if (exchange.getStatus() == ExchangeStatus.ACTIVE && exchange.getRole() == Role.CONSUMER) {
076                ServiceMixComponent comp = endpoint.getServiceUnit().getComponent();
077                comp.prepareConsumerExchange(exchange, endpoint);
078            }
079            channel.send(exchange);
080        }
081    
082        public boolean sendSync(MessageExchange exchange, long timeout) throws MessagingException {
083            if (exchange.getStatus() == ExchangeStatus.ACTIVE && exchange.getRole() == Role.CONSUMER) {
084                ServiceMixComponent comp = endpoint.getServiceUnit().getComponent();
085                comp.prepareConsumerExchange(exchange, endpoint);
086            }
087            return channel.sendSync(exchange, timeout);
088        }
089    
090        public boolean sendSync(MessageExchange exchange) throws MessagingException {
091            if (exchange.getStatus() == ExchangeStatus.ACTIVE && exchange.getRole() == Role.CONSUMER) {
092                ServiceMixComponent comp = endpoint.getServiceUnit().getComponent();
093                comp.prepareConsumerExchange(exchange, endpoint);
094            }
095            return channel.sendSync(exchange);
096        }
097    }