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

📄 cmssessionmanager.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(message)) {
            // don't broadcast empty messages
            return;
        }
        // create the broadcast
        CmsBroadcast broadcast = new CmsBroadcast(cms.getRequestContext().currentUser(), message);
        // send the broadcast to all authenticated sessions
        synchronized (m_sessions) {
            Iterator i = getConcurrentSessionIterator();
            while (i.hasNext()) {
                CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(i.next());
                if (sessionInfo != null) {
                    // double check for concurrent modification
                    sessionInfo.getBroadcastQueue().add(broadcast);
                }
            }
        }
    }

    /**
     * Sends a broadcast to all sessions of a given user.<p>
     * 
     * @param cms the OpenCms user context of the user sending the broadcast
     * 
     * @param message the message to broadcast
     * @param user the target (reciever) of the broadcast
     */
    public void sendBroadcast(CmsObject cms, String message, CmsUser user) {

        if (CmsStringUtil.isEmptyOrWhitespaceOnly(message)) {
            // don't broadcast empty messages
            return;
        }
        // create the broadcast
        CmsBroadcast broadcast = new CmsBroadcast(cms.getRequestContext().currentUser(), message);
        List userSessions = getSessionInfos(user.getId());
        Iterator i = userSessions.iterator();
        // send the broadcast to all sessions of the selected user
        synchronized (m_sessions) {
            while (i.hasNext()) {
                CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(i.next());
                if (sessionInfo != null) {
                    // double check for concurrent modification
                    sessionInfo.getBroadcastQueue().add(broadcast);
                }
            }
        }
    }

    /**
     * Sends a broadcast to the specified user session.<p>
     * 
     * @param cms the OpenCms user context of the user sending the broadcast
     * 
     * @param message the message to broadcast
     * @param sessionId the session id target (reciever) of the broadcast
     */
    public void sendBroadcast(CmsObject cms, String message, String sessionId) {

        if (CmsStringUtil.isEmptyOrWhitespaceOnly(message)) {
            // don't broadcast empty messages
            return;
        }
        // send the broadcast only to the selected session
        CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(sessionId);
        if (sessionInfo != null) {
            // double check for concurrent modification
            sessionInfo.getBroadcastQueue().add(new CmsBroadcast(cms.getRequestContext().currentUser(), message));
        }
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {

        StringBuffer output = new StringBuffer();
        synchronized (m_sessions) {
            Iterator i = getConcurrentSessionIterator();
            output.append("[CmsSessions]:\n");
            while (i.hasNext()) {
                String key = (String)i.next();
                CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(key);
                output.append(key);
                output.append(" : ");
                output.append(sessionInfo.getUser().toString());
                output.append('\n');
            }
        }
        return output.toString();
    }

    /**
     * Adds a new session info into the session storage.<p>
     *
     * @param sessionInfo the session info to store for the id
     */
    protected void addSessionInfo(CmsSessionInfo sessionInfo) {

        m_sessions.put(sessionInfo.getSessionId(), sessionInfo);
    }

    /**
     * Called by the {@link OpenCmsListener} when a http session is created.<p>
     * 
     * @param event the http session event
     * 
     * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
     * @see OpenCmsListener#sessionCreated(HttpSessionEvent)
     */
    protected synchronized void sessionCreated(HttpSessionEvent event) {

        m_sessionCountCurrent = (m_sessionCountCurrent <= 0) ? 1 : (m_sessionCountCurrent + 1);
        m_sessionCountTotal++;
        if (LOG.isInfoEnabled()) {
            LOG.info(Messages.get().getBundle().key(
                Messages.LOG_SESSION_CREATED_2,
                new Integer(m_sessionCountTotal),
                new Integer(m_sessionCountCurrent)));
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_SESSION_CREATED_1, event.getSession().getId()));
        }
    }

    /**
     * Called by the {@link OpenCmsListener} when a http session is destroyed.<p>
     * 
     * @param event the http session event
     * 
     * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
     * @see OpenCmsListener#sessionDestroyed(HttpSessionEvent)
     */
    protected synchronized void sessionDestroyed(HttpSessionEvent event) {

        m_sessionCountCurrent = (m_sessionCountCurrent <= 0) ? 0 : (m_sessionCountCurrent - 1);

        String sessionId = event.getSession().getId();
        // remove the session for the session info storage
        CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(sessionId);
        CmsUUID userId = CmsUUID.getNullUUID();
        if (sessionInfo != null) {
            userId = sessionInfo.getUser().getId();
        }
        synchronized (m_sessions) {
            m_sessions.remove(sessionId);
        }
        if (!userId.isNullUUID() && getSessionInfos(userId).size() == 0) {
            // remove the temporary locks of this user from memory
            OpenCms.getLockManager().removeTempLocks(userId);
        }

        if (LOG.isInfoEnabled()) {
            LOG.info(Messages.get().getBundle().key(
                Messages.LOG_SESSION_DESTROYED_2,
                new Integer(m_sessionCountTotal),
                new Integer(m_sessionCountCurrent)));
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_SESSION_DESTROYED_1, event.getSession().getId()));
        }
    }

    /**
     * Validates the sessions stored in this manager and removes 
     * any sessions that have become invalidated.<p>
     */
    protected void validateSessionInfos() {

        synchronized (m_sessions) {
            Iterator i = getConcurrentSessionIterator();
            while (i.hasNext()) {
                String sessionId = (String)i.next();
                CmsSessionInfo sessionInfo = (CmsSessionInfo)m_sessions.get(sessionId);
                if (sessionInfo != null) {
                    // may be the case in case of concurrent modification
                    if (sessionInfo.isExpired()) {
                        // session is invalid, try to remove it
                        try {
                            m_sessions.remove(sessionId);
                        } catch (ConcurrentModificationException ex) {
                            // ignore, better luck next time...
                        }
                    }
                }
            }
        }
    }

    /**
     * Returns an iterator of a copy of the keyset of the current session map,
     * which should be used for iterators to avoid concurrent modification exceptions.<p>
     * 
     * @return an iterator of the keyset of the current session map 
     */
    private Iterator getConcurrentSessionIterator() {

        Set keySet;
        int count = 0;
        do {
            keySet = new HashSet(m_sessions.size());
            try {
                keySet.addAll(m_sessions.keySet());
            } catch (ConcurrentModificationException e) {
                // problem creating a copy of the keyset, try up to 5 times 
                count++;
                keySet = null;
            }
        } while ((keySet == null) && (count < 5));
        if (keySet == null) {
            // no success, so we return an empty set to avoid problems
            keySet = new HashSet();
        }
        return keySet.iterator();
    }
}

⌨️ 快捷键说明

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