📄 pubsubpersistencemanager.java
字号:
try { con = DbConnectionManager.getConnection(); if (create) { // Add the subscription of the user to the database pstmt = con.prepareStatement(ADD_SUBSCRIPTION); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.setString(3, subscription.getID()); pstmt.setString(4, subscription.getJID().toString()); pstmt.setString(5, subscription.getOwner().toString()); pstmt.setString(6, subscription.getState().name()); pstmt.setInt(7, (subscription.shouldDeliverNotifications() ? 1 : 0)); pstmt.setInt(8, (subscription.isUsingDigest() ? 1 : 0)); pstmt.setInt(9, subscription.getDigestFrequency()); Date expireDate = subscription.getExpire(); if (expireDate == null) { pstmt.setString(10, null); } else { pstmt.setString(10, StringUtils.dateToMillis(expireDate)); } pstmt.setInt(11, (subscription.isIncludingBody() ? 1 : 0)); pstmt.setString(12, encodeWithComma(subscription.getPresenceStates())); pstmt.setString(13, subscription.getType().name()); pstmt.setInt(14, subscription.getDepth()); pstmt.setString(15, subscription.getKeyword()); pstmt.executeUpdate(); // Indicate the subscription that is has been saved to the database subscription.setSavedToDB(true); } else { if (NodeSubscription.State.none == subscription.getState()) { // Remove the subscription of the user from the table pstmt = con.prepareStatement(DELETE_SUBSCRIPTION); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.setString(2, subscription.getID()); pstmt.executeUpdate(); } else { // Update the subscription of the user in the backend store pstmt = con.prepareStatement(UPDATE_SUBSCRIPTION); pstmt.setString(1, subscription.getOwner().toString()); pstmt.setString(2, subscription.getState().name()); pstmt.setInt(3, (subscription.shouldDeliverNotifications() ? 1 : 0)); pstmt.setInt(4, (subscription.isUsingDigest() ? 1 : 0)); pstmt.setInt(5, subscription.getDigestFrequency()); Date expireDate = subscription.getExpire(); if (expireDate == null) { pstmt.setString(6, null); } else { pstmt.setString(6, StringUtils.dateToMillis(expireDate)); } pstmt.setInt(7, (subscription.isIncludingBody() ? 1 : 0)); pstmt.setString(8, encodeWithComma(subscription.getPresenceStates())); pstmt.setString(9, subscription.getType().name()); pstmt.setInt(10, subscription.getDepth()); pstmt.setString(11, subscription.getKeyword()); pstmt.setString(12, service.getServiceID()); pstmt.setString(13, encodeNodeID(node.getNodeID())); pstmt.setString(14, subscription.getID()); pstmt.executeUpdate(); } } } catch (SQLException sqle) { Log.error(sqle); } finally { try {if (pstmt != null) {pstmt.close();}} catch (Exception e) {Log.error(e);} try {if (con != null) {con.close();}} catch (Exception e) {Log.error(e);} } } /** * Removes the subscription of the user from the DB. * * @param service The pubsub service that is hosting the node. * @param node The node where the user was subscribed to. * @param subscription The existing subsription of the user to the node. */ public static void removeSubscription(PubSubService service, Node node, NodeSubscription subscription) { Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); // Remove the affiliate from the table of node affiliates pstmt = con.prepareStatement(DELETE_SUBSCRIPTION); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.setString(3, subscription.getID()); pstmt.executeUpdate(); } catch (SQLException sqle) { Log.error(sqle); } finally { try {if (pstmt != null) {pstmt.close();}} catch (Exception e) {Log.error(e);} try {if (con != null) {con.close();}} catch (Exception e) {Log.error(e);} } } /** * Loads and adds the published items to the specified node. * * @param service the pubsub service that is hosting the node. * @param node the leaf node to load its published items. */ public static void loadItems(PubSubService service, LeafNode node) { Connection con = null; PreparedStatement pstmt = null; SAXReader xmlReader = null; try { // Get a sax reader from the pool xmlReader = xmlReaders.take(); con = DbConnectionManager.getConnection(); // Get published items of the specified node pstmt = con.prepareStatement(LOAD_ITEMS); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); ResultSet rs = pstmt.executeQuery(); // Rebuild loaded published items while(rs.next()) { String itemID = rs.getString(1); JID publisher = new JID(rs.getString(2)); Date creationDate = new Date(Long.parseLong(rs.getString(3).trim())); // Create the item PublishedItem item = new PublishedItem(node, publisher, itemID, creationDate); // Add the extra fields to the published item if (rs.getString(4) != null) { item.setPayload( xmlReader.read(new StringReader(rs.getString(4))).getRootElement()); } // Add the published item to the node node.addPublishedItem(item); } rs.close(); } catch (Exception sqle) { Log.error(sqle); } finally { // Return the sax reader to the pool if (xmlReader != null) { xmlReaders.add(xmlReader); } try { if (pstmt != null) pstmt.close(); } catch (Exception e) { Log.error(e); } try { if (con != null) con.close(); } catch (Exception e) { Log.error(e); } } } /** * Creates and stores the published item in the database. * * @param service the pubsub service that is hosting the node. * @param item The published item to save. * @return true if the item was successfully saved to the database. */ public static boolean createPublishedItem(PubSubService service, PublishedItem item) { boolean success = false; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); // Remove the published item from the database pstmt = con.prepareStatement(ADD_ITEM); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(item.getNode().getNodeID())); pstmt.setString(3, item.getID()); pstmt.setString(4, item.getPublisher().toString()); pstmt.setString(5, StringUtils.dateToMillis(item.getCreationDate())); pstmt.setString(6, item.getPayloadXML()); pstmt.executeUpdate(); // Set that the item was successfully saved to the database success = true; } catch (SQLException sqle) { Log.error(sqle); } finally { try {if (pstmt != null) {pstmt.close();}} catch (Exception e) {Log.error(e);} try {if (con != null) {con.close();}} catch (Exception e) {Log.error(e);} } return success; } /** * Removes the specified published item from the DB. * * @param service the pubsub service that is hosting the node. * @param item The published item to delete. * @return true if the item was successfully deleted from the database. */ public static boolean removePublishedItem(PubSubService service, PublishedItem item) { boolean success = false; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); // Remove the published item from the database pstmt = con.prepareStatement(DELETE_ITEM); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(item.getNode().getNodeID())); pstmt.setString(3, item.getID()); pstmt.executeUpdate(); // Set that the item was successfully deleted from the database success = true; } catch (SQLException sqle) { Log.error(sqle); } finally { try {if (pstmt != null) {pstmt.close();}} catch (Exception e) {Log.error(e);} try {if (con != null) {con.close();}} catch (Exception e) {Log.error(e);} } return success; } /** * Loads from the database the default node configuration for the specified node type * and pubsub service. * * @param service the default node configuration used by this pubsub service. * @param isLeafType true if loading default configuration for leaf nodes. * @return the loaded default node configuration for the specified node type and service * or <tt>null</tt> if none was found. */ public static DefaultNodeConfiguration loadDefaultConfiguration(PubSubService service, boolean isLeafType) { Connection con = null; PreparedStatement pstmt = null; DefaultNodeConfiguration config = null; try { con = DbConnectionManager.getConnection(); // Get default node configuration for the specified service pstmt = con.prepareStatement(LOAD_DEFAULT_CONF); pstmt.setString(1, service.getServiceID()); pstmt.setInt(2, (isLeafType ? 1 : 0)); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { config = new DefaultNodeConfiguration(isLeafType); // Rebuild loaded default node configuration config.setDeliverPayloads(rs.getInt(1) == 1); config.setMaxPayloadSize(rs.getInt(2)); config.setPersistPublishedItems(rs.getInt(3) == 1); config.setMaxPublishedItems(rs.getInt(4)); config.setNotifyConfigChanges(rs.getInt(5) == 1); config.setNotifyDelete(rs.getInt(6) == 1); config.setNotifyRetract(rs.getInt(7) == 1); config.setPresenceBasedDelivery(rs.getInt(8) == 1); config.setSendItemSubscribe(rs.getInt(9) == 1); config.setPublisherModel(PublisherModel.valueOf(rs.getString(10))); config.setSubscriptionEnabled(rs.getInt(11) == 1); config.setAccessModel(AccessModel.valueOf(rs.getString(12))); config.setLanguage(rs.getString(13)); if (rs.getString(14) != null) { config.setReplyPolicy(Node.ItemReplyPolicy.valueOf(rs.getString(14))); } config.setAssociationPolicy( CollectionNode.LeafNodeAssociationPolicy.valueOf(rs.getString(15))); config.setMaxLeafNodes(rs.getInt(16)); } rs.close(); } catch (Exception sqle) { Log.error(sqle); } finally { try { if (pstmt != null) pstmt.close(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -