Author: ningjiang
Date: Thu Aug 27 02:23:25 2009
New Revision: 808253

URL: http://svn.apache.org/viewvc?rev=808253&view=rev
Log:
CAMEL-1946 applied the patch with thanks Stan

Added:
    
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppLogger.java
   (with props)
    
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteMultipleProducersSingleConsumerTest.java
   (with props)
Modified:
    
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java
    
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java
    
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppPrivateChatProducer.java
    
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteChatTest.java

Modified: 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java?rev=808253&r1=808252&r2=808253&view=diff
==============================================================================
--- 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java
 (original)
+++ 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java
 Thu Aug 27 02:23:25 2009
@@ -22,6 +22,8 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.ChatManager;
+import org.jivesoftware.smack.ChatManagerListener;
 import org.jivesoftware.smack.MessageListener;
 import org.jivesoftware.smack.PacketListener;
 import org.jivesoftware.smack.SmackConfiguration;
@@ -40,11 +42,12 @@
  *
  * @version $Revision$
  */
-public class XmppConsumer extends DefaultConsumer implements PacketListener, 
MessageListener {
+public class XmppConsumer extends DefaultConsumer implements PacketListener, 
MessageListener, ChatManagerListener {
     private static final transient Log LOG = 
LogFactory.getLog(XmppConsumer.class);
     private final XmppEndpoint endpoint;
     private MultiUserChat muc;
     private Chat privateChat;
+    private ChatManager chatManager;
     private XMPPConnection connection;
 
     public XmppConsumer(XmppEndpoint endpoint, Processor processor) {
@@ -55,21 +58,19 @@
     @Override
     protected void doStart() throws Exception {
         connection = endpoint.createConnection();
+        chatManager = connection.getChatManager();
+        chatManager.addChatListener(this);
 
         if (endpoint.getRoom() == null) {
-
-            // if an existing chat session has been opened (for example by a 
producer) let's
-            // just add a listener to that chat
-            privateChat = 
connection.getChatManager().getThreadChat(endpoint.getParticipant());
+            privateChat = chatManager.getThreadChat(endpoint.getChatId());
 
             if (privateChat != null) {
                 LOG.debug("Adding listener to existing chat opened to " + 
privateChat.getParticipant());
                 privateChat.addMessageListener(this);
             } else {                
-                privateChat = 
connection.getChatManager().createChat(endpoint.getParticipant(), 
endpoint.getParticipant(), this);
+                privateChat = 
connection.getChatManager().createChat(endpoint.getParticipant(),endpoint.getChatId(),
 this);
                 LOG.debug("Opening private chat to " + 
privateChat.getParticipant());
             }
-
         } else {
             // add the presence packet listener to the connection so we only 
get packets that concers us
             // we must add the listener before creating the muc
@@ -105,6 +106,13 @@
         //the endpoint will clean up the connection
     }
 
+    public void chatCreated(Chat chat, boolean createdLocally) {
+        if (!createdLocally) {
+            LOG.debug("Accepting incoming chat session from " + 
chat.getParticipant());
+            chat.addMessageListener(this);
+        }
+    }
+
     public void processPacket(Packet packet) {
         if (packet instanceof Message) {
             processMessage(null, (Message)packet);

Modified: 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java?rev=808253&r1=808252&r2=808253&view=diff
==============================================================================
--- 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java
 (original)
+++ 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java
 Thu Aug 27 02:23:25 2009
@@ -35,7 +35,9 @@
 import org.jivesoftware.smack.ConnectionConfiguration;
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.filter.PacketFilter;
 import org.jivesoftware.smack.packet.Message;
+import org.jivesoftware.smack.packet.Packet;
 import org.jivesoftware.smackx.muc.MultiUserChat;
 
 /**
@@ -137,6 +139,17 @@
 
         connection.connect();
 
+        connection.addPacketListener(new XmppLogger("INBOUND"), new 
PacketFilter() {
+                public boolean accept(Packet packet) {
+                    return true;
+                }
+        });
+        connection.addPacketWriterListener(new XmppLogger("OUTBOUND"), new 
PacketFilter() {
+                public boolean accept(Packet packet) {
+                    return true;
+                }
+        });
+
         if (login && !connection.isAuthenticated()) {
             if (user != null) {
                 if (LOG.isDebugEnabled()) {
@@ -196,6 +209,10 @@
         return connection.getHost() + ":" + connection.getPort() + "/" + 
connection.getServiceName();
     }
 
+    public String getChatId() {
+        return "Chat:" + getParticipant() + ":" + getUser();
+    }
+
     protected synchronized void destroy() throws Exception {
         if (connection != null) {
             connection.disconnect();

Added: 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppLogger.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppLogger.java?rev=808253&view=auto
==============================================================================
--- 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppLogger.java
 (added)
+++ 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppLogger.java
 Thu Aug 27 02:23:25 2009
@@ -0,0 +1,22 @@
+package org.apache.camel.component.xmpp;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jivesoftware.smack.PacketListener;
+import org.jivesoftware.smack.packet.Packet;
+
+public class XmppLogger implements PacketListener {
+
+    private static final transient Log LOG = 
LogFactory.getLog(XmppLogger.class);
+    private String direction;
+
+    public XmppLogger(String direction) {
+        this.direction = direction;
+    }
+
+    public void processPacket(Packet packet) {
+        if ( LOG.isDebugEnabled() ) {
+            LOG.debug(direction + " : " + packet.toXML());
+        }
+    }
+}

Propchange: 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppLogger.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppLogger.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppPrivateChatProducer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppPrivateChatProducer.java?rev=808253&r1=808252&r2=808253&view=diff
==============================================================================
--- 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppPrivateChatProducer.java
 (original)
+++ 
camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppPrivateChatProducer.java
 Thu Aug 27 02:23:25 2009
@@ -65,11 +65,14 @@
         }
 
         ChatManager chatManager = connection.getChatManager();
-        Chat chat = chatManager.getThreadChat(getParticipant());
+        LOG.debug("Looking for existing chat instance with thread ID " + 
endpoint.getChatId());
+        Chat chat = chatManager.getThreadChat(endpoint.getChatId());
         if (chat == null) {
-            chat = chatManager.createChat(getParticipant(), getParticipant(), 
new MessageListener() {
+            LOG.debug("Creating new chat instance with thread ID " + 
endpoint.getChatId());
+            chat = chatManager.createChat(getParticipant(), 
endpoint.getChatId(), new MessageListener() {
                 public void processMessage(Chat chat, Message message) {
                     // not here to do conversation
+                    LOG.debug("Received and discarding message from " + 
getParticipant() + " : " + message.getBody());
                 }
             });
         }
@@ -78,7 +81,7 @@
         try {
             message = new Message();
             message.setTo(getParticipant());
-            message.setThread(getParticipant());
+            message.setThread(endpoint.getChatId());
             message.setType(Message.Type.normal);
 
             endpoint.getBinding().populateXmppMessage(message, exchange);

Modified: 
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteChatTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteChatTest.java?rev=808253&r1=808252&r2=808253&view=diff
==============================================================================
--- 
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteChatTest.java
 (original)
+++ 
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteChatTest.java
 Thu Aug 27 02:23:25 2009
@@ -90,11 +90,11 @@
     }
 
     protected String getProducerUri() {
-        return 
"xmpp://jabber.org:5222/camel_consumer?user=camel_producer&password=secret&serviceName=jabber.org";
+        return 
"xmpp://jabber.org:5222/[email protected]?user=camel_producer&password=secret&serviceName=jabber.org";
     }
     
     protected String getConsumerUri() {
-        return 
"xmpp://jabber.org:5222/camel_producer?user=camel_consumer&password=secret&serviceName=jabber.org";
+        return 
"xmpp://jabber.org:5222/[email protected]?user=camel_consumer&password=secret&serviceName=jabber.org";
     }
 
 }

Added: 
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteMultipleProducersSingleConsumerTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteMultipleProducersSingleConsumerTest.java?rev=808253&view=auto
==============================================================================
--- 
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteMultipleProducersSingleConsumerTest.java
 (added)
+++ 
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteMultipleProducersSingleConsumerTest.java
 Thu Aug 27 02:23:25 2009
@@ -0,0 +1,106 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.xmpp;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.jivesoftware.smack.packet.Message;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class XmppRouteMultipleProducersSingleConsumerTest extends 
CamelTestSupport {
+    private static final transient Log LOG = 
LogFactory.getLog(XmppRouteMultipleProducersSingleConsumerTest.class);
+    protected MockEndpoint goodEndpoint;
+    protected MockEndpoint badEndpoint;
+
+    @Test
+    public void testProducerGetsEverything() throws Exception {
+
+        goodEndpoint = (MockEndpoint)context.getEndpoint("mock:good");
+        badEndpoint = (MockEndpoint)context.getEndpoint("mock:bad");
+
+        goodEndpoint.expectedMessageCount(4);
+        badEndpoint.expectedMessageCount(0);
+
+        template.sendBody("direct:toProducer1", "From producer");
+        template.sendBody("direct:toProducer1", "From producer");
+
+        template.sendBody("direct:toProducer2", "From producer1");
+        template.sendBody("direct:toProducer2", "From producer1");
+
+        goodEndpoint.assertIsSatisfied();
+        badEndpoint.assertIsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+
+                //getContext().setTracing(true);
+
+                Processor stringConverter = new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        XmppMessage xmppMessage = 
(XmppMessage)exchange.getIn();
+                        Message message = xmppMessage.getXmppMessage();
+                        String body = message.getBody();
+                        LOG.debug("Converting message - " + body);
+                        exchange.getIn().setBody(body);
+                    }
+                };
+
+                from("direct:toProducer1")
+                    .to(getProducer1Uri());
+
+                from("direct:toProducer2")
+                    .to(getProducer2Uri());
+
+                from(getConsumerUri())
+                    .process(stringConverter)
+                    .to(getConsumerUri());
+
+                from(getProducer1Uri())
+                    .process(stringConverter)
+                    .to("mock:good");
+
+                from(getProducer2Uri())
+                    .process(stringConverter)
+                    .to("mock:bad");
+            }
+        };
+    }
+
+    protected String getProducer1Uri() {
+        return 
"xmpp://jabber.org:5222/[email protected]?user=camel_producer&password=secret&serviceName=jabber.org";
+    }
+
+    protected String getProducer2Uri() {
+        return 
"xmpp://jabber.org:5222/[email protected]?user=camel_producer1&password=secret&serviceName=jabber.org";
+    }
+    
+    protected String getConsumerUri() {
+        return 
"xmpp://jabber.org:5222/[email protected]?user=camel_consumer&password=secret&serviceName=jabber.org";
+    }
+
+}

Propchange: 
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteMultipleProducersSingleConsumerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteMultipleProducersSingleConsumerTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to