KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: XmppMessageDispatcher.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.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jivesoftware.smack.Chat;
16 import org.jivesoftware.smack.GroupChat;
17 import org.jivesoftware.smack.XMPPConnection;
18 import org.jivesoftware.smack.packet.Message;
19 import org.mule.impl.MuleMessage;
20 import org.mule.providers.AbstractMessageDispatcher;
21 import org.mule.umo.UMOEvent;
22 import org.mule.umo.UMOMessage;
23 import org.mule.umo.endpoint.MalformedEndpointException;
24 import org.mule.umo.endpoint.UMOEndpointURI;
25 import org.mule.umo.endpoint.UMOImmutableEndpoint;
26
27 /**
28  * <code>XmppMessageDispatcher</code> allows Mule events to be sent and received
29  * over Xmpp
30  *
31  * @author Peter Braswell
32  * @author Ross Mason
33  * @version $Revision: 3798 $
34  */

35
36 public class XmppMessageDispatcher extends AbstractMessageDispatcher
37 {
38     /**
39      * logger used by this class
40      */

41     protected static Log logger = LogFactory.getLog(XmppMessageDispatcher.class);
42
43     private XmppConnector connector;
44
45     private XMPPConnection xmppConnection = null;
46
47     private Chat chat;
48
49     private GroupChat groupChat;
50
51     public XmppMessageDispatcher(UMOImmutableEndpoint endpoint)
52     {
53         super(endpoint);
54         this.connector = (XmppConnector)endpoint.getConnector();
55     }
56
57     protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception JavaDoc
58     {
59         if (xmppConnection == null)
60         {
61             UMOEndpointURI uri = endpoint.getEndpointURI();
62             xmppConnection = connector.createXmppConnection(uri);
63         }
64     }
65
66     protected void doDisconnect() throws Exception JavaDoc
67     {
68         try
69         {
70             if (groupChat != null)
71             {
72                 groupChat.leave();
73             }
74             if (xmppConnection != null)
75             {
76                 xmppConnection.close();
77             }
78         }
79         finally
80         {
81             xmppConnection = null;
82         }
83     }
84
85     protected void doDispose()
86     {
87         // template method
88
}
89
90     protected void doDispatch(UMOEvent event) throws Exception JavaDoc
91     {
92         sendMessage(event);
93     }
94
95     protected UMOMessage doSend(UMOEvent event) throws Exception JavaDoc
96     {
97         sendMessage(event);
98         if (useRemoteSync(event))
99         {
100             Message response = null;
101             if (groupChat != null)
102             {
103                 response = groupChat.nextMessage(event.getTimeout());
104             }
105             else
106             {
107                 response = chat.nextMessage(event.getTimeout());
108             }
109
110             if (response != null)
111             {
112                 logger.debug("Got a response from chat: " + chat);
113                 return new MuleMessage(connector.getMessageAdapter(response));
114             }
115         }
116         return null;
117     }
118
119     protected void sendMessage(UMOEvent event) throws Exception JavaDoc
120     {
121         if (chat == null && groupChat == null)
122         {
123             UMOMessage msg = event.getMessage();
124             boolean group = msg.getBooleanProperty(XmppConnector.XMPP_GROUP_CHAT, false);
125             String JavaDoc nickname = msg.getStringProperty(XmppConnector.XMPP_NICKNAME, "mule");
126             String JavaDoc recipient = event.getEndpoint().getEndpointURI().getPath().substring(1);
127
128             if (group)
129             {
130                 groupChat = new GroupChat(xmppConnection, recipient);
131                 if (!groupChat.isJoined())
132                 {
133                     groupChat.join(nickname);
134                 }
135             }
136             else
137             {
138                 chat = new Chat(xmppConnection, recipient);
139             }
140         }
141         Object JavaDoc msgObj = event.getMessage().getPayload();
142         Message message;
143         // avoid duplicate transformation
144
if (!(msgObj instanceof Message))
145         {
146             message = (Message)event.getTransformedMessage();
147         }
148         else
149         {
150             message = (Message)msgObj;
151         }
152
153         if (logger.isTraceEnabled())
154         {
155             logger.trace("Transformed packet: " + message.toXML());
156         }
157
158         if (chat != null)
159         {
160             chat.sendMessage(message);
161         }
162         else
163         {
164             groupChat.sendMessage(message);
165         }
166         if (logger.isDebugEnabled())
167         {
168             logger.debug("packet successfully sent");
169         }
170     }
171
172     /**
173      * Make a specific request to the underlying transport
174      *
175      * @param endpoint the endpoint to use when connecting to the resource
176      * @param timeout the maximum time the operation should block before returning.
177      * The call should return immediately if there is data available. If
178      * no data becomes available before the timeout elapses, null will be
179      * returned
180      * @return the result of the request wrapped in a UMOMessage object. Null will be
181      * returned if no data was avaialable
182      * @throws Exception if the call to the underlying protocal cuases an exception
183      */

184     protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception JavaDoc
185     {
186
187         // Should be in the form of xmpp://user:pass@host:[port]/folder
188
String JavaDoc to = (String JavaDoc)endpoint.getProperty("folder");
189         if (to == null)
190         {
191             throw new MalformedEndpointException(endpoint.getEndpointURI().toString());
192         }
193         Chat chat = xmppConnection.createChat(to);
194         Message message = null;
195         if (timeout == UMOEvent.TIMEOUT_WAIT_FOREVER)
196         {
197             message = chat.nextMessage();
198         }
199         else if (timeout == UMOEvent.TIMEOUT_DO_NOT_WAIT)
200         {
201             message = chat.nextMessage(1);
202         }
203         else
204         {
205             message = chat.nextMessage(timeout);
206         }
207         if (message != null)
208         {
209             return new MuleMessage(connector.getMessageAdapter(message));
210         }
211         else
212         {
213             return null;
214         }
215     }
216
217     public Object JavaDoc getDelegateSession()
218     {
219         return null;
220     }
221 }
222
Popular Tags