View Javadoc

1   /*
2    * $Id: XmppMessageRequester.java 10961 2008-02-22 19:01:02Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.DefaultMuleMessage;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.endpoint.EndpointURI;
17  import org.mule.api.endpoint.InboundEndpoint;
18  import org.mule.api.endpoint.MalformedEndpointException;
19  import org.mule.transport.AbstractMessageRequester;
20  
21  import org.jivesoftware.smack.Chat;
22  import org.jivesoftware.smack.XMPPConnection;
23  import org.jivesoftware.smack.packet.Message;
24  
25  /**
26   * Allows Mule events to be received over Xmpp
27   */
28  
29  public class XmppMessageRequester extends AbstractMessageRequester
30  {
31  
32      private final XmppConnector connector;
33      private volatile XMPPConnection xmppConnection = null;
34  
35      public XmppMessageRequester(InboundEndpoint endpoint)
36      {
37          super(endpoint);
38          this.connector = (XmppConnector)endpoint.getConnector();
39      }
40  
41      protected void doConnect() throws Exception
42      {
43          if (xmppConnection == null)
44          {
45              EndpointURI uri = endpoint.getEndpointURI();
46              xmppConnection = connector.createXmppConnection(uri);
47          }
48      }
49  
50      protected void doDisconnect() throws Exception
51      {
52          try
53          {
54              if (xmppConnection != null)
55              {
56                  xmppConnection.close();
57              }
58          }
59          finally
60          {
61              xmppConnection = null;
62          }
63      }
64  
65      protected void doDispose()
66      {
67          // template method
68      }
69  
70      /**
71       * Make a specific request to the underlying transport
72       *
73       * @param timeout the maximum time the operation should block before returning.
74       *            The call should return immediately if there is data available. If
75       *            no data becomes available before the timeout elapses, null will be
76       *            returned
77       * @return the result of the request wrapped in a MuleMessage object. Null will be
78       *         returned if no data was avaialable
79       * @throws Exception if the call to the underlying protocal cuases an exception
80       */
81      protected MuleMessage doRequest(long timeout) throws Exception
82      {
83          // Should be in the form of xmpp://user:pass@host:[port]/folder
84          String to = (String)endpoint.getProperty("folder");
85          if (to == null)
86          {
87              throw new MalformedEndpointException(endpoint.getEndpointURI().toString());
88          }
89          Chat chat = xmppConnection.createChat(to);
90          Message message = null;
91          if (timeout == MuleEvent.TIMEOUT_WAIT_FOREVER)
92          {
93              message = chat.nextMessage();
94          }
95          else if (timeout == MuleEvent.TIMEOUT_DO_NOT_WAIT)
96          {
97              message = chat.nextMessage(1);
98          }
99          else
100         {
101             message = chat.nextMessage(timeout);
102         }
103         if (message != null)
104         {
105             return new DefaultMuleMessage(connector.getMessageAdapter(message));
106         }
107         else
108         {
109             return null;
110         }
111     }
112 
113 }