1. 程式人生 > >smack+openfire自定義協議獲取當前使用者加入的群

smack+openfire自定義協議獲取當前使用者加入的群

                  </room>
              </rooms>
            </iq>
            */
            IQ responseIQ = new IQ(org.xmpp.packet.IQ.Type.result,iq.getID());
            responseIQ.setTo(fromJID);
            Element queryEle = responseIQ.setChildElement("rooms", "myjoinedroom");
            List<Map<String, String>> mucs = getMUCInfo(fromJID.toBareJID());
            if(mucs!=null && mucs.size()>0){
                Element itemEle = null;
                for (Map<String, String> map : mucs) {
                    itemEle=queryEle.addElement("room");
                    itemEle.addElement("serviceId").setText(map.get("serviceId"));
                    itemEle.addElement("name").setText(map.get("roomName"));
                    itemEle.addElement("roomid").setText( map.get("roomid"));
                    itemEle.addElement("nickName").setText( map.get("nickName"));
                    itemEle.addElement("memberJID").setText( map.get("memberJID"));
                }
            }
            
            server.getRoutingTable().routePacket(fromJID, responseIQ, true);
            System.out.println("響應給["+fromJID.toString()+"]群組:"+responseIQ.toXML());
        }        
    }
    
    public List<Map<String, String>> getMUCInfo(String jid) {  
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();  
        String sql = "select ofmucroom.serviceID, ofmucroom.name, ofmucroom.roomid ,ofmucmember.nickname,ofmucmember.jid from ofmucroom join ofmucmember on ofmucroom.roomID = ofmucmember.roomID and ofmucmember.jid = ?";  
        Connection connection = null;  
        PreparedStatement statement = null;  
        ResultSet resultSet = null;  
        Map<String, String> map = null;  
        try {  
            connection = DbConnectionManager.getConnection();  
            statement = connection.prepareStatement(sql);  
            statement.setString(1, jid);  
            resultSet = statement.executeQuery();  
            while (resultSet.next()) {
                map = new HashMap<String, String>();
                map.put("serviceId", resultSet.getString(1)==null?"":resultSet.getString(1));  
                map.put("roomName", resultSet.getString(2)==null?"":resultSet.getString(2));  
                map.put("roomid", resultSet.getString(3)==null?"":resultSet.getString(3));  
                map.put("nickName", resultSet.getString(4)==null?"":resultSet.getString(4));  
                map.put("memberJID", resultSet.getString(5)==null?"":resultSet.getString(5));  
                list.add(map);  
            }  
        } catch (Exception e1) {  
            e1.printStackTrace();
            log.error(e1.getMessage());
        } finally {  
            DbConnectionManager.closeConnection(resultSet, statement,  
                    connection);  
        }  
        return list;  
    }

    @Override
    public void destroyPlugin() {
        // TODO Auto-generated method stub
        
    }  
}