1 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 ; 33 import javax.resource.spi.work.WorkException ; 34 import javax.resource.spi.work.WorkManager ; 35 36 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 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 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 createWork(Packet message) 87 { 88 return new XMPPWorker(message); 89 } 90 91 94 public void processPacket(Packet packet) 95 { 96 if (logger.isDebugEnabled()) 97 { 98 logger.debug("processing packet: " + packet.toXML()); 99 } 100 101 Work work = createWork(packet); 102 try 103 { 104 getWorkManager().scheduleWork(work, WorkManager.INDEFINITE, null, connector); 105 } 106 catch (WorkException e) 107 { 108 logger.error("Xmpp Server receiver work failed: " + e.getMessage(), e); 109 } 110 } 111 112 private class XMPPWorker implements Work 113 { 114 Packet packet = null; 115 116 public XMPPWorker(Packet message) 117 { 118 this.packet = message; 119 } 120 121 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 e) 147 { 148 handleException(e); 149 } 150 } 151 152 public void release() 153 { 154 } 156 } 157 } 158 | Popular Tags |