📄 pubsubpersistencemanager.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.io.SAXReader;import org.jivesoftware.database.DbConnectionManager;import org.jivesoftware.openfire.pubsub.models.AccessModel;import org.jivesoftware.openfire.pubsub.models.PublisherModel;import org.jivesoftware.util.Log;import org.jivesoftware.util.StringUtils;import org.xmpp.packet.JID;import java.io.StringReader;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.*;import java.util.concurrent.BlockingQueue;import java.util.concurrent.LinkedBlockingQueue;/** * A manager responsible for ensuring node persistence. * * @author Matt Tucker */public class PubSubPersistenceManager { private static final String LOAD_NON_LEAF_NODES = "SELECT nodeID, leaf, creationDate, modificationDate, parent, deliverPayloads, " + "maxPayloadSize, persistItems, maxItems, notifyConfigChanges, notifyDelete, " + "notifyRetract, presenceBased, sendItemSubscribe, publisherModel, " + "subscriptionEnabled, configSubscription, accessModel, payloadType, " + "bodyXSLT, dataformXSLT, creator, description, language, name, " + "replyPolicy, associationPolicy, maxLeafNodes FROM pubsubNode " + "WHERE serviceID=? AND leaf=0 ORDER BY nodeID"; private static final String LOAD_LEAF_NODES = "SELECT nodeID, leaf, creationDate, modificationDate, parent, deliverPayloads, " + "maxPayloadSize, persistItems, maxItems, notifyConfigChanges, notifyDelete, " + "notifyRetract, presenceBased, sendItemSubscribe, publisherModel, " + "subscriptionEnabled, configSubscription, accessModel, payloadType, " + "bodyXSLT, dataformXSLT, creator, description, language, name, " + "replyPolicy, associationPolicy, maxLeafNodes FROM pubsubNode " + "WHERE serviceID=? AND leaf=1 ORDER BY nodeID"; private static final String UPDATE_NODE = "UPDATE pubsubNode SET modificationDate=?, parent=?, deliverPayloads=?, " + "maxPayloadSize=?, persistItems=?, maxItems=?, " + "notifyConfigChanges=?, notifyDelete=?, notifyRetract=?, presenceBased=?, " + "sendItemSubscribe=?, publisherModel=?, subscriptionEnabled=?, configSubscription=?, " + "accessModel=?, payloadType=?, bodyXSLT=?, dataformXSLT=?, description=?, " + "language=?, name=?, replyPolicy=?, associationPolicy=?, maxLeafNodes=? " + "WHERE serviceID=? AND nodeID=?"; private static final String ADD_NODE = "INSERT INTO pubsubNode (serviceID, nodeID, leaf, creationDate, modificationDate, " + "parent, deliverPayloads, maxPayloadSize, persistItems, maxItems, " + "notifyConfigChanges, notifyDelete, notifyRetract, presenceBased, " + "sendItemSubscribe, publisherModel, subscriptionEnabled, configSubscription, " + "accessModel, payloadType, bodyXSLT, dataformXSLT, creator, description, " + "language, name, replyPolicy, associationPolicy, maxLeafNodes) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; private static final String DELETE_NODE = "DELETE FROM pubsubNode WHERE serviceID=? AND nodeID=?"; private static final String LOAD_NODES_JIDS = "SELECT nodeID, jid, associationType FROM pubsubNodeJIDs WHERE serviceID=?"; private static final String ADD_NODE_JIDS = "INSERT INTO pubsubNodeJIDs (serviceID, nodeID, jid, associationType) " + "VALUES (?,?,?,?)"; private static final String DELETE_NODE_JIDS = "DELETE FROM pubsubNodeJIDs WHERE serviceID=? AND nodeID=?"; private static final String LOAD_NODES_GROUPS = "SELECT nodeID, rosterGroup FROM pubsubNodeGroups WHERE serviceID=?"; private static final String ADD_NODE_GROUPS = "INSERT INTO pubsubNodeGroups (serviceID, nodeID, rosterGroup) " + "VALUES (?,?,?)"; private static final String DELETE_NODE_GROUPS = "DELETE FROM pubsubNodeGroups WHERE serviceID=? AND nodeID=?"; private static final String LOAD_AFFILIATIONS = "SELECT nodeID,jid,affiliation FROM pubsubAffiliation WHERE serviceID=? " + "ORDER BY nodeID"; private static final String ADD_AFFILIATION = "INSERT INTO pubsubAffiliation (serviceID,nodeID,jid,affiliation) VALUES (?,?,?,?)"; private static final String UPDATE_AFFILIATION = "UPDATE pubsubAffiliation SET affiliation=? WHERE serviceID=? AND nodeID=? AND jid=?"; private static final String DELETE_AFFILIATION = "DELETE FROM pubsubAffiliation WHERE serviceID=? AND nodeID=? AND jid=?"; private static final String DELETE_AFFILIATIONS = "DELETE FROM pubsubAffiliation WHERE serviceID=? AND nodeID=?"; private static final String LOAD_SUBSCRIPTIONS = "SELECT nodeID, id, jid, owner, state, deliver, digest, digest_frequency, " + "expire, includeBody, showValues, subscriptionType, subscriptionDepth, " + "keyword FROM pubsubSubscription WHERE serviceID=? ORDER BY nodeID"; private static final String ADD_SUBSCRIPTION = "INSERT INTO pubsubSubscription (serviceID, nodeID, id, jid, owner, state, " + "deliver, digest, digest_frequency, expire, includeBody, showValues, " + "subscriptionType, subscriptionDepth, keyword) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; private static final String UPDATE_SUBSCRIPTION = "UPDATE pubsubSubscription SET owner=?, state=?, deliver=?, digest=?, " + "digest_frequency=?, expire=?, includeBody=?, showValues=?, subscriptionType=?, " + "subscriptionDepth=?, keyword=? WHERE serviceID=? AND nodeID=? AND id=?"; private static final String DELETE_SUBSCRIPTION = "DELETE FROM pubsubSubscription WHERE serviceID=? AND nodeID=? AND id=?"; private static final String DELETE_SUBSCRIPTIONS = "DELETE FROM pubsubSubscription WHERE serviceID=? AND nodeID=?"; private static final String LOAD_ALL_ITEMS = "SELECT id,jid,creationDate,payload,nodeID FROM pubsubItem " + "WHERE serviceID=? ORDER BY creationDate"; private static final String LOAD_ITEMS = "SELECT id,jid,creationDate,payload FROM pubsubItem " + "WHERE serviceID=? AND nodeID=? ORDER BY creationDate"; private static final String ADD_ITEM = "INSERT INTO pubsubItem (serviceID,nodeID,id,jid,creationDate,payload) " + "VALUES (?,?,?,?,?,?)"; private static final String DELETE_ITEM = "DELETE FROM pubsubItem WHERE serviceID=? AND nodeID=? AND id=?"; private static final String DELETE_ITEMS = "DELETE FROM pubsubItem WHERE serviceID=? AND nodeID=?"; private static final String LOAD_DEFAULT_CONF = "SELECT deliverPayloads, maxPayloadSize, persistItems, maxItems, " + "notifyConfigChanges, notifyDelete, notifyRetract, presenceBased, " + "sendItemSubscribe, publisherModel, subscriptionEnabled, accessModel, language, " + "replyPolicy, associationPolicy, maxLeafNodes " + "FROM pubsubDefaultConf WHERE serviceID=? AND leaf=?"; private static final String UPDATE_DEFAULT_CONF = "UPDATE pubsubDefaultConf SET deliverPayloads=?, maxPayloadSize=?, persistItems=?, " + "maxItems=?, notifyConfigChanges=?, notifyDelete=?, notifyRetract=?, " + "presenceBased=?, sendItemSubscribe=?, publisherModel=?, subscriptionEnabled=?, " + "accessModel=?, language=? replyPolicy=?, associationPolicy=?, maxLeafNodes=? " + "WHERE serviceID=? AND leaf=?"; private static final String ADD_DEFAULT_CONF = "INSERT INTO pubsubDefaultConf (serviceID, leaf, deliverPayloads, maxPayloadSize, " + "persistItems, maxItems, notifyConfigChanges, notifyDelete, notifyRetract, " + "presenceBased, sendItemSubscribe, publisherModel, subscriptionEnabled, " + "accessModel, language, replyPolicy, associationPolicy, maxLeafNodes) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; /** * Pool of SAX Readers. SAXReader is not thread safe so we need to have a pool of readers. */ private static BlockingQueue<SAXReader> xmlReaders = new LinkedBlockingQueue<SAXReader>(); static { // Initialize the pool of sax readers for (int i=0; i<50; i++) { SAXReader xmlReader = new SAXReader(); xmlReader.setEncoding("UTF-8"); xmlReaders.add(xmlReader); } } /** * Creates and stores the node configuration in the database. * * @param service The pubsub service that is hosting the node. * @param node The newly created node. */ public static void createNode(PubSubService service, Node node) { Connection con = null; PreparedStatement pstmt = null; boolean abortTransaction = false; try { con = DbConnectionManager.getTransactionConnection(); pstmt = con.prepareStatement(ADD_NODE); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.setInt(3, (node.isCollectionNode() ? 0 : 1)); pstmt.setString(4, StringUtils.dateToMillis(node.getCreationDate())); pstmt.setString(5, StringUtils.dateToMillis(node.getModificationDate())); pstmt.setString(6, node.getParent() != null ? encodeNodeID(node.getParent().getNodeID()) : null); pstmt.setInt(7, (node.isPayloadDelivered() ? 1 : 0)); if (!node.isCollectionNode()) { pstmt.setInt(8, ((LeafNode) node).getMaxPayloadSize()); pstmt.setInt(9, (((LeafNode) node).isPersistPublishedItems() ? 1 : 0)); pstmt.setInt(10, ((LeafNode) node).getMaxPublishedItems()); } else { pstmt.setInt(8, 0); pstmt.setInt(9, 0); pstmt.setInt(10, 0); } pstmt.setInt(11, (node.isNotifiedOfConfigChanges() ? 1 : 0)); pstmt.setInt(12, (node.isNotifiedOfDelete() ? 1 : 0)); pstmt.setInt(13, (node.isNotifiedOfRetract() ? 1 : 0)); pstmt.setInt(14, (node.isPresenceBasedDelivery() ? 1 : 0)); pstmt.setInt(15, (node.isSendItemSubscribe() ? 1 : 0)); pstmt.setString(16, node.getPublisherModel().getName()); pstmt.setInt(17, (node.isSubscriptionEnabled() ? 1 : 0)); pstmt.setInt(18, (node.isSubscriptionConfigurationRequired() ? 1 : 0)); pstmt.setString(19, node.getAccessModel().getName()); pstmt.setString(20, node.getPayloadType()); pstmt.setString(21, node.getBodyXSLT()); pstmt.setString(22, node.getDataformXSLT()); pstmt.setString(23, node.getCreator().toString()); pstmt.setString(24, node.getDescription()); pstmt.setString(25, node.getLanguage()); pstmt.setString(26, node.getName()); if (node.getReplyPolicy() != null) { pstmt.setString(27, node.getReplyPolicy().name()); } else { pstmt.setString(27, null); } if (node.isCollectionNode()) { pstmt.setString(28, ((CollectionNode)node).getAssociationPolicy().name()); pstmt.setInt(29, ((CollectionNode)node).getMaxLeafNodes()); } else { pstmt.setString(28, null); pstmt.setInt(29, 0); } pstmt.executeUpdate(); // Save associated JIDs and roster groups saveAssociatedElements(con, node, service); } catch (SQLException sqle) { Log.error(sqle); abortTransaction = true; } finally { try {if (pstmt != null) {pstmt.close();}} catch (Exception e) {Log.error(e);} DbConnectionManager.closeTransactionConnection(con, abortTransaction); } } /** * Updates the node configuration in the database. * * @param service The pubsub service that is hosting the node. * @param node The updated node. */ public static void updateNode(PubSubService service, Node node) { Connection con = null; PreparedStatement pstmt = null; boolean abortTransaction = false; try { con = DbConnectionManager.getTransactionConnection(); pstmt = con.prepareStatement(UPDATE_NODE); pstmt.setString(1, StringUtils.dateToMillis(node.getModificationDate())); pstmt.setString(2, node.getParent() != null ? encodeNodeID(node.getParent().getNodeID()) : null); pstmt.setInt(3, (node.isPayloadDelivered() ? 1 : 0)); if (!node.isCollectionNode()) { pstmt.setInt(4, ((LeafNode) node).getMaxPayloadSize()); pstmt.setInt(5, (((LeafNode) node).isPersistPublishedItems() ? 1 : 0)); pstmt.setInt(6, ((LeafNode) node).getMaxPublishedItems()); } else { pstmt.setInt(4, 0); pstmt.setInt(5, 0); pstmt.setInt(6, 0); } pstmt.setInt(7, (node.isNotifiedOfConfigChanges() ? 1 : 0)); pstmt.setInt(8, (node.isNotifiedOfDelete() ? 1 : 0)); pstmt.setInt(9, (node.isNotifiedOfRetract() ? 1 : 0)); pstmt.setInt(10, (node.isPresenceBasedDelivery() ? 1 : 0)); pstmt.setInt(11, (node.isSendItemSubscribe() ? 1 : 0)); pstmt.setString(12, node.getPublisherModel().getName()); pstmt.setInt(13, (node.isSubscriptionEnabled() ? 1 : 0)); pstmt.setInt(14, (node.isSubscriptionConfigurationRequired() ? 1 : 0)); pstmt.setString(15, node.getAccessModel().getName()); pstmt.setString(16, node.getPayloadType()); pstmt.setString(17, node.getBodyXSLT()); pstmt.setString(18, node.getDataformXSLT()); pstmt.setString(19, node.getDescription());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -