InternalComponentManager.java 11.2 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 16 17
import org.dom4j.Element;
import org.xmpp.component.Component;
import org.xmpp.component.ComponentManager;
import org.xmpp.component.ComponentManagerFactory;
18
import org.xmpp.component.ComponentException;
19
import org.xmpp.packet.IQ;
20
import org.xmpp.packet.JID;
21
import org.xmpp.packet.Packet;
22
import org.xmpp.packet.Presence;
23
import org.jivesoftware.util.Log;
24
import org.jivesoftware.util.JiveGlobals;
Derek DeMoro's avatar
Derek DeMoro committed
25

26 27 28
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

29
/**
30
 * Manages the registration and delegation of Components. The ComponentManager
Matt Tucker's avatar
Matt Tucker committed
31
 * is responsible for managing registration and delegation of {@link Component Components},
32
 * as well as offering a facade around basic server functionallity such as sending and
33 34 35 36 37
 * receiving of packets.<p>
 *
 * This component manager will be an internal service whose JID will be component.[domain]. So the
 * component manager will be able to send packets to other internal or external components and also
 * receive packets from other components or even from trusted clients (e.g. ad-hoc commands).
38 39 40
 *
 * @author Derek DeMoro
 */
41
public class InternalComponentManager implements ComponentManager, RoutableChannelHandler {
42

43
    private Map<String, Component> components = new ConcurrentHashMap<String, Component>();
44
    private Map<JID, JID> presenceMap = new ConcurrentHashMap<JID, JID>();
Derek DeMoro's avatar
Derek DeMoro committed
45

46
    private static InternalComponentManager instance = new InternalComponentManager();
47 48 49 50 51 52 53 54 55
    /**
     * XMPP address of this internal service. The address is of the form: component.[domain]
     */
    private JID serviceAddress;
    /**
     * Holds the domain of the server. We are using an iv since we use this value many times
     * in many methods.
     */
    private String serverDomain;
Derek DeMoro's avatar
Derek DeMoro committed
56

57
    public static InternalComponentManager getInstance() {
58
        return instance;
Derek DeMoro's avatar
Derek DeMoro committed
59 60
    }

61 62 63 64
    public void start() {
        // Set this ComponentManager as the current component manager
        ComponentManagerFactory.setComponentManager(instance);

65 66 67 68
        XMPPServer server = XMPPServer.getInstance();
        serverDomain = server.getServerInfo().getName();
        // Set the address of this internal service. component.[domain]
        serviceAddress = new JID(null, "component." + serverDomain, null);
69 70 71 72
        if (!server.isSetupMode()) {
            // Add a route to this service
            server.getRoutingTable().addRoute(getAddress(), this);
        }
Derek DeMoro's avatar
Derek DeMoro committed
73 74
    }

75
    public void addComponent(String subdomain, Component component) throws ComponentException {
76 77
        components.put(subdomain, component);

78
        JID componentJID = new JID(subdomain + "." + serverDomain);
79

80
        // Add the route to the new service provided by the component
81 82
        XMPPServer.getInstance().getRoutingTable().addRoute(componentJID,
                new RoutableComponent(componentJID, component));
83

84 85 86 87 88 89 90 91 92 93 94 95
        // Initialize the new component
        try {
            component.initialize(componentJID, this);
            component.start();
        }
        catch (ComponentException e) {
            // Remove the route
            XMPPServer.getInstance().getRoutingTable().removeRoute(componentJID);
            // Rethrow the exception
            throw e;
        }

96 97
        // Check for potential interested users.
        checkPresences();
98 99 100
        // Send a disco#info request to the new component. If the component provides information
        // then it will be added to the list of discoverable server items.
        checkDiscoSupport(component, componentJID);
Derek DeMoro's avatar
Derek DeMoro committed
101 102
    }

103 104 105
    public void removeComponent(String subdomain) {
        components.remove(subdomain);

106
        JID componentJID = new JID(subdomain + "." + serverDomain);
107 108 109 110 111

        // Remove the route for the service provided by the component
        if (XMPPServer.getInstance().getRoutingTable() != null) {
            XMPPServer.getInstance().getRoutingTable().removeRoute(componentJID);
        }
112 113

        // Remove the disco item from the server for the component that is being removed
114 115 116
        if (XMPPServer.getInstance().getIQDiscoItemsHandler() != null) {
            XMPPServer.getInstance().getIQDiscoItemsHandler().removeComponentItem(componentJID.toBareJID());
        }
Derek DeMoro's avatar
Derek DeMoro committed
117 118
    }

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    public void sendPacket(Component component, Packet packet) {
        PacketRouter router;
        router = XMPPServer.getInstance().getPacketRouter();
        if (router != null) {
            router.route(packet);
        }
    }

    public String getProperty(String name) {
        return JiveGlobals.getProperty(name);
    }

    public void setProperty(String name, String value) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

135 136 137 138 139 140 141 142
    public String getServerName() {
        return serverDomain;
    }

