PacketTransporterImpl.java 2.8 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10 11
 */

Matt Tucker's avatar
Matt Tucker committed
12
package org.jivesoftware.messenger.spi;
Matt Tucker's avatar
Matt Tucker committed
13 14 15 16 17 18

import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.transport.TransportHandler;
Derek DeMoro's avatar
Derek DeMoro committed
19
import org.xmpp.packet.Packet;
Matt Tucker's avatar
Matt Tucker committed
20 21 22 23 24 25

/**
 * In-memory implementation of the packet transporter service.
 *
 * @author Iain Shigeoka
 */
Derek DeMoro's avatar
Derek DeMoro committed
26
public class PacketTransporterImpl extends BasicModule  {
Matt Tucker's avatar
Matt Tucker committed
27 28 29 30

    /**
     * The handler that does the actual delivery (could be a channel instead)
     */
31
    private TransportHandler transportHandler;
Matt Tucker's avatar
Matt Tucker committed
32 33 34 35

    /**
     * deliverer for xmpp server
     */
36
    private PacketDeliverer deliverer;
Matt Tucker's avatar
Matt Tucker committed
37 38 39 40

    /**
     * xmpp server
     */
41
    private XMPPServer xmppServer;
Matt Tucker's avatar
Matt Tucker committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    /**
     * This is a singleton, you can't create one. Be very careful not to do anything
     * that refers back to the factory's create method. Do initialization in the init()
     * method if at all possible.
     */
    public PacketTransporterImpl() {
        super("XMPP Packet Transporter");
    }

    /**
     * Obtain the transport handler that this transporter uses for delivering
     * transport packets.
     *
     * @return The transport handler instance used by this transporter
     */
    public TransportHandler getTransportHandler() {
        return transportHandler;
    }

    /**
     * Delivers the given packet based on packet recipient and sender. The
     * deliverer defers actual routing decisions to other classes.
     * <h2>Warning</h2>
     * Be careful to enforce concurrency DbC of concurrent by synchronizing
     * any accesses to class resources.
     *
     * @param packet The packet to route
     * @throws NullPointerException If the packet is null or the
     *                              packet could not be routed
     */
Derek DeMoro's avatar
Derek DeMoro committed
73
    public void deliver(Packet packet) throws UnauthorizedException, PacketException {
Matt Tucker's avatar
Matt Tucker committed
74 75 76 77
        if (packet == null) {
            throw new NullPointerException();
        }

Derek DeMoro's avatar
Derek DeMoro committed
78
        if (xmppServer != null && xmppServer.isLocal(packet.getTo())) {
Matt Tucker's avatar
Matt Tucker committed
79 80 81 82 83 84 85 86 87 88 89
            deliverer.deliver(packet);
        }
        else if (transportHandler != null) {
            transportHandler.process(packet);
        }
        else {
            Log.warn("Could not deliver message: no deliverer available "
                    + packet.toString());
        }
    }

90 91 92 93 94
    public void initialize(XMPPServer server) {
        super.initialize(server);
        xmppServer = server;
        deliverer = server.getPacketDeliverer();
        transportHandler = server.getTransportHandler();
Matt Tucker's avatar
Matt Tucker committed
95 96
    }
}