KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > xmpp > XmppConnector


1 /*
2  * $Id: XmppConnector.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.jivesoftware.smack.AccountManager;
14 import org.jivesoftware.smack.XMPPConnection;
15 import org.jivesoftware.smack.XMPPException;
16 import org.mule.providers.AbstractServiceEnabledConnector;
17 import org.mule.umo.endpoint.UMOEndpointURI;
18
19 /**
20  * <code>XmppConnector</code> TODO
21  *
22  * @author Peter Braswell
23  * @author John Evans
24  * @version $Revision: 3798 $
25  */

26 public class XmppConnector extends AbstractServiceEnabledConnector
27 {
28     public static final String JavaDoc XMPP_PROPERTY_PREFIX = "";
29     public static final String JavaDoc XMPP_SUBJECT = XMPP_PROPERTY_PREFIX + "subject";
30     public static final String JavaDoc XMPP_THREAD = XMPP_PROPERTY_PREFIX + "thread";
31     public static final String JavaDoc XMPP_TO = XMPP_PROPERTY_PREFIX + "to";
32     public static final String JavaDoc XMPP_FROM = XMPP_PROPERTY_PREFIX + "from";
33     public static final String JavaDoc XMPP_GROUP_CHAT = XMPP_PROPERTY_PREFIX + "groupChat";
34     public static final String JavaDoc XMPP_NICKNAME = XMPP_PROPERTY_PREFIX + "nickname";
35
36     public String JavaDoc getProtocol()
37     {
38         return "xmpp";
39     }
40
41     public XMPPConnection createXmppConnection(UMOEndpointURI endpointURI) throws XMPPException
42     {
43         logger.info("Trying to find XMPP connection for uri: " + endpointURI);
44         XMPPConnection xmppConnection = null;
45
46         String JavaDoc username = endpointURI.getUsername();
47         String JavaDoc hostname = endpointURI.getHost();
48         String JavaDoc password = endpointURI.getPassword();
49         String JavaDoc resource = (String JavaDoc)endpointURI.getParams().get("resource");
50
51         if (endpointURI.getPort() != -1)
52         {
53             xmppConnection = new XMPPConnection(endpointURI.getHost(), endpointURI.getPort());
54         }
55         else
56         {
57             xmppConnection = new XMPPConnection(endpointURI.getHost());
58         }
59
60         if (!xmppConnection.isAuthenticated())
61         {
62             // Make sure we have an account. If we don't, make one.
63
try
64             {
65                 AccountManager accManager = new AccountManager(xmppConnection);
66                 accManager.createAccount(username, password);
67             }
68             catch (XMPPException ex)
69             {
70                 // User probably already exists, throw away...
71
logger.info("*** account (" + username + ") already exists ***");
72             }
73
74             if (logger.isDebugEnabled())
75             {
76                 logger.debug("Logging in as: " + username);
77                 logger.debug("pw is : " + password);
78                 logger.debug("server : " + hostname);
79                 logger.debug("resource : " + resource);
80             }
81
82             if (resource == null)
83             {
84                 xmppConnection.login(username, password);
85             }
86             else
87             {
88                 xmppConnection.login(username, password, resource);
89             }
90         }
91         else
92         {
93             if (logger.isDebugEnabled())
94                 logger.debug("Already authenticated on this connection, no need to log in again.");
95         }
96         return xmppConnection;
97     }
98
99     /**
100      * Template method to perform any work when destroying the connectoe
101      */

102     protected void doDispose()
103     {
104         // template method
105
}
106
107     public boolean isRemoteSyncEnabled()
108     {
109         return true;
110     }
111 }
112
Popular Tags