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