001: /*
002: * $Id: XmppConnector.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.xmpp;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.endpoint.EndpointURI;
015: import org.mule.api.lifecycle.InitialisationException;
016: import org.mule.transport.AbstractConnector;
017:
018: import org.jivesoftware.smack.AccountManager;
019: import org.jivesoftware.smack.XMPPConnection;
020: import org.jivesoftware.smack.XMPPException;
021:
022: /**
023: * <code>XmppConnector</code> TODO
024: */
025: public class XmppConnector extends AbstractConnector {
026:
027: public static final String XMPP = "xmpp";
028: public static final String XMPP_PROPERTY_PREFIX = "";
029: public static final String XMPP_RESOURCE = XMPP_PROPERTY_PREFIX
030: + "resource";
031: public static final String XMPP_SUBJECT = XMPP_PROPERTY_PREFIX
032: + "subject";
033: public static final String XMPP_THREAD = XMPP_PROPERTY_PREFIX
034: + "thread";
035: public static final String XMPP_TO = XMPP_PROPERTY_PREFIX + "to";
036: public static final String XMPP_FROM = XMPP_PROPERTY_PREFIX
037: + "from";
038: public static final String XMPP_GROUP_CHAT = XMPP_PROPERTY_PREFIX
039: + "groupChat";
040: public static final String XMPP_NICKNAME = XMPP_PROPERTY_PREFIX
041: + "nickname";
042:
043: protected void doInitialise() throws InitialisationException {
044: // template method, nothing to do
045: }
046:
047: protected void doDispose() {
048: // template method
049: }
050:
051: protected void doConnect() throws Exception {
052: // template method
053: }
054:
055: protected void doDisconnect() throws Exception {
056: // template method
057: }
058:
059: protected void doStart() throws MuleException {
060: // template method
061: }
062:
063: protected void doStop() throws MuleException {
064: // template method
065: }
066:
067: public String getProtocol() {
068: return XMPP;
069: }
070:
071: public XMPPConnection createXmppConnection(EndpointURI endpointURI)
072: throws XMPPException {
073: logger.info("Trying to find XMPP connection for uri: "
074: + endpointURI);
075:
076: String username = endpointURI.getUser();
077: String hostname = endpointURI.getHost();
078: String password = endpointURI.getPassword();
079: String resource = (String) endpointURI.getParams().get(
080: "resource");
081:
082: XMPPConnection xmppConnection = this
083: .doCreateXmppConnection(endpointURI);
084:
085: if (!xmppConnection.isAuthenticated()) {
086: // Make sure we have an account. If we don't, make one.
087: try {
088: AccountManager accManager = new AccountManager(
089: xmppConnection);
090: accManager.createAccount(username, password);
091: } catch (XMPPException ex) {
092: // User probably already exists, throw away...
093: logger.info("*** account (" + username
094: + ") already exists ***");
095: }
096:
097: if (logger.isDebugEnabled()) {
098: logger.debug("Logging in as: " + username);
099: logger.debug("pw is : " + password);
100: logger.debug("server : " + hostname);
101: logger.debug("resource : " + resource);
102: }
103:
104: if (resource == null) {
105: xmppConnection.login(username, password);
106: } else {
107: xmppConnection.login(username, password, resource);
108: }
109: } else {
110: if (logger.isDebugEnabled()) {
111: logger
112: .debug("Already authenticated on this connection, no need to log in again.");
113: }
114: }
115: return xmppConnection;
116: }
117:
118: /**
119: * This method creates and returns the {@link XMPPConnection} object that's uses to talk to
120: * the Jabber server.
121: * <p/>
122: * Subclasses can override this method to create a more specialized {@link XMPPConnection}.
123: *
124: * @param endpointURI
125: * @return
126: * @throws XMPPException
127: */
128: protected XMPPConnection doCreateXmppConnection(
129: EndpointURI endpointURI) throws XMPPException {
130: XMPPConnection xmppConnection = null;
131:
132: if (endpointURI.getPort() != -1) {
133: xmppConnection = new XMPPConnection(endpointURI.getHost(),
134: endpointURI.getPort());
135: } else {
136: xmppConnection = new XMPPConnection(endpointURI.getHost());
137: }
138:
139: return xmppConnection;
140: }
141:
142: public boolean isRemoteSyncEnabled() {
143: return true;
144: }
145: }
|