View Javadoc

1   /*
2    * $Id: AbstractXmppConversation.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.xmpp;
12  
13  import org.mule.api.endpoint.ImmutableEndpoint;
14  import org.mule.transport.ConnectException;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  import org.jivesoftware.smack.PacketCollector;
19  import org.jivesoftware.smack.XMPPConnection;
20  import org.jivesoftware.smack.filter.PacketFilter;
21  import org.jivesoftware.smack.packet.Message;
22  
23  
24  public abstract class AbstractXmppConversation implements XmppConversation
25  {
26      protected final Log logger = LogFactory.getLog(getClass());
27      
28      protected XMPPConnection connection;
29      protected String recipient;
30      protected PacketCollector packetCollector;
31  
32      public AbstractXmppConversation(ImmutableEndpoint endpoint)
33      {
34          super();        
35          connection = ((XmppConnector) endpoint.getConnector()).getXmppConnection();
36          recipient = XmppConnector.getRecipient(endpoint);
37      }
38  
39      public void connect() throws ConnectException
40      {
41          doConnect();
42          packetCollector = createPacketCollector();
43      }
44      
45      /**
46       * Subclasses can override this method to create their conversation specific connection.
47       */
48      protected void doConnect() throws ConnectException
49      {
50          // template method
51      }
52  
53      /**
54       * @return a {@link PacketCollector} that can be used to retrieve messages for this 
55       * conversation.
56       */
57      protected PacketCollector createPacketCollector()
58      {
59          PacketFilter filter = createPacketFilter();
60          return connection.createPacketCollector(filter);
61      }
62      
63      /**
64       * @return a {@link PacketFilter} instance that matches the desired message type and recipient
65       * for this conversation.
66       */
67      protected PacketFilter createPacketFilter()
68      {
69          return null;
70      }
71      
72      public void disconnect()
73      {
74          if (packetCollector != null)
75          {
76              packetCollector.cancel();
77          }
78          
79          doDisconnect();
80      }
81  
82      /**
83       * Subclasses can override this method to perform custom disconnect actions.
84       */
85      protected void doDisconnect()
86      {
87          // template method
88      }
89      
90      public Message receive(long timeout)
91      {
92          // The filter of our packetCollector should make sure that we receive only
93          // Message instances here
94          return (Message) packetCollector.nextResult(timeout);
95      }
96      
97      public Message receive()
98      {
99          // The filter of our packetCollector should make sure that we receive only
100         // Message instances here
101         return (Message) packetCollector.nextResult();
102     }
103 }