KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile: PacketTransporterImpl.java,v $
3  * $Revision: 1.4 $
4  * $Date: 2004/12/05 05:37:06 $
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.container.BasicModule;
15 import org.jivesoftware.util.Log;
16 import org.jivesoftware.messenger.*;
17 import org.jivesoftware.messenger.auth.UnauthorizedException;
18 import org.jivesoftware.messenger.transport.TransportHandler;
19 import org.xmpp.packet.Packet;
20
21 /**
22  * In-memory implementation of the packet transporter service.
23  *
24  * @author Iain Shigeoka
25  */

26 public class PacketTransporterImpl extends BasicModule {
27
28     /**
29      * The handler that does the actual delivery (could be a channel instead)
30      */

31     private TransportHandler transportHandler;
32
33     /**
34      * deliverer for xmpp server
35      */

36     private PacketDeliverer deliverer;
37
38     /**
39      * xmpp server
40      */

41     private XMPPServer xmppServer;
42
43     /**
44      * This is a singleton, you can't create one. Be very careful not to do anything
45      * that refers back to the factory's create method. Do initialization in the init()
46      * method if at all possible.
47      */

48     public PacketTransporterImpl() {
49         super("XMPP Packet Transporter");
50     }
51
52     /**
53      * Obtain the transport handler that this transporter uses for delivering
54      * transport packets.
55      *
56      * @return The transport handler instance used by this transporter
57      */

58     public TransportHandler getTransportHandler() {
59         return transportHandler;
60     }
61
62     /**
63      * Delivers the given packet based on packet recipient and sender. The
64      * deliverer defers actual routing decisions to other classes.
65      * <h2>Warning</h2>
66      * Be careful to enforce concurrency DbC of concurrent by synchronizing
67      * any accesses to class resources.
68      *
69      * @param packet The packet to route
70      * @throws NullPointerException If the packet is null or the
71      * packet could not be routed
72      */

73     public void deliver(Packet packet) throws UnauthorizedException, PacketException {
74         if (packet == null) {
75             throw new NullPointerException JavaDoc();
76         }
77
78         if (xmppServer != null && xmppServer.isLocal(packet.getTo())) {
79             deliverer.deliver(packet);
80         }
81         else if (transportHandler != null) {
82             transportHandler.process(packet);
83         }
84         else {
85             Log.warn("Could not deliver message: no deliverer available "
86                     + packet.toString());
87         }
88     }
89
90     public void initialize(XMPPServer server) {
91         super.initialize(server);
92         xmppServer = server;
93         deliverer = server.getPacketDeliverer();
94         transportHandler = server.getTransportHandler();
95     }
96 }
97
Popular Tags