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

📄 cmsmemorymonitor.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

        // temporary xml entities cache
        LRUMap xmlTemporaryCache = new LRUMap(128);
        m_xmlTemporaryEntityCache = Collections.synchronizedMap(xmlTemporaryCache);
        register(CmsXmlEntityResolver.class.getName() + ".xmlEntityTemporaryCache", m_xmlTemporaryEntityCache);

        // permanent xml entities cache
        Map xmlPermanentCache = new HashMap(32);
        m_xmlPermanentEntityCache = Collections.synchronizedMap(xmlPermanentCache);
        register(CmsXmlEntityResolver.class.getName() + ".xmlEntityPermanentCache", m_xmlPermanentEntityCache);

        // xml content definitions cache
        LRUMap contentDefinitionsCache = new LRUMap(64);
        m_contentDefinitionsCache = Collections.synchronizedMap(contentDefinitionsCache);
        register(CmsXmlEntityResolver.class.getName() + ".contentDefinitionsCache", m_contentDefinitionsCache);

        // lock cache
        Map lockCache = new HashMap();
        m_lockCache = Collections.synchronizedMap(lockCache);
        register(CmsLockManager.class.getName(), lockCache);

        // locale cache
        Map map = new HashMap();
        m_localeCache = Collections.synchronizedMap(map);
        register(CmsLocaleManager.class.getName(), map);

        // permissions cache
        LRUMap lruMap = new LRUMap(cacheSettings.getPermissionCacheSize());
        m_permissionCache = Collections.synchronizedMap(lruMap);
        register(CmsSecurityManager.class.getName(), lruMap);

        // user cache
        lruMap = new LRUMap(cacheSettings.getUserCacheSize());
        m_userCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".userCache", lruMap);

        // group cache
        lruMap = new LRUMap(cacheSettings.getGroupCacheSize());
        m_groupCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".groupCache", lruMap);

        // organizational unit cache
        lruMap = new LRUMap(cacheSettings.getOrgUnitCacheSize());
        m_orgUnitCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".orgUnitCache", lruMap);

        // user groups list cache
        lruMap = new LRUMap(cacheSettings.getUserGroupsCacheSize());
        m_userGroupsCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".userGroupsCache", lruMap);

        // project cache
        lruMap = new LRUMap(cacheSettings.getProjectCacheSize());
        m_projectCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".projectCache", lruMap);

        // project resources cache cache
        lruMap = new LRUMap(cacheSettings.getProjectResourcesCacheSize());
        m_projectResourcesCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".projectResourcesCache", lruMap);

        // publish history
        int size = configuration.getPublishManager().getPublishHistorySize();
        Buffer buffer = CmsPublishHistory.getQueue(size);
        m_publishHistory = SynchronizedBuffer.decorate(buffer);
        register(CmsPublishHistory.class.getName() + ".publishHistory", buffer);

        // publish queue
        buffer = CmsPublishQueue.getQueue();
        m_publishQueue = SynchronizedBuffer.decorate(buffer);
        register(CmsPublishQueue.class.getName() + ".publishQueue", buffer);

        // resource cache
        lruMap = new LRUMap(cacheSettings.getResourceCacheSize());
        m_resourceCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".resourceCache", lruMap);

        // roles cache
        lruMap = new LRUMap(cacheSettings.getRolesCacheSize());
        m_rolesCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".rolesCache", lruMap);

        // role lists cache
        lruMap = new LRUMap(cacheSettings.getRolesCacheSize());
        m_roleListsCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".roleListsCache", lruMap);

        // resource list cache
        lruMap = new LRUMap(cacheSettings.getResourcelistCacheSize());
        m_resourceListCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".resourceListCache", lruMap);

        // property cache
        lruMap = new LRUMap(cacheSettings.getPropertyCacheSize());
        m_propertyCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".propertyCache", lruMap);

        // property list cache
        lruMap = new LRUMap(cacheSettings.getPropertyListsCacheSize());
        m_propertyListCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".propertyListCache", lruMap);

        // acl cache
        lruMap = new LRUMap(cacheSettings.getAclCacheSize());
        m_accessControlListCache = Collections.synchronizedMap(lruMap);
        register(CmsDriverManager.class.getName() + ".accessControlListCache", lruMap);

        // vfs object cache
        Map vfsObjectCache = new HashMap();
        m_vfsObjectCache = Collections.synchronizedMap(vfsObjectCache);
        register(CmsVfsMemoryObjectCache.class.getName(), vfsObjectCache);

        // memory object cache
        Map memObjectCache = new HashMap();
        m_memObjectCache = Collections.synchronizedMap(memObjectCache);
        register(CmsMemoryObjectCache.class.getName(), memObjectCache);

        if (LOG.isDebugEnabled()) {
            // this will happen only once during system startup
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_MM_CREATED_1, new Date(System.currentTimeMillis())));
        }
    }

    /**
     * Checks if there is a registered monitored object with the given key.<p>
     * 
     * @param key the key to look for
     * 
     * @return <code>true</code> if there is a registered monitored object with the given key
     */
    public boolean isMonitoring(String key) {

        return (m_monitoredObjects.get(key) != null);
    }

    /**
     * @see org.opencms.scheduler.I_CmsScheduledJob#launch(CmsObject, Map)
     */
    public String launch(CmsObject cms, Map parameters) throws Exception {

        CmsMemoryMonitor monitor = OpenCms.getMemoryMonitor();

        // make sure job is not launched twice
        if (m_currentlyRunning) {
            return null;
        }

        try {
            m_currentlyRunning = true;

            // update the memory status
            monitor.updateStatus();

            // check if the system is in a low memory condition
            if (monitor.lowMemory()) {
                // log warning
                monitor.monitorWriteLog(true);
                // send warning email
                monitor.monitorSendEmail(true);
                // clean up caches     
                monitor.clearCaches();
            }

            // check if regular a log entry must be written
            if ((System.currentTimeMillis() - monitor.m_lastLogStatus) > monitor.m_intervalLog) {
                monitor.monitorWriteLog(false);
            }

            // check if the memory status email must be send
            if ((System.currentTimeMillis() - monitor.m_lastEmailStatus) > monitor.m_intervalEmail) {
                monitor.monitorSendEmail(false);
            }
        } finally {
            // make sure state is reset even if an error occurs, 
            // otherwise MM will not be executed after an error
            m_currentlyRunning = false;
        }

        return null;
    }

    /**
     * Returns true if the system runs low on memory.<p>
     * 
     * @return true if the system runs low on memory
     */
    public boolean lowMemory() {

        return ((m_maxUsagePercent > 0) && (m_memoryCurrent.getUsage() > m_maxUsagePercent));
    }

    /**
     * Adds a new object to the monitor.<p>
     * 
     * @param objectName name of the object
     * @param object the object for monitoring
     */
    public void register(String objectName, Object object) {

        if (enabled()) {
            m_monitoredObjects.put(objectName, object);
        }
    }

    /**
     * Checks if some kind of persistency is required.<p>
     * 
     * This could be overwritten in a distributed environment.<p>
     * 
     * @return <code>true</code> if some kind of persistency is required
     */
    public boolean requiresPersistency() {

        return true;
    }

    /**
     * Flushes all cached objects.<p>
     * 
     * @throws Exception if something goes wrong 
     */
    public void shutdown() throws Exception {

        flushACLs();
        flushGroups();
        flushLocales();
        flushMemObjects();
        flushOrgUnits();
        flushPermissions();
        flushProjectResources();
        flushProjects();
        flushProperties();
        flushPropertyLists();
        flushResourceLists();
        flushResources();
        flushUserGroups();
        flushUsers();
        flushVfsObjects();
        flushLocks(null);
        flushContentDefinitions();
        flushXmlPermanentEntities();
        flushXmlTemporaryEntities();
        flushRoles();
        flushRoleLists();
    }

    /**
     * Removes the given xml content definition from the cache.<p>
     * 
     * @param key the cache key to remove from cache
     */
    public void uncacheContentDefinition(String key) {

        m_contentDefinitionsCache.remove(key);
    }

    /**
     * Removes the given group from the cache.<p>
     * 
     * The group is removed by name AND also by uuid.<p>
     * 
     * @param group the group to remove from cache
     */
    public void uncacheGroup(CmsGroup group) {

        m_groupCache.remove(group.getId().toString());
        m_groupCache.remove(group.getName());
    }

    /**
     * Removes the cached lock for the given root path from the cache.<p>
     * 
     * @param rootPath the root path of the lock to remove from cache
     */
    public void uncacheLock(String rootPath) {

        m_lockCache.remove(rootPath);
    }

    /**
     * Removes the given organizational unit from the cache.<p>
     * 
     * The organizational unit is removed by name AND also by uuid.<p>
     * 
     * @param orgUnit the organizational unit to remove from cache
     */
    public void uncacheOrgUnit(CmsOrganizationalUnit orgUnit) {

        m_orgUnitCache.remove(orgUnit.getId().toString());
        m_orgUnitCache.remove(orgUnit.getName());
    }

    /**
     * Removes the given project from the cache.<p>
     * 
     * The project is removed by name AND also by uuid.<p>
     * 
     * @param project the project to remove from cache
     */
    public void uncacheProject(CmsProject project) {

        m_projectCache.remove(project.getUuid().toString());
        m_projectCache.remove(project.getName());
    }

    /**
     * Removes the given publish job from the cache.<p>
     * 
     * @param publishJob the publish job to remove
     */
    public void uncachePublishJob(CmsPublishJobInfoBean publishJob) {

        m_publishQueue.remove(publishJob);
    }

    /**
     * Removes the given publish job from the history.<p>
     * 
     * @param publishJob the publish job to remove
     */
    public void uncachePublishJobInHistory(CmsPublishJobInfoBean publishJob) {

        m_publishHistory.remove(publishJob);
    }

    /**
     * Removes the given user from the cache.<p>
     * 
     * The user is removed by name AND also by uuid.<p>
     * 
     * @param user the user to remove from cache
     */
    public void uncacheUser(CmsUser user) {

        m_userCache.remove(user.getId().toString());
        m_userCache.remove(user.getName());
    }

    /**
     * Removes the given vfs object from the cache.<p>
     * 
     * @param key the cache key to remove from cache
     */
    public void uncacheVfsObject(String key) {

        m_vfsObjectCache.remove(key);
    }

    /**
     * Removes the given xml temporary entity from the cache.<p>
     * 
     * @param key the cache key to remove from cache
     */
    public void uncacheXmlTemporaryEntity(String key) {

        m_xmlTemporaryEntityCache.remove(key);
    }

    /**
     * Clears the OpenCms caches.<p> 
     */
    private void clearCaches() {

        if ((m_lastClearCache + INTERVAL_CLEAR) > System.currentTimeMillis()) {
            // if the cache has already been cleared less then 15 minutes ago we skip this because 
            // clearing the caches to often will hurt system performance and the 
            // setup seems to be in trouble anyway
            return;
        }
        m_lastClearCache = System.currentTimeMillis();
        if (LOG.isWarnEnabled()) {
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_CLEAR_CACHE_MEM_CONS_0));
        }
        OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, Collections.EMPTY_MAP));
        System.gc();
    }

    /**
     * Returns the cache costs of a monitored object.<p>
     * obj must be of type CmsLruCache 
     * 
     * @param obj the object
     * @return the cache costs or "-"
     */
    private long getCosts(Object obj) {

        long costs = 0;
        if (obj instanceof CmsLruCache) {
            costs = ((CmsLruCache)obj).getObjectCosts();
            if (costs < 0) {
                costs = 0;
            }
        }

        return costs;
    }

    /**
     * Returns the number of items within a monitored object.<p>
     * obj must be of type CmsLruCache, CmsLruHashMap or Map
     * 
     * @param obj the object
     * @return the number of items or "-"
     */
    private String getItems(Object obj) {

        if (obj instanceof CmsLruCache) {
            return Integer.toString(((CmsLruCache)obj).size());
        }
        if (obj instanceof Map) {
            return Integer.toString(((Map)obj).size());
        }
        return "-";

⌨️ 快捷键说明

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