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