KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: XmppMessageReceiver.java 4219 2006-12-09 10:15:14Z lajos $
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.PacketListener;
14 import org.jivesoftware.smack.XMPPConnection;
15 import org.jivesoftware.smack.XMPPException;
16 import org.jivesoftware.smack.filter.PacketFilter;
17 import org.jivesoftware.smack.filter.PacketTypeFilter;
18 import org.jivesoftware.smack.packet.Message;
19 import org.jivesoftware.smack.packet.Packet;
20 import org.mule.config.i18n.Messages;
21 import org.mule.impl.MuleMessage;
22 import org.mule.impl.RequestContext;
23 import org.mule.providers.AbstractConnector;
24 import org.mule.providers.AbstractMessageReceiver;
25 import org.mule.providers.ConnectException;
26 import org.mule.umo.UMOComponent;
27 import org.mule.umo.UMOMessage;
28 import org.mule.umo.endpoint.UMOEndpoint;
29 import org.mule.umo.lifecycle.InitialisationException;
30 import org.mule.umo.provider.UMOMessageAdapter;
31
32 import javax.resource.spi.work.Work JavaDoc;
33 import javax.resource.spi.work.WorkException JavaDoc;
34 import javax.resource.spi.work.WorkManager JavaDoc;
35
36 /**
37  * <code>XmppMessageReceiver</code> is responsible for receiving Mule events over XMPP.
38  */

39 public class XmppMessageReceiver extends AbstractMessageReceiver implements PacketListener
40 {
41     private XMPPConnection xmppConnection = null;
42
43     public XmppMessageReceiver(AbstractConnector connector, UMOComponent component, UMOEndpoint endpoint)
44         throws InitialisationException
45     {
46         super(connector, component, endpoint);
47     }
48
49     public void doConnect() throws Exception JavaDoc
50     {
51         try
52         {
53             XmppConnector cnn = (XmppConnector)connector;
54             xmppConnection = cnn.createXmppConnection(endpoint.getEndpointURI());
55             if (endpoint.getFilter() instanceof PacketFilter)
56             {
57                 xmppConnection.addPacketListener(this, (PacketFilter)endpoint.getFilter());
58             }
59             else
60             {
61                 PacketFilter filter = new PacketTypeFilter(Message.class);
62                 xmppConnection.addPacketListener(this, filter);
63             }
64         }
65         catch (XMPPException e)
66         {
67             throw new ConnectException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X,
68                 "XMPP Connection"), e, this);
69         }
70     }
71
72     public void doDisconnect() throws Exception JavaDoc
73     {
74         if (xmppConnection != null)
75         {
76             xmppConnection.removePacketListener(this);
77             xmppConnection.close();
78         }
79     }
80
81     protected void doDispose()
82     {
83         logger.info("Closed Xmpp Listener");
84     }
85
86     protected Work JavaDoc createWork(Packet message)
87     {
88         return new XMPPWorker(message);
89     }
90
91     /**
92      * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet)
93      */

94     public void processPacket(Packet packet)
95     {
96         if (logger.isDebugEnabled())
97         {
98             logger.debug("processing packet: " + packet.toXML());
99         }
100
101         Work JavaDoc work = createWork(packet);
102         try
103         {
104             getWorkManager().scheduleWork(work, WorkManager.INDEFINITE, null, connector);
105         }
106         catch (WorkException JavaDoc e)
107         {
108             logger.error("Xmpp Server receiver work failed: " + e.getMessage(), e);
109         }
110     }
111
112     private class XMPPWorker implements Work JavaDoc
113     {
114         Packet packet = null;
115
116         public XMPPWorker(Packet message)
117         {
118             this.packet = message;
119         }
120
121         /**
122          * Accept requests from a given TCP port
123          */

124         public void run()
125         {
126             try
127             {
128                 UMOMessageAdapter adapter = connector.getMessageAdapter(packet);
129
130                 if (logger.isDebugEnabled())
131                 {
132                     logger.debug("Processing XMPP packet from: " + packet.getFrom());
133                     logger.debug("UMOMessageAdapter is a: " + adapter.getClass().getName());
134                 }
135
136                 UMOMessage returnMessage = routeMessage(new MuleMessage(adapter), endpoint.isSynchronous());
137
138                 if (returnMessage != null && packet instanceof Message)
139                 {
140                     RequestContext.rewriteEvent(returnMessage);
141                     Packet result = (Packet)connector.getDefaultResponseTransformer().transform(
142                         returnMessage.getPayload());
143                     xmppConnection.sendPacket(result);
144                 }
145             }
146             catch (Exception JavaDoc e)
147             {
148                 handleException(e);
149             }
150         }
151
152         public void release()
153         {
154             // template method
155
}
156     }
157 }
158
Popular Tags