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.cxf;
018    
019    import org.apache.camel.Processor;
020    import org.apache.camel.impl.DefaultConsumer;
021    import org.apache.cxf.message.Message;
022    import org.apache.cxf.transport.Destination;
023    import org.apache.cxf.transport.MessageObserver;
024    import org.apache.cxf.transport.local.LocalTransportFactory;
025    
026    /**
027     * A consumer of exchanges for a service in CXF
028     * 
029     * @version $Revision: 563665 $
030     */
031    public class CxfConsumer extends DefaultConsumer<CxfExchange> {
032        private CxfEndpoint endpoint;
033        private final LocalTransportFactory transportFactory;
034        private Destination destination;
035    
036        public CxfConsumer(CxfEndpoint endpoint, Processor processor, LocalTransportFactory transportFactory) {
037            super(endpoint, processor);
038            this.endpoint = endpoint;
039            this.transportFactory = transportFactory;
040        }
041    
042        @Override
043        protected void doStart() throws Exception {
044            super.doStart();
045    
046            destination = transportFactory.getDestination(endpoint.getEndpointInfo());
047            destination.setMessageObserver(new MessageObserver() {
048                public void onMessage(Message message) {
049                    incomingCxfMessage(message);
050                }
051            });
052        }
053    
054        @Override
055        protected void doStop() throws Exception {
056            if (destination != null) {
057                destination.shutdown();
058            }
059            super.doStop();
060        }
061    
062        protected void incomingCxfMessage(Message message) {
063            try {
064                CxfExchange exchange = endpoint.createExchange(message);
065                getProcessor().process(exchange);
066            } catch (Exception e) {
067                // TODO: what do do if we are getting processing errors from camel?
068                // Shutdown?
069                e.printStackTrace();
070            }
071        }
072    }