KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > spi > PacketDelivererImpl


1 /**
2  * $RCSfile: PacketDelivererImpl.java,v $
3  * $Revision: 1.10 $
4  * $Date: 2005/06/24 04:58:50 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.spi;
13
14 import org.jivesoftware.messenger.*;
15 import org.jivesoftware.messenger.component.InternalComponentManager;
16 import org.jivesoftware.messenger.auth.UnauthorizedException;
17 import org.jivesoftware.messenger.container.BasicModule;
18 import org.jivesoftware.messenger.net.SocketPacketWriteHandler;
19 import org.xmpp.component.Component;
20 import org.xmpp.packet.JID;
21 import org.xmpp.packet.Packet;
22
23 /**
24  * In-memory implementation of the packet deliverer service
25  *
26  * @author Iain Shigeoka
27  */

28 public class PacketDelivererImpl extends BasicModule implements PacketDeliverer {
29
30     /**
31      * The handler that does the actual delivery (could be a channel instead)
32      */

33     protected SocketPacketWriteHandler deliverHandler;
34
35     private OfflineMessageStrategy messageStrategy;
36     private SessionManager sessionManager;
37
38     private InternalComponentManager componentManager;
39
40     public PacketDelivererImpl() {
41         super("Packet Delivery");
42         componentManager = InternalComponentManager.getInstance();
43     }
44
45     public void deliver(Packet packet) throws UnauthorizedException, PacketException {
46         if (packet == null) {
47             throw new PacketException("Packet was null");
48         }
49         if (deliverHandler == null) {
50             throw new PacketException("Could not send packet - no route" + packet.toString());
51         }
52         // Check if the target of the packet is the a component itself. If it does then just pass
53
// the packet to the component for processing it
54
JID recipient = packet.getTo();
55         if (recipient != null && recipient.getNode() == null && recipient.getResource() == null) {
56             // Check for registered components
57
Component component = componentManager.getComponent(recipient);
58             if (component != null) {
59                 component.processPacket(packet);
60                 return;
61             }
62         }
63         // Let the SocketPacketWriteHandler process the packet. SocketPacketWriteHandler may send
64
// it over the socket or store it when user is offline or drop it.
65
deliverHandler.process(packet);
66     }
67
68     public void initialize(XMPPServer server) {
69         super.initialize(server);
70         messageStrategy = server.getOfflineMessageStrategy();
71         sessionManager = server.getSessionManager();
72     }
73
74     public void start() throws IllegalStateException JavaDoc {
75         super.start();
76         deliverHandler =
77                 new SocketPacketWriteHandler(sessionManager,
78                         XMPPServer.getInstance().getRoutingTable(), messageStrategy);
79     }
80
81     public void stop() {
82         super.stop();
83         deliverHandler = null;
84     }
85 }
86
Popular Tags