Coverage Report - org.mule.providers.xmpp.XmppConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
XmppConnector
22%
8/37
0%
0/10
1.6
 
 1  
 /*
 2  
  * $Id: XmppConnector.java 9169 2007-10-16 12:47:45Z dirk.olmes $
 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.providers.xmpp;
 12  
 
 13  
 import org.mule.providers.AbstractConnector;
 14  
 import org.mule.umo.UMOException;
 15  
 import org.mule.umo.endpoint.UMOEndpointURI;
 16  
 import org.mule.umo.lifecycle.InitialisationException;
 17  
 
 18  
 import org.jivesoftware.smack.AccountManager;
 19  
 import org.jivesoftware.smack.XMPPConnection;
 20  
 import org.jivesoftware.smack.XMPPException;
 21  
 
 22  
 /**
 23  
  * <code>XmppConnector</code> TODO
 24  
  */
 25  28
 public class XmppConnector extends AbstractConnector
 26  
 {
 27  
     public static final String XMPP_PROPERTY_PREFIX = "";
 28  
     public static final String XMPP_SUBJECT = XMPP_PROPERTY_PREFIX + "subject";
 29  
     public static final String XMPP_THREAD = XMPP_PROPERTY_PREFIX + "thread";
 30  
     public static final String XMPP_TO = XMPP_PROPERTY_PREFIX + "to";
 31  
     public static final String XMPP_FROM = XMPP_PROPERTY_PREFIX + "from";
 32  
     public static final String XMPP_GROUP_CHAT = XMPP_PROPERTY_PREFIX + "groupChat";
 33  
     public static final String XMPP_NICKNAME = XMPP_PROPERTY_PREFIX + "nickname";
 34  
 
 35  
 
 36  
     protected void doInitialise() throws InitialisationException
 37  
     {
 38  
         // template method, nothing to do
 39  28
     }
 40  
 
 41  
     protected void doDispose()
 42  
     {
 43  
         // template method
 44  28
     }
 45  
 
 46  
     protected void doConnect() throws Exception
 47  
     {
 48  
         // template method
 49  4
     }
 50  
 
 51  
     protected void doDisconnect() throws Exception
 52  
     {
 53  
         // template method
 54  4
     }
 55  
 
 56  
     protected void doStart() throws UMOException
 57  
     {
 58  
         // template method
 59  4
     }
 60  
 
 61  
     protected void doStop() throws UMOException
 62  
     {
 63  
         // template method
 64  4
     }
 65  
 
 66  
     public String getProtocol()
 67  
     {
 68  88
         return "xmpp";
 69  
     }
 70  
 
 71  
     public XMPPConnection createXmppConnection(UMOEndpointURI endpointURI) throws XMPPException
 72  
     {
 73  0
         logger.info("Trying to find XMPP connection for uri: " + endpointURI);
 74  
 
 75  0
         String username = endpointURI.getUsername();
 76  0
         String hostname = endpointURI.getHost();
 77  0
         String password = endpointURI.getPassword();
 78  0
         String resource = (String)endpointURI.getParams().get("resource");
 79  
 
 80  0
         XMPPConnection xmppConnection = this.doCreateXmppConnection(endpointURI);
 81  
 
 82  0
         if (!xmppConnection.isAuthenticated())
 83  
         {
 84  
             // Make sure we have an account. If we don't, make one.
 85  
             try
 86  
             {
 87  0
                 AccountManager accManager = new AccountManager(xmppConnection);
 88  0
                 accManager.createAccount(username, password);
 89  
             }
 90  0
             catch (XMPPException ex)
 91  
             {
 92  
                 // User probably already exists, throw away...
 93  0
                 logger.info("*** account (" + username + ") already exists ***");
 94  0
             }
 95  
 
 96  0
             if (logger.isDebugEnabled())
 97  
             {
 98  0
                 logger.debug("Logging in as: " + username);
 99  0
                 logger.debug("pw is        : " + password);
 100  0
                 logger.debug("server       : " + hostname);
 101  0
                 logger.debug("resource     : " + resource);
 102  
             }
 103  
 
 104  0
             if (resource == null)
 105  
             {
 106  0
                 xmppConnection.login(username, password);
 107  
             }
 108  
             else
 109  
             {
 110  0
                 xmppConnection.login(username, password, resource);
 111  
             }
 112  
         }
 113  
         else
 114  
         {
 115  0
             if (logger.isDebugEnabled())
 116  
             {
 117  0
                 logger.debug("Already authenticated on this connection, no need to log in again.");
 118  
             }
 119  
         }
 120  0
         return xmppConnection;
 121  
     }
 122  
 
 123  
     /**
 124  
      * This method creates and returns the {@link XMPPConnection} object that's uses to talk to
 125  
      * the Jabber server.
 126  
      * <p/>
 127  
      * Subclasses can override this method to create a more specialized {@link XMPPConnection}.
 128  
      * 
 129  
      * @param endpointURI
 130  
      * @return
 131  
      * @throws XMPPException
 132  
      */
 133  
     protected XMPPConnection doCreateXmppConnection(UMOEndpointURI endpointURI) throws XMPPException
 134  
     {
 135  0
         XMPPConnection xmppConnection = null;
 136  
         
 137  0
         if (endpointURI.getPort() != -1)
 138  
         {
 139  0
             xmppConnection = new XMPPConnection(endpointURI.getHost(), endpointURI.getPort());
 140  
         }
 141  
         else
 142  
         {
 143  0
             xmppConnection = new XMPPConnection(endpointURI.getHost());
 144  
         }
 145  
         
 146  0
         return xmppConnection;
 147  
     }
 148  
 
 149  
     public boolean isRemoteSyncEnabled()
 150  
     {
 151  0
         return true;
 152  
     }
 153  
 }