/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.messenger.spi;

import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.messenger.*;

/**
 * Generic packet routing base class.
 *
 * @author Iain Shigeoka
 */
public class PacketRouterImpl extends BasicModule implements PacketRouter {
    public IQRouter iqRouter;
    public PresenceRouter presenceRouter;
    public MessageRouter messageRouter;

    /**
     * Initialize ComponentManager to handle delegation of packets.
     */
    private ComponentManager componentManager;

    /**
     * Create a packet router.
     */
    public PacketRouterImpl() {
        super("XMPP Packet Router");

        componentManager = ComponentManager.getInstance();
    }

    /**
     * Routes the given packet based on packet recipient and sender. The
     * router 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
     */
    public void route(XMPPPacket packet) {
         // Check for registered components
        Component component = componentManager.getComponent(packet.getRecipient().toBareStringPrep());
        if(component != null){
            component.processPacket(packet);
            return;
        }

        if (packet instanceof Message) {
            route((Message)packet);
        }
        else if (packet instanceof Presence) {
            route((Presence)packet);
        }
        else if (packet instanceof IQ) {
            route((IQ)packet);
        }
        else {
            throw new IllegalArgumentException();
        }
    }

    public void route(IQ packet) {
        iqRouter.route(packet);
    }

    public void route(Message packet) {
        messageRouter.route(packet);
    }

    public void route(Presence packet) {
        presenceRouter.route(packet);
    }

    protected TrackInfo getTrackInfo() {
        TrackInfo trackInfo = new TrackInfo();
        trackInfo.getTrackerClasses().put(IQRouter.class, "iqRouter");
        trackInfo.getTrackerClasses().put(MessageRouter.class, "messageRouter");
        trackInfo.getTrackerClasses().put(PresenceRouter.class, "presenceRouter");
        return trackInfo;
    }
}

@@ -1492,221 +1492,28 @@
     
- // Check for registered components%0A        Component component = componentManager.getComponent(packet.getRecipient().toBareStringPrep());%0A        if(component != null)%7B%0A            component.processPacket
+if(hasRouted
 (packet)
 ;%0A  
@@ -1504,25 +1504,26 @@
 uted(packet)
-;
+)%7B
 %0A           
@@ -1914,32 +1914,66 @@
 te(IQ packet) %7B%0A
+        if(!hasRouted(packet))%7B%0A  
         iqRouter
@@ -1980,32 +1980,42 @@
 .route(packet);%0A
+        %7D%0A
     %7D%0A%0A    publi
@@ -2037,32 +2037,67 @@
 ssage packet) %7B%0A
+        if(!hasRouted(packet))%7B%0A   
         messageR
@@ -2109,32 +2109,42 @@
 .route(packet);%0A
+        %7D%0A
     %7D%0A%0A    publi
@@ -2187,36 +2187,495 @@
     
-presenceRouter.route(packet)
+if(!hasRouted(packet))%7B%0A          presenceRouter.route(packet);%0A        %7D%0A    %7D%0A%0A    public boolean hasRouted(XMPPPacket packet)%7B%0A        if(packet.getRecipient() == null)%7B%0A            return false;%0A        %7D%0A         // Check for registered components%0A        Component component = componentManager.getComponent(packet.getRecipient().toBareStringPrep());%0A        if(component != null)%7B%0A            component.processPacket(packet);%0A            return true;%0A        %7D%0A        return false
 ;%0A  

Use component manager.