pubsubpersistencemanager.java
来自「基于Jabber协议的即时消息服务器」· Java 代码 · 共 1,384 行 · 第 1/5 页
JAVA
1,384 行
else { pstmt.setString(22, null); } if (node.isCollectionNode()) { pstmt.setString(23, ((CollectionNode) node).getAssociationPolicy().name()); pstmt.setInt(24, ((CollectionNode) node).getMaxLeafNodes()); } else { pstmt.setString(23, null); pstmt.setInt(24, 0); } pstmt.setString(25, service.getServiceID()); pstmt.setString(26, encodeNodeID(node.getNodeID())); pstmt.executeUpdate(); // Remove existing JIDs associated with the the node pstmt = con.prepareStatement(DELETE_NODE_JIDS); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.executeUpdate(); pstmt.close(); // Remove roster groups associated with the the node being deleted pstmt = con.prepareStatement(DELETE_NODE_GROUPS); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.executeUpdate(); pstmt.close(); pstmt = null; // 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); } } private static void saveAssociatedElements(Connection con, Node node, PubSubService service) throws SQLException { // Add new JIDs associated with the the node PreparedStatement pstmt = con.prepareStatement(ADD_NODE_JIDS); try { for (JID jid : node.getContacts()) { pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.setString(3, jid.toString()); pstmt.setString(4, "contacts"); pstmt.executeUpdate(); } for (JID jid : node.getReplyRooms()) { pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.setString(3, jid.toString()); pstmt.setString(4, "replyRooms"); pstmt.executeUpdate(); } for (JID jid : node.getReplyTo()) { pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.setString(3, jid.toString()); pstmt.setString(4, "replyTo"); pstmt.executeUpdate(); } if (node.isCollectionNode()) { for (JID jid : ((CollectionNode) node).getAssociationTrusted()) { pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.setString(3, jid.toString()); pstmt.setString(4, "associationTrusted"); pstmt.executeUpdate(); } } pstmt.close(); // Add new roster groups associated with the the node pstmt = con.prepareStatement(ADD_NODE_GROUPS); for (String groupName : node.getRosterGroupsAllowed()) { pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.setString(3, groupName); pstmt.executeUpdate(); } } finally { pstmt.close(); } } /** * Removes the specified node from the DB. * * @param service The pubsub service that is hosting the node. * @param node The node that is being deleted. * @return true If the operation was successful. */ public static boolean removeNode(PubSubService service, Node node) { Connection con = null; PreparedStatement pstmt = null; boolean abortTransaction = false; try { con = DbConnectionManager.getTransactionConnection(); // Remove the affiliate from the table of node affiliates pstmt = con.prepareStatement(DELETE_NODE); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.executeUpdate(); pstmt.close(); // Remove JIDs associated with the the node being deleted pstmt = con.prepareStatement(DELETE_NODE_JIDS); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.executeUpdate(); pstmt.close(); // Remove roster groups associated with the the node being deleted pstmt = con.prepareStatement(DELETE_NODE_GROUPS); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.executeUpdate(); pstmt.close(); // Remove published items of the node being deleted pstmt = con.prepareStatement(DELETE_ITEMS); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.executeUpdate(); pstmt.close(); // Remove all affiliates from the table of node affiliates pstmt = con.prepareStatement(DELETE_AFFILIATIONS); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.executeUpdate(); pstmt.close(); // Remove users that were subscribed to the node pstmt = con.prepareStatement(DELETE_SUBSCRIPTIONS); pstmt.setString(1, service.getServiceID()); pstmt.setString(2, encodeNodeID(node.getNodeID())); pstmt.executeUpdate(); } 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); } return !abortTransaction; } /** * Loads all nodes from the database and adds them to the PubSub service. * * @param service the pubsub service that is hosting the nodes. */ public static void loadNodes(PubSubService service) { Connection con = null; PreparedStatement pstmt = null; Map<String, Node> nodes = new HashMap<String, Node>(); try { con = DbConnectionManager.getConnection(); // Get all nodes at once (with 1 query) pstmt = con.prepareStatement(LOAD_NODES); pstmt.setString(1, service.getServiceID()); ResultSet rs = pstmt.executeQuery(); // Rebuild all loaded nodes while(rs.next()) { loadNode(service, nodes, rs); } rs.close(); pstmt.close(); // Get JIDs associated with all nodes pstmt = con.prepareStatement(LOAD_NODES_JIDS); pstmt.setString(1, service.getServiceID()); rs = pstmt.executeQuery(); // Add to each node the associated JIDs while(rs.next()) { loadAssociatedJIDs(nodes, rs); } rs.close(); pstmt.close(); // Get roster groups associateds with all nodes pstmt = con.prepareStatement(LOAD_NODES_GROUPS); pstmt.setString(1, service.getServiceID()); rs = pstmt.executeQuery(); // Add to each node the associated Groups while(rs.next()) { loadAssociatedGroups(nodes, rs); } rs.close(); pstmt.close(); // Get affiliations of all nodes pstmt = con.prepareStatement(LOAD_AFFILIATIONS); pstmt.setString(1, service.getServiceID()); rs = pstmt.executeQuery(); // Add to each node the correspondiding affiliates while(rs.next()) { loadAffiliations(nodes, rs); } rs.close(); pstmt.close(); // Get subscriptions to all nodes pstmt = con.prepareStatement(LOAD_SUBSCRIPTIONS); pstmt.setString(1, service.getServiceID()); rs = pstmt.executeQuery(); // Add to each node the correspondiding subscriptions while(rs.next()) { loadSubscriptions(service, nodes, rs); } rs.close(); // TODO We may need to optimize memory consumption and load items on-demand // Load published items of all nodes pstmt = con.prepareStatement(LOAD_ALL_ITEMS); pstmt.setString(1, service.getServiceID()); rs = pstmt.executeQuery(); // Add to each node the correspondiding subscriptions while(rs.next()) { loadItems(nodes, rs); } rs.close(); } 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); } } for (Node node : nodes.values()) { // Set now that the node is persistent in the database. Note: We need to // set this now since otherwise the node's affiliations will be saved to the database // "again" while adding them to the node! node.setSavedToDB(true); // Add the node to the service service.addNode(node); } } private static void loadNode(PubSubService service, Map<String, Node> loadedNodes, ResultSet rs) { Node node; try { String nodeID = decodeNodeID(rs.getString(1)); boolean leaf = rs.getInt(2) == 1; String parent = decodeNodeID(rs.getString(5)); JID creator = new JID(rs.getString(22)); CollectionNode parentNode = null; if (parent != null) { // Check if the parent has already been loaded parentNode = (CollectionNode) loadedNodes.get(parent); if (parentNode == null) { // Parent is not in memory so try to load it Log.warn("Node not loaded due to missing parent. NodeID: " + nodeID); return; } } if (leaf) { // Retrieving a leaf node
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?