001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.component.cxf;
019
020 import org.apache.camel.Processor;
021 import org.apache.camel.impl.DefaultConsumer;
022 import org.apache.cxf.message.Message;
023 import org.apache.cxf.transport.Destination;
024 import org.apache.cxf.transport.MessageObserver;
025 import org.apache.cxf.transport.local.LocalTransportFactory;
026
027 /**
028 * A consumer of exchanges for a service in CXF
029 *
030 * @version $Revision: 534145 $
031 */
032 public class CxfConsumer extends DefaultConsumer<CxfExchange> {
033 private CxfEndpoint endpoint;
034 private final LocalTransportFactory transportFactory;
035 private Destination destination;
036
037 public CxfConsumer(CxfEndpoint endpoint, Processor processor, LocalTransportFactory transportFactory) {
038 super(endpoint, processor);
039 this.endpoint = endpoint;
040 this.transportFactory = transportFactory;
041 }
042
043 @Override
044 protected void doStart() throws Exception {
045 super.doStart();
046
047 destination = transportFactory.getDestination(endpoint.getEndpointInfo());
048 destination.setMessageObserver(new MessageObserver() {
049 public void onMessage(Message message) {
050 incomingCxfMessage(message);
051 }
052 });
053 }
054
055 @Override
056 protected void doStop() throws Exception {
057 if (destination != null) {
058 destination.shutdown();
059 }
060 super.doStop();
061 }
062
063 protected void incomingCxfMessage(Message message) {
064 try {
065 CxfExchange exchange = endpoint.createExchange(message);
066 getProcessor().process(exchange);
067 } catch (Exception e) {
068 // TODO: what do do if we are getting processing errors from camel? Shutdown?
069 e.printStackTrace();
070 }
071 }
072 }