📄 leafnode.java
字号:
/** * $RCSfile: $ * $Revision: $ * $Date: $ * * Copyright (C) 2006 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.wildfire.pubsub;import org.dom4j.Element;import org.jivesoftware.util.LocaleUtils;import org.jivesoftware.util.StringUtils;import org.xmpp.forms.DataForm;import org.xmpp.forms.FormField;import org.xmpp.packet.JID;import org.xmpp.packet.Message;import org.xmpp.packet.IQ;import java.util.*;/** * A type of node that contains published items only. It is NOT a container for * other nodes. * * @author Matt Tucker */public class LeafNode extends Node { /** * Flag that indicates whether to persist items to storage. Note that when the * variable is false then the last published item is the only items being saved * to the backend storage. */ private boolean persistPublishedItems; /** * Maximum number of published items to persist. Note that all nodes are going to persist * their published items. The only difference is the number of the last published items * to be persisted. Even nodes that are configured to not use persitent items are going * to save the last published item. */ private int maxPublishedItems; /** * The maximum payload size in bytes. */ private int maxPayloadSize; /** * Flag that indicates whether to send items to new subscribers. */ private boolean sendItemSubscribe; /** * List of items that were published to the node and that are still active. If the node is * not configured to persist items then the last published item will be kept. The list is * sorted cronologically. */ protected final List<PublishedItem> publishedItems = new ArrayList<PublishedItem>(); protected Map<String, PublishedItem> itemsByID = new HashMap<String, PublishedItem>(); // TODO Add checking of max payload size. Return <not-acceptable> plus a application specific error condition of <payload-too-big/>. LeafNode(PubSubService service, CollectionNode parentNode, String nodeID, JID creator) { super(service, parentNode, nodeID, creator); // Configure node with default values (get them from the pubsub service) DefaultNodeConfiguration defaultConfiguration = service.getDefaultNodeConfiguration(true); this.persistPublishedItems = defaultConfiguration.isPersistPublishedItems(); this.maxPublishedItems = defaultConfiguration.getMaxPublishedItems(); this.maxPayloadSize = defaultConfiguration.getMaxPayloadSize(); this.sendItemSubscribe = defaultConfiguration.isSendItemSubscribe(); } void configure(FormField field) { List<String> values; String booleanValue; if ("pubsub#persist_items".equals(field.getVariable())) { values = field.getValues(); booleanValue = (values.size() > 0 ? values.get(0) : "1"); persistPublishedItems = "1".equals(booleanValue); } else if ("pubsub#max_payload_size".equals(field.getVariable())) { values = field.getValues(); maxPayloadSize = values.size() > 0 ? Integer.parseInt(values.get(0)) : 5120; } else if ("pubsub#send_item_subscribe".equals(field.getVariable())) { values = field.getValues(); booleanValue = (values.size() > 0 ? values.get(0) : "1"); sendItemSubscribe = "1".equals(booleanValue); } } void postConfigure(DataForm completedForm) { List<String> values; if (!persistPublishedItems) { // Always save the last published item when not configured to use persistent items maxPublishedItems = 1; } else { FormField field = completedForm.getField("pubsub#max_items"); if (field != null) { values = field.getValues(); maxPublishedItems = values.size() > 0 ? Integer.parseInt(values.get(0)) : 50; } } synchronized (publishedItems) { // Remove stored published items based on the new max items while (!publishedItems.isEmpty() && publishedItems.size() > maxPublishedItems) { PublishedItem removedItem = publishedItems.remove(0); itemsByID.remove(removedItem.getID()); // Add the removed item to the queue of items to delete from the database. The // queue is going to be processed by another thread service.getPubSubEngine().queueItemToRemove(removedItem); } } } protected void addFormFields(DataForm form, boolean isEditing) { super.addFormFields(form, isEditing); FormField formField = form.addField(); formField.setVariable("pubsub#send_item_subscribe"); if (isEditing) { formField.setType(FormField.Type.boolean_type); formField.setLabel( LocaleUtils.getLocalizedString("pubsub.form.conf.send_item_subscribe")); } formField.addValue(sendItemSubscribe); formField = form.addField(); formField.setVariable("pubsub#persist_items"); if (isEditing) { formField.setType(FormField.Type.boolean_type); formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.persist_items")); } formField.addValue(persistPublishedItems); formField = form.addField(); formField.setVariable("pubsub#max_items"); if (isEditing) { formField.setType(FormField.Type.text_single); formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.max_items")); } formField.addValue(maxPublishedItems); formField = form.addField(); formField.setVariable("pubsub#max_payload_size"); if (isEditing) { formField.setType(FormField.Type.text_single); formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.max_payload_size")); } formField.addValue(maxPayloadSize); } protected void deletingNode() { synchronized (publishedItems) { // Remove stored published items while (!publishedItems.isEmpty()) { PublishedItem removedItem = publishedItems.remove(0); itemsByID.remove(removedItem.getID()); // Add the removed item to the queue of items to delete from the database. The // queue is going to be processed by another thread service.getPubSubEngine().queueItemToRemove(removedItem); } } } void addPublishedItem(PublishedItem item) { synchronized (publishedItems) { publishedItems.add(item); itemsByID.put(item.getID(), item); } } public int getMaxPayloadSize() { return maxPayloadSize; } public boolean isPersistPublishedItems() { return persistPublishedItems; } public int getMaxPublishedItems() { return maxPublishedItems; } /** * Returns true if an item element is required to be included when publishing an * item to this node. When an item is included then the item will have an item ID * that will be included when sending items to node subscribers.<p> * * Leaf nodes that are transient and do not deliver payloads with event notifications * do not require an item element. If a user tries to publish an item to a node * that does not require items then an error will be returned. * * @return true if an item element is required to be included when publishing an * item to this node. */ public boolean isItemRequired() { return isPersistPublishedItems() || isPayloadDelivered(); } /** * Publishes the list of items to the node. Event notifications will be sent to subscribers * for the new published event. The published event may or may not include an item. When the * node is not persistent and does not require payloads then an item is not going to be created * nore included in the event notification.<p> * * When an affiliate has many subscriptions to the node, the affiliate will get a * notification for each set of items that affected the same list of subscriptions.<p> * * When an item is included in the published event then a new {@link PublishedItem} is * going to be created and added to the list of published item. Each published item will * have a unique ID in the node scope. The new published item will be added to the end * of the published list to keep the cronological order. When the max number of published * items is exceeded then the oldest published items will be removed.<p> * * For performance reasons the newly added published items and the deleted items (if any) * are saved to the database using a background thread. Sending event notifications to * node subscribers may also use another thread to ensure good performance.<p> * * @param publisher the full JID of the user that sent the new published event. * @param itemElements list of dom4j elements that contain info about the published items. */ public void publishItems(JID publisher, List<Element> itemElements) { List<PublishedItem> newPublishedItems = new ArrayList<PublishedItem>(); if (isItemRequired()) { String itemID; Element payload; PublishedItem newItem; for (Element item : itemElements) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -