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 java.util.Collection;
020    
021    import dojox.cometd.Client;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.impl.DefaultProducer;
024    import org.apache.camel.util.ObjectHelper;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.mortbay.cometd.AbstractBayeux;
028    
029    /**
030     * A Producer to send messages using Cometd and Bayeux protocol.
031     * 
032     * @version $Revision: 2780 $
033     */
034    public class CometdProducer extends DefaultProducer implements CometdProducerConsumer {
035        private static final transient Log LOG = LogFactory.getLog(CometdProducer.class);
036        
037        private AbstractBayeux bayeux;
038        private final CometdEndpoint endpoint;
039    
040        public CometdProducer(CometdEndpoint endpoint) {
041            super(endpoint);
042            this.endpoint = endpoint;
043        }
044    
045        @Override
046        public void start() throws Exception {
047            super.start();
048            endpoint.connect(this);
049        }
050    
051        @Override
052        public void stop() throws Exception {
053            super.stop();
054            endpoint.disconnect(this);
055        }
056    
057        public void process(final Exchange exchange) {
058            ObjectHelper.notNull(bayeux, "bayeux");
059    
060            Collection<Client> clients = bayeux.getClients();
061            for (Client client : clients) {
062                if (LOG.isTraceEnabled()) {
063                    LOG.trace("Delivering to client id: " + client.getId() + " path:" + endpoint.getPath() + " exchange: " + exchange);
064                }
065                client.deliver(client, endpoint.getPath(), exchange.getIn().getBody(), null);
066            }
067        }
068    
069        public CometdEndpoint getEndpoint() {
070            return endpoint;
071        }
072        
073        public AbstractBayeux getBayeux() {
074            return bayeux;
075        }
076    
077        public void setBayeux(AbstractBayeux bayeux) {
078            this.bayeux = bayeux;
079        }
080    }