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.cometd;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Message;
021    import org.apache.camel.Processor;
022    import org.apache.camel.impl.DefaultConsumer;
023    import org.apache.camel.impl.DefaultMessage;
024    import org.cometd.Bayeux;
025    import org.cometd.Client;
026    import org.cometd.server.AbstractBayeux;
027    import org.cometd.server.BayeuxService;
028    
029    /**
030     * A Consumer for receiving messages using Cometd and Bayeux protocol.
031     * 
032     * @version $Revision: 18251 $
033     */
034    public class CometdConsumer extends DefaultConsumer implements CometdProducerConsumer {
035    
036        private AbstractBayeux bayeux;
037        private final CometdEndpoint endpoint;
038        private ConsumerService service;
039    
040        public CometdConsumer(CometdEndpoint endpoint, Processor processor) {
041            super(endpoint, processor);
042            this.endpoint = endpoint;
043        }
044    
045        @Override
046        public void start() throws Exception {
047            super.start();
048            endpoint.connect(this);
049            service = new ConsumerService(endpoint.getPath(), bayeux, this);
050        }
051    
052        @Override
053        public void stop() throws Exception {
054            endpoint.disconnect(this);
055            super.stop();
056        }
057    
058        public void setBayeux(AbstractBayeux bayeux) {
059            this.bayeux = bayeux;
060        }
061    
062        public CometdEndpoint getEndpoint() {
063            return endpoint;
064        }
065    
066        public static class ConsumerService extends BayeuxService {
067    
068            private final CometdEndpoint endpoint;
069            private final CometdConsumer consumer;
070    
071            public ConsumerService(String channel, Bayeux bayeux, CometdConsumer consumer) {
072                super(bayeux, channel);
073                this.consumer = consumer;
074                this.endpoint = consumer.getEndpoint();
075                subscribe(channel, "push");
076            }
077    
078            public void push(Client client, Object data) throws Exception {
079                Message message = new DefaultMessage();
080                message.setBody(data);
081    
082                Exchange exchange = endpoint.createExchange();
083                exchange.setIn(message);
084    
085                consumer.getProcessor().process(exchange);
086            }
087        }
088    }