ComponentManager.java 3.96 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11
/**
 * $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.
 */

Derek DeMoro's avatar
Derek DeMoro committed
12 13
package org.jivesoftware.messenger;

14 15
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
16
import org.xmpp.packet.Packet;
17 18
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
Derek DeMoro's avatar
Derek DeMoro committed
19

20
/**
21
 * Manages the registration and delegation of Components. The ComponentManager
Matt Tucker's avatar
Matt Tucker committed
22
 * is responsible for managing registration and delegation of {@link Component Components},
23 24
 * as well as offering a facade around basic server functionallity such as sending and
 * receiving of packets.
25 26 27
 *
 * @author Derek DeMoro
 */
Derek DeMoro's avatar
Derek DeMoro committed
28
public class ComponentManager {
29

30
    private Map<String, Component> components = new ConcurrentHashMap<String, Component>();
31
    private Map<JID, JID> presenceMap = new ConcurrentHashMap<JID, JID>();
Derek DeMoro's avatar
Derek DeMoro committed
32

33
    private static ComponentManager instance = new ComponentManager();
Derek DeMoro's avatar
Derek DeMoro committed
34 35 36 37 38 39 40


    /**
     * Returns the singleton instance of <CODE>ComponentManager</CODE>,
     * creating it if necessary.
     * <p/>
     *
41
     * @return the singleton instance of <Code>ComponentManager</CODE>
Derek DeMoro's avatar
Derek DeMoro committed
42 43
     */
    public static ComponentManager getInstance() {
44
        return instance;
Derek DeMoro's avatar
Derek DeMoro committed
45 46 47 48 49
    }

    private ComponentManager() {
    }

50 51 52 53 54 55 56 57
    /**
     * Registers a <code>Component</code> with the server and maps
     * to particular jid.
     *
     * @param jid       the jid to map to.
     * @param component the <code>Component</code> to register.
     */
    public void addComponent(String jid, Component component) {
Matt Tucker's avatar
Matt Tucker committed
58
        jid = new JID(jid).toBareJID();
Derek DeMoro's avatar
Derek DeMoro committed
59
        components.put(jid, component);
60 61 62

        // Check for potential interested users.
        checkPresences();
Derek DeMoro's avatar
Derek DeMoro committed
63 64
    }

65 66 67 68 69 70
    /**
     * Removes a <code>Component</code> from the server.
     *
     * @param jid the jid mapped to the particular component.
     */
    public void removeComponent(String jid) {
Matt Tucker's avatar
Matt Tucker committed
71
        components.remove(new JID(jid).toBareJID());
Derek DeMoro's avatar
Derek DeMoro committed
72 73
    }

74 75 76 77 78
    /**
     * Retrieves the <code>Component</code> which is mapped
     * to the specified JID.
     *
     * @param jid the jid mapped to the component.
Matt Tucker's avatar
Matt Tucker committed
79
     * @return the component with the specified id.
80 81
     */
    public Component getComponent(String jid) {
Matt Tucker's avatar
Matt Tucker committed
82 83 84
        jid = new JID(jid).toBareJID();
        if (components.containsKey(jid)) {
            return components.get(jid);
Derek DeMoro's avatar
Derek DeMoro committed
85 86
        }
        else {
Matt Tucker's avatar
Matt Tucker committed
87
            String serverName = new JID(jid).getDomain();
Derek DeMoro's avatar
Derek DeMoro committed
88
            int index = serverName.indexOf(".");
89
            if (index != -1) {
Derek DeMoro's avatar
Derek DeMoro committed
90 91 92 93
                String serviceName = serverName.substring(0, index);
                jid = serviceName;
            }
        }
Matt Tucker's avatar
Matt Tucker committed
94
        return components.get(jid);
Derek DeMoro's avatar
Derek DeMoro committed
95 96
    }

97 98
    /**
     * Registers Probeers who have not yet been serviced.
99
     *
100 101 102
     * @param prober the jid probing.
     * @param probee the presence being probed.
     */
103
    public void addPresenceRequest(JID prober, JID probee) {
104 105 106 107 108 109
        presenceMap.put(prober, probee);
    }

    /**
     * Send a packet to the specified recipient. Please note that this sends packets only
     * to outgoing jids and does to the incoming server reader.
110
     *
111 112
     * @param packet the packet to send.
     */
113
    public void sendPacket(Packet packet) {
114
        PacketRouter router;
115
        router = XMPPServer.getInstance().getPacketRouter();
116 117
        if (router != null) {
            router.route(packet);
118 119 120 121
        }
    }

    private void checkPresences() {
122 123
        for (JID prober : presenceMap.keySet()) {
            JID probee = presenceMap.get(prober);
124

125
            Component component = getComponent(probee.toBareJID());
126
            if (component != null) {
127 128 129
                Presence presence = new Presence();
                presence.setFrom(prober);
                presence.setTo(probee);
130
                component.processPacket(presence);
131 132 133

                // No reason to hold onto prober reference.
                presenceMap.remove(prober);
134 135 136
            }
        }
    }
137
}