Coverage Report - org.mule.transport.xmpp.XmppMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
XmppMessageRequester
0%
0/27
0%
0/12
2.6
 
 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  0
     private volatile XMPPConnection xmppConnection = null;
 34  
 
 35  
     public XmppMessageRequester(InboundEndpoint endpoint)
 36  
     {
 37  0
         super(endpoint);
 38  0
         this.connector = (XmppConnector)endpoint.getConnector();
 39  0
     }
 40  
 
 41  
     protected void doConnect() throws Exception
 42  
     {
 43  0
         if (xmppConnection == null)
 44  
         {
 45  0
             EndpointURI uri = endpoint.getEndpointURI();
 46  0
             xmppConnection = connector.createXmppConnection(uri);
 47  
         }
 48  0
     }
 49  
 
 50  
     protected void doDisconnect() throws Exception
 51  
     {
 52  
         try
 53  
         {
 54  0
             if (xmppConnection != null)
 55  
             {
 56  0
                 xmppConnection.close();
 57  
             }
 58  
         }
 59  
         finally
 60  
         {
 61  0
             xmppConnection = null;
 62  0
         }
 63  0
     }
 64  
 
 65  
     protected void doDispose()
 66  
     {
 67  
         // template method
 68  0
     }
 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  0
         String to = (String)endpoint.getProperty("folder");
 85  0
         if (to == null)
 86  
         {
 87  0
             throw new MalformedEndpointException(endpoint.getEndpointURI().toString());
 88  
         }
 89  0
         Chat chat = xmppConnection.createChat(to);
 90  0
         Message message = null;
 91  0
         if (timeout == MuleEvent.TIMEOUT_WAIT_FOREVER)
 92  
         {
 93  0
             message = chat.nextMessage();
 94  
         }
 95  0
         else if (timeout == MuleEvent.TIMEOUT_DO_NOT_WAIT)
 96  
         {
 97  0
             message = chat.nextMessage(1);
 98  
         }
 99  
         else
 100  
         {
 101  0
             message = chat.nextMessage(timeout);
 102  
         }
 103  0
         if (message != null)
 104  
         {
 105  0
             return new DefaultMuleMessage(connector.getMessageAdapter(message));
 106  
         }
 107  
         else
 108  
         {
 109  0
             return null;
 110  
         }
 111  
     }
 112  
 
 113  
 }