📄 pubsubmodule.java
字号:
/** * $RCSfile: $ * $Revision: $ * $Date: $ * * Copyright (C) 2008 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, or a commercial license * agreement with Jive. */package org.jivesoftware.openfire.pubsub;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.jivesoftware.openfire.PacketRouter;import org.jivesoftware.openfire.RoutableChannelHandler;import org.jivesoftware.openfire.RoutingTable;import org.jivesoftware.openfire.XMPPServer;import org.jivesoftware.openfire.cluster.ClusterEventListener;import org.jivesoftware.openfire.cluster.ClusterManager;import org.jivesoftware.openfire.commands.AdHocCommandManager;import org.jivesoftware.openfire.component.InternalComponentManager;import org.jivesoftware.openfire.container.BasicModule;import org.jivesoftware.openfire.disco.*;import org.jivesoftware.openfire.forms.DataForm;import org.jivesoftware.openfire.forms.spi.XDataFormImpl;import org.jivesoftware.openfire.forms.spi.XFormFieldImpl;import org.jivesoftware.openfire.pubsub.models.AccessModel;import org.jivesoftware.openfire.pubsub.models.PublisherModel;import org.jivesoftware.util.*;import org.xmpp.packet.*;import java.util.*;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.CopyOnWriteArrayList;import java.util.concurrent.LinkedBlockingQueue;/** * Module that implements JEP-60: Publish-Subscribe. By default node collections and * instant nodes are supported. * * @author Matt Tucker */public class PubSubModule extends BasicModule implements ServerItemsProvider, DiscoInfoProvider, DiscoItemsProvider, RoutableChannelHandler, PubSubService, ClusterEventListener, PropertyEventListener { /** * the chat service's hostname */ private String serviceName = null; /** * Collection node that acts as the root node of the entire node hierarchy. */ private CollectionNode rootCollectionNode = null; /** * Nodes managed by this manager, table: key nodeID (String); value Node */ private Map<String, Node> nodes = new ConcurrentHashMap<String, Node>(); /** * Keep a registry of the presence's show value of users that subscribed to a node of * the pubsub service and for which the node only delivers notifications for online users * or node subscriptions deliver events based on the user presence show value. Offline * users will not have an entry in the map. Note: Key-> bare JID and Value-> Map whose key * is full JID of connected resource and value is show value of the last received presence. */ private Map<String, Map<String, String>> barePresences = new ConcurrentHashMap<String, Map<String, String>>(); /** * Queue that holds the items that need to be added to the database. */ private Queue<PublishedItem> itemsToAdd = new LinkedBlockingQueue<PublishedItem>(); /** * Queue that holds the items that need to be deleted from the database. */ private Queue<PublishedItem> itemsToDelete = new LinkedBlockingQueue<PublishedItem>(); /** * Manager that keeps the list of ad-hoc commands and processing command requests. */ private AdHocCommandManager manager; /** * The time to elapse between each execution of the maintenance process. Default * is 2 minutes. */ private int items_task_timeout = 2 * 60 * 1000; /** * Task that saves or deletes published items from the database. */ private PublishedItemTask publishedItemTask; /** * Timer to save published items to the database or remove deleted or old items. */ private Timer timer = new Timer("PubSub maintenance"); /** * Returns the permission policy for creating nodes. A true value means that not anyone can * create a node, only the JIDs listed in <code>allowedToCreate</code> are allowed to create * nodes. */ private boolean nodeCreationRestricted = false; /** * Flag that indicates if a user may have more than one subscription with the node. When multiple * subscriptions is enabled each subscription request, event notification and unsubscription request * should include a subid attribute. */ private boolean multipleSubscriptionsEnabled = true; /** * Bare jids of users that are allowed to create nodes. An empty list means that anyone can * create nodes. */ private Collection<String> allowedToCreate = new CopyOnWriteArrayList<String>(); /** * Bare jids of users that are system administrators of the PubSub service. A sysadmin * has the same permissions as a node owner. */ private Collection<String> sysadmins = new CopyOnWriteArrayList<String>(); /** * The packet router for the server. */ private PacketRouter router = null; private RoutingTable routingTable = null; /** * Default configuration to use for newly created leaf nodes. */ private DefaultNodeConfiguration leafDefaultConfiguration; /** * Default configuration to use for newly created collection nodes. */ private DefaultNodeConfiguration collectionDefaultConfiguration; /** * Private component that actually performs the pubsub work. */ private PubSubEngine engine = null; /** * Flag that indicates if the service is enabled. */ private boolean serviceEnabled = true; public PubSubModule() { super("Publish Subscribe Service"); // Initialize the ad-hoc commands manager to use for this pubsub service manager = new AdHocCommandManager(); manager.addCommand(new PendingSubscriptionsCommand(this)); // Save or delete published items from the database every 2 minutes starting in // 2 minutes (default values) publishedItemTask = new PublishedItemTask(this); timer.schedule(publishedItemTask, items_task_timeout, items_task_timeout); } public void process(Packet packet) { // TODO Remove this method when moving PubSub as a component and removing module code // The MUC service will receive all the packets whose domain matches the domain of the MUC // service. This means that, for instance, a disco request should be responded by the // service itself instead of relying on the server to handle the request. try { // Check if the packet is a disco request or a packet with namespace iq:register if (packet instanceof IQ) { if (!engine.process(this, (IQ) packet)) { process((IQ) packet); } } else if (packet instanceof Presence) { engine.process(this, (Presence) packet); } else { engine.process(this, (Message) packet); } } catch (Exception e) { Log.error(LocaleUtils.getLocalizedString("admin.error"), e); if (packet instanceof IQ) { // Send internal server error IQ reply = IQ.createResultIQ((IQ) packet); reply.setError(PacketError.Condition.internal_server_error); send(reply); } } } private void process(IQ iq) { // Ignore IQs of type ERROR if (IQ.Type.error == iq.getType()) { return; } Element childElement = iq.getChildElement(); String namespace = null; if (childElement != null) { namespace = childElement.getNamespaceURI(); } if ("http://jabber.org/protocol/disco#info".equals(namespace)) { // TODO PubSub should have an IQDiscoInfoHandler of its own when PubSub becomes // a component IQ reply = XMPPServer.getInstance().getIQDiscoInfoHandler().handleIQ(iq); router.route(reply); } else if ("http://jabber.org/protocol/disco#items".equals(namespace)) { // TODO PubSub should have an IQDiscoItemsHandler of its own when PubSub becomes // a component IQ reply = XMPPServer.getInstance().getIQDiscoItemsHandler().handleIQ(iq); router.route(reply); } else { // Unknown namespace requested so return error to sender engine.sendErrorPacket(iq, PacketError.Condition.service_unavailable, null); } } public String getServiceID() { return "pubsub"; } public boolean canCreateNode(JID creator) { // Node creation is always allowed for sysadmin if (isNodeCreationRestricted() && !isServiceAdmin(creator)) { // The user is not allowed to create nodes return false; } return true; } public boolean isServiceAdmin(JID user) { return sysadmins.contains(user.toBareJID()) || allowedToCreate.contains(user.toBareJID()) || InternalComponentManager.getInstance().hasComponent(user); } public boolean isInstantNodeSupported() { return true; } public boolean isCollectionNodesSupported() { return true; } public CollectionNode getRootCollectionNode() { return rootCollectionNode; } public DefaultNodeConfiguration getDefaultNodeConfiguration(boolean leafType) { if (leafType) { return leafDefaultConfiguration; } return collectionDefaultConfiguration; } public Collection<String> getShowPresences(JID subscriber) { return PubSubEngine.getShowPresences(this, subscriber); } public void presenceSubscriptionNotRequired(Node node, JID user) { PubSubEngine.presenceSubscriptionNotRequired(this, node, user); } public void presenceSubscriptionRequired(Node node, JID user) { PubSubEngine.presenceSubscriptionRequired(this, node, user); } public void queueItemToAdd(PublishedItem newItem) { PubSubEngine.queueItemToAdd(this, newItem); } public void queueItemToRemove(PublishedItem removedItem) { PubSubEngine.queueItemToRemove(this, removedItem); } public String getServiceName() { return serviceName; } public String getServiceDomain() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -