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.xmpp;
019    
020    import org.apache.camel.Exchange;
021    import org.apache.camel.impl.DefaultProducer;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.jivesoftware.smack.GroupChat;
025    import org.jivesoftware.smack.XMPPException;
026    import org.jivesoftware.smack.packet.Message;
027    
028    /**
029     * @version $Revision: 534145 $
030     */
031    public class XmppGroupChatProducer extends DefaultProducer {
032        private static final transient Log log = LogFactory.getLog(XmppGroupChatProducer.class);
033        private final XmppEndpoint endpoint;
034        private final String room;
035        private GroupChat chat;
036    
037        public XmppGroupChatProducer(XmppEndpoint endpoint, String room) {
038            super(endpoint);
039            this.endpoint = endpoint;
040            this.room = room;
041            if (room == null) {
042                throw new IllegalArgumentException("No room property specified");
043            }
044        }
045    
046        public void process(Exchange exchange) {
047            // TODO it would be nice if we could reuse the message from the exchange
048            Message message = chat.createMessage();
049            message.setTo(room);
050            message.setFrom(endpoint.getUser());
051    
052            endpoint.getBinding().populateXmppMessage(message, exchange);
053            if (log.isDebugEnabled()) {
054                log.debug(">>>> message: " + message.getBody());
055            }
056            try {
057                chat.sendMessage(message);
058            }
059            catch (XMPPException e) {
060                throw new RuntimeXmppException(e);
061            }
062        }
063    
064        @Override
065        protected void doStart() throws Exception {
066            super.doStart();
067            if (chat == null) {
068                chat = endpoint.getConnection().createGroupChat(room);
069            }
070        }
071    
072        @Override
073        protected void doStop() throws Exception {
074            if (chat != null) {
075                chat.leave();
076                chat = null;
077            }
078            super.doStop();
079        }
080    
081        // Properties
082        //-------------------------------------------------------------------------
083        public GroupChat getChat() {
084            return chat;
085        }
086    
087        public void setChat(GroupChat chat) {
088            this.chat = chat;
089        }
090    
091        public String getRoom() {
092            return room;
093        }
094    }