    public String getHomeDirectory() {
        return JiveGlobals.getHomeDirectory();
    }

143 144 145 146
    public boolean isExternalMode() {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    public org.xmpp.component.Log getLog() {
        return new  org.xmpp.component.Log() {
            public void error(String msg) {
                Log.error(msg);
            }

            public void error(String msg, Throwable throwable) {
                Log.error(msg, throwable);
            }

            public void error(Throwable throwable) {
                Log.error(throwable);
            }

            public void warn(String msg) {
                Log.warn(msg);
            }

            public void warn(String msg, Throwable throwable) {
                Log.warn(msg, throwable);
            }

            public void warn(Throwable throwable) {
                Log.warn(throwable);
            }

            public void info(String msg) {
                Log.info(msg);
            }

            public void info(String msg, Throwable throwable) {
                Log.info(msg, throwable);
            }

            public void info(Throwable throwable) {
                Log.info(throwable);
            }

            public void debug(String msg) {
                Log.debug(msg);
            }

            public void debug(String msg, Throwable throwable) {
                Log.debug(msg, throwable);
            }

            public void debug(Throwable throwable) {
                Log.debug(throwable);
            }
        };
197 198
    }

199 200 201 202
    /**
     * Retrieves the <code>Component</code> which is mapped
     * to the specified JID.
     *
203
     * @param componentJID the jid mapped to the component.
Matt Tucker's avatar
Matt Tucker committed
204
     * @return the component with the specified id.
205
     */
206 207
    public Component getComponent(JID componentJID) {
        String jid = componentJID.toBareJID();
Matt Tucker's avatar
Matt Tucker committed
208 209
        if (components.containsKey(jid)) {
            return components.get(jid);
Derek DeMoro's avatar
Derek DeMoro committed
210 211
        }
        else {
Matt Tucker's avatar
Matt Tucker committed
212
            String serverName = new JID(jid).getDomain();
Derek DeMoro's avatar
Derek DeMoro committed
213
            int index = serverName.indexOf(".");
214
            if (index != -1) {
Derek DeMoro's avatar
Derek DeMoro committed
215 216 217 218
                String serviceName = serverName.substring(0, index);
                jid = serviceName;
            }
        }
Matt Tucker's avatar
Matt Tucker committed
219
        return components.get(jid);
Derek DeMoro's avatar
Derek DeMoro committed
220 221
    }

222 223 224 225 226 227 228 229 230 231 232
    /**
     * Retrieves the <code>Component</code> which is mapped
     * to the specified JID.
     *
     * @param jid the jid mapped to the component.
     * @return the component with the specified id.
     */
    public Component getComponent(String jid) {
        return getComponent(new JID(jid));
    }

233 234
    /**
     * Registers Probeers who have not yet been serviced.
235
     *
236 237 238
     * @param prober the jid probing.
     * @param probee the presence being probed.
     */
239
    public void addPresenceRequest(JID prober, JID probee) {
240 241 242 243
        presenceMap.put(prober, probee);
    }

    private void checkPresences() {
244 245
        for (JID prober : presenceMap.keySet()) {
            JID probee = presenceMap.get(prober);
246

247
            Component component = getComponent(probee.toBareJID());
248
            if (component != null) {
249 250 251
                Presence presence = new Presence();
                presence.setFrom(prober);
                presence.setTo(probee);
252
                component.processPacket(presence);
253

254 255
                // No reason to hold onto prober reference.
                presenceMap.remove(prober);
256 257 258
            }
        }
    }
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    /**
     *  Send a disco#info request to the new component. If the component provides information
     *  then it will be added to the list of discoverable server items.
     *
     * @param component the new component that was added to this manager.
     * @param componentJID the XMPP address of the new component.
     */
    private void checkDiscoSupport(Component component, JID componentJID) {
        // Build a disco#info request that will be sent to the component
        IQ iq = new IQ(IQ.Type.get);
        iq.setFrom(getAddress());
        iq.setTo(componentJID);
        iq.setChildElement("query", "http://jabber.org/protocol/disco#info");
        // Send the disco#info request to the component. The reply (if any) will be processed in
        // #process(Packet)
        sendPacket(component, iq);
    }

    public JID getAddress() {
        return serviceAddress;
    }

    /**
     * Processes packets that were sent to this service. Currently only packets that were sent from
     * registered components are being processed. In the future, we may also process packet of
     * trusted clients. Trusted clients may be able to execute ad-hoc commands such as adding or
     * removing components.
     *
     * @param packet the packet to process.
     */
290
    public void process(Packet packet) throws PacketException {
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
        Component component = getComponent(packet.getFrom().getDomain());
        // Only process packets that were sent by registered components
        if (component != null) {
            if (packet instanceof IQ && IQ.Type.result == ((IQ) packet).getType()) {
                IQ iq = (IQ) packet;
                Element childElement = iq.getChildElement();
                String namespace = null;
                if (childElement != null) {
                    namespace = childElement.getNamespaceURI();
                }
                if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
                    // Add a disco item to the server for the component that supports disco
                    XMPPServer.getInstance().getIQDiscoItemsHandler().addComponentItem(packet.getFrom()
                            .toBareJID(),
                            childElement.element("identity").attributeValue("name"));
                }
            }
        }
    }

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    /**
     * Exposes a Component as a RoutableChannelHandler.
     */
    public static class RoutableComponent implements RoutableChannelHandler {

        private JID jid;
        private Component component;

        public RoutableComponent(JID jid, Component component) {
            this.jid = jid;
            this.component = component;
        }

        public JID getAddress() {
            return jid;
        }

328
        public void process(Packet packet) throws PacketException {
329 330 331
            component.processPacket(packet);
        }
    }
332
}