⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 workgrouppresence.java

📁 openfire 服务器源码下载
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    // to the workgroup
                    for (JID tempSubscriber : listeners) {
                        sendPresence(tempSubscriber);
                    }
                }
                catch (Exception e) {
                    ComponentManagerFactory.getComponentManager().getLog().error(
                            "Error broadcasting available presence", e);
                }
            }
        });
    }

    /**
     * The workgroup is being destroyed so remove all the accepted presence subscriptions.
     */
    void workgroupDestroyed() {
        TaskEngine.getInstance().submit(new Runnable() {
            public void run() {
                try {
                    for (String bareJID : presenceSubscribers) {
                        // Cancel the previously granted subscription request
                        Presence reply = new Presence();
                        reply.setTo(bareJID);
                        reply.setFrom(workgroup.getJID());
                        reply.setType(Presence.Type.unsubscribed);
                        workgroup.send(reply);
                    }
                    // Delete the subscribers from the database
                    deleteRosterItems();
                }
                catch (Exception e) {
                    ComponentManagerFactory.getComponentManager().getLog().error(
                            "Error broadcasting available presence", e);
                }
            }
        });
    }

    private void agentToWorkgroup(Presence packet) throws AgentNotFoundException {
        String workgroupNode = workgroup.getJID().getNode();
        String resource = packet.getFrom().getResource();
        boolean usesAgentResource = workgroupNode.equalsIgnoreCase(resource);

        final JID sender = packet.getFrom();
        Agent agent = null;
        // Check if the sender of the Presence is an Agent otherwise return an error
        try {
            agent = workgroup.getAgentManager().getAgent(sender);
        }
        catch (AgentNotFoundException notFound) {
            if (usesAgentResource) {
                throw notFound;
            }
        }

        // Check if the presence includes the Workgroup extension
        boolean includesExtension = packet
                .getChildElement("agent-status", "http://jabber.org/protocol/workgroup") != null;
        AgentManager agentManager = workgroup.getAgentManager();


        if (agent != null && !agentManager.isInWorkgroup(agent, workgroup) && includesExtension) {
            throw new AgentNotFoundException();
        }

        if (agent == null || !agentManager.isInWorkgroup(agent, workgroup)) {
            if (packet.isAvailable()) {
                if (!sender.equals(workgroup.getJID())) {
                    // Add the user to the list of temporary subscribers. Since the
                    // user sent this presence using a directed presence the server
                    // will send an unavailable presence if the user gets disconnected.
                    // When an unavailable presence is received the subscription is
                    // going to be removed.
                    listeners.add(packet.getFrom());
                    sendPresence(packet.getFrom());
                }
            }
            else {
                listeners.remove(packet.getFrom());
            }
            return;
        }

        InterceptorManager interceptorManager = AgentInterceptorManager.getInstance();
        try {
            interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), packet, true, false);
            // Try to update the presence of the AgentSession with the new presence
            AgentSession agentSession = agent.getAgentSession();
            if (agentSession == null) {
                if (!includesExtension) {
                    // Ignote presence packet of agents that have not joined the workgroup
                    // and does not contain the proper extension
                    return;
                }

                // Add new agent session.
                agentSession = agent.createSession(packet.getFrom());
                if (agentSession == null) {
                    // User is not able to join since an existing session from another resource already exists
                    Presence reply = new Presence();
                    reply.setID(packet.getID());
                    reply.setTo(packet.getFrom());
                    reply.setFrom(packet.getTo());
                    reply.setError(PacketError.Condition.not_allowed);
                    workgroup.send(reply);
                    return;
                }
            }

            // Update session's presence with latest presence
            agentSession.updatePresence(packet);
            if (agentSession.getPresence().getType() == null) {
                agentSession.join(workgroup);
            }
            else {
                agentSession.depart(workgroup);
            }

            interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), packet, true, true);
        }
        catch (PacketRejectedException e) {
            workgroup.rejectPacket(packet, e);
        }
    }

    private void loadRosterItems() {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_ROSTER);
            pstmt.setLong(1, workgroup.getID());
            rs = pstmt.executeQuery();
            List<String> jids = new ArrayList<String>();
            while (rs.next()) {
                jids.add(rs.getString(1));
            }
            presenceSubscribers.addAll(jids);
        }
        catch (SQLException e) {
            ComponentManagerFactory.getComponentManager().getLog().error(
                    "Error loading workgroup roster items ", e);
        }
        finally {
            DbConnectionManager.closeConnection(rs, pstmt, con);
        }
    }

    private void createRosterItem(JID sender) throws SQLException {
        String bareJID = sender.toBareJID();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(CREATE_ROSTER_ITEM);
            pstmt.setLong(1, workgroup.getID());
            pstmt.setString(2, bareJID);
            pstmt.executeUpdate();
            // Add the bareJID of the user to the list of users that are tracking the
            // workgroup's presence
            presenceSubscribers.add(bareJID);
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
    }

    private void deleteRosterItem(JID sender) throws SQLException {
        String bareJID = sender.toBareJID();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_ROSTER_ITEM);
            pstmt.setLong(1, workgroup.getID());
            pstmt.setString(2, bareJID);
            pstmt.executeUpdate();
            // Remove the bareJID of the user from the list of users that are tracking the
            // workgroup's presence
            presenceSubscribers.remove(bareJID);
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
    }

    private void deleteRosterItems() throws SQLException {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_ROSTER_ITEMS);
            pstmt.setLong(1, workgroup.getID());
            pstmt.executeUpdate();
        }
        finally {
            DbConnectionManager.closeConnection(pstmt, con);
        }
    }

    private int getNumberOfAgentsOnline() {
        int onlineAgents = 0;
        WorkgroupManager workgroupManager = WorkgroupManager.getInstance();
        for (Workgroup workgroup : workgroupManager.getWorkgroups()) {
            onlineAgents += workgroup.getAgentSessions().size();
        }
        return onlineAgents;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -