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

📄 cacheservermemoryservice.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            cacheTable.put(cacheKey, curNode);        }    }    public Object getEntryProperty(String pageID,                                   String userName,                                   String userAgent,                                   String propertyName) {        CacheEntry entry = getEntry(pageID, userName, userAgent);        if (entry == null)            return null;        return entry.getProperty(propertyName);    }    public void setEntryProperty(String pageID,                                 String userName,                                 String userAgent,                                 String propertyName,                                 Object propertyValue) {        CacheEntry entry = getEntry(pageID, userName, userAgent);        if (entry == null)            return;        entry.setProperty(propertyName, propertyValue);    }    public String getContentBody(String pageID,                                 String userName,                                 String userAgent) {        CacheEntry entry = getEntry(pageID, userName, userAgent);        if (entry == null)            return null;        return entry.getContentBody();    }    public void setContentBody(String pageID,                               String userName,                               String userAgent,                               String content) {        CacheEntry entry = getEntry(pageID, userName, userAgent);        if (entry == null)            return;        entry.setContentBody(content);    }    private boolean removeCacheNode(String cacheKey) {        if (cacheTable.containsKey(cacheKey)) {            // JahiaConsole.println("CacheServerMemoryServer.removeHTML", "Removing key <" + cacheKey + ">");            // first let's remove it from the linked list            CacheNode curNode = (CacheNode) cacheTable.get(cacheKey);            CacheNode previousNode = curNode.getPreviousNode();            CacheNode nextNode = curNode.getNextNode();            previousNode.setNextNode(nextNode);            nextNode.setPreviousNode(previousNode);            this.debugDisplayCacheAccessList("removeCacheNode");            // we may now remove it from the hash map.            cacheTable.remove(cacheKey);            return true;        } else {            return false;        }    }    public boolean removeEntry(String pageID, String userName, String userAgent) {        String cacheKey = buildCacheKey(pageID, userName, userAgent);        return removeCacheNode(cacheKey);    }    public boolean removeEntry(String pageID, String userName) {        String matchStr = pageID + KEY_SEPARATOR + userName;        // we must go through a seperate Vector because removing elements from an        // Enumeration has undefined behaviours.        JahiaConsole.println("CacheServerMemoryService",                             "Removing content cache for pageID=" + pageID);        Vector keysToRemove = new Vector();        Set cacheKeySet = cacheTable.keySet();        Iterator cacheKeyIterator = cacheKeySet.iterator();        while (cacheKeyIterator.hasNext()) {            String curKey = (String) cacheKeyIterator.next();            if (curKey.startsWith(matchStr)) {                keysToRemove.add(curKey);            }        }        Enumeration keysElements = keysToRemove.elements();        while (keysElements.hasMoreElements()) {            String curKey = (String) keysElements.nextElement();            removeCacheNode(curKey);        }        return true;    }    public boolean removeEntry(String pageID) {        // we must go through a seperate Vector because removing elements from an        // Enumeration has undefined behaviours.        JahiaConsole.println("CacheServerMemoryService",                             "Removing content cache for pageID=" + pageID);        Vector keysToRemove = new Vector();        Set cacheKeySet = cacheTable.keySet();        Iterator cacheKeyIterator = cacheKeySet.iterator();        while (cacheKeyIterator.hasNext()) {            String curKey = (String) cacheKeyIterator.next();            if (curKey.startsWith(pageID)) {                keysToRemove.add(curKey);            }        }        Enumeration keysElements = keysToRemove.elements();        while (keysElements.hasMoreElements()) {            String curKey = (String) keysElements.nextElement();            removeCacheNode(curKey);        }        return true;    }    public boolean removeUserEntries(String userName) {        // WARNING: this implementation assumes that the        // KEY_SEPARATOR + userName + KEY_SEPARATOR will never be sent in        // a user agent. If it does it will simply match more strings, but        // this is not too dangerous since we are only clearing cache entries.        // we must go through a seperate Vector because removing elements from an        // Enumeration has undefined behaviours.        JahiaConsole.println("CacheServerMemoryService",                             "Removing content cache for userName=" + userName);        String matchStr = KEY_SEPARATOR + userName + KEY_SEPARATOR;        Vector keysToRemove = new Vector();        Set cacheKeySet = cacheTable.keySet();        Iterator cacheKeyIterator = cacheKeySet.iterator();        while (cacheKeyIterator.hasNext()) {            String curKey = (String) cacheKeyIterator.next();            if (curKey.indexOf(matchStr) != -1) {                keysToRemove.add(curKey);            }        }        Enumeration keysElements = keysToRemove.elements();        while (keysElements.hasMoreElements()) {            String curKey = (String) keysElements.nextElement();            removeCacheNode(curKey);        }        return true;    }    public void flushCache() {        // empty linked list        beforeFirstNode.setNextNode(afterLastNode);        afterLastNode.setPreviousNode(beforeFirstNode);        this.debugDisplayCacheAccessList("flushCache");        // empty hash map        cacheTable.clear();    }    public void serializeCacheConfig() {        if (configFileName == null) {            return;        }        if ("".equals(configFileName)) {            return;        }        File configFile = new File(configFileName);        serializeCacheConfig(configFile);    }    /**     * Serializes the current cache configuration data into the cache output     * file. This is mostly used to add new agent strings as they are encountered.     */    public synchronized void serializeCacheConfig(File configFile) {        try {            JahiaConsole.println("CacheServerMemoryService.serializeCacheConfig",                                 "Saving current cache user agent mapping to file " +                                 configFile.getName());            if (configFile.exists()) {                if (!configFile.canWrite()) {                    JahiaConsole.println("CacheServerMemoryService.serializeCacheConfig",                                         "Don't have write access to file " +                                         configFile.getName() +                                         ", unable to serialize configuration.");                    return;                }                configFile.delete();            }            configFile.createNewFile();            FileWriter fileWriter = new FileWriter(configFile);            PrintWriter filePrint = new PrintWriter(fileWriter);            filePrint.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");            filePrint.println("<cacheconfig>");            filePrint.println("    <!-- This file is automatically re-generated everytime a new user agent is detected and ");            filePrint.println("         it is added to the defaultgroup user agent list. If you want to make changes to this ");            filePrint.println("         file, for example to group an automatically added user agent, SHUTDOWN TOMCAT first,  ");            filePrint.println("         make a backup copy of the file, and then you are free to modify it.");            filePrint.println("    -->");            filePrint.println("    <parameters>");            filePrint.println("        <max-entries>" + Integer.toString(maxCacheEntries) + "</max-entries>");            if (debugActivated) {                filePrint.println("        <debug-mode>on</debug-mode>");            } else {                filePrint.println("        <debug-mode>off</debug-mode>");            }            filePrint.println("    </parameters>");            filePrint.println("    <agentgroups>");            Enumeration groupEnum = userAgentGroupList.elements();            while (groupEnum.hasMoreElements()) {                UserAgentGroup curGroup = (UserAgentGroup) groupEnum.nextElement();                filePrint.println("        <group>");                filePrint.println("            <name>" + curGroup.getName() + "</name>");                // let's dump the regular expressions if there are any for this group                Enumeration regexpEnum = curGroup.getRegExpStrings();                if (regexpEnum.hasMoreElements()) {                    filePrint.println("            <regexps>");                    while (regexpEnum.hasMoreElements()) {                        String curRegExp = (String) regexpEnum.nextElement();                        filePrint.println("                <regexp value=\"" + curRegExp + "\"/>");                    }                    filePrint.println("            </regexps>");                }                // let's dump the user agent strings                Iterator agentIter = curGroup.getUserAgentSetIterator();                if (agentIter.hasNext()) {                    filePrint.println("            <agents>");                    while (agentIter.hasNext()) {                        String curAgent = (String) agentIter.next();                        curAgent = JahiaTools.replacePattern(curAgent, "&", "&amp;");                        curAgent = JahiaTools.replacePattern(curAgent, "<", "&lt;");                        curAgent = JahiaTools.replacePattern(curAgent, ">", "&gt;");                        curAgent = JahiaTools.replacePattern(curAgent, "'", "&apos;");                        curAgent = JahiaTools.replacePattern(curAgent, "\"", "&quot;");                        filePrint.println("                <agent value=\"" + curAgent + "\"/>");                    }                    filePrint.println("            </agents>");                }                filePrint.println("        </group>");            }            filePrint.println("    </agentgroups>");            filePrint.println("</cacheconfig>");            fileWriter.flush();            fileWriter.close();        } catch (IOException ioe) {            JahiaConsole.printe("CacheServerMemoryService.serializeConfig", ioe);        }    }    public int size() {        return cacheTable.size();    }    /**     * Returns the maximum size allowed for the cache     * @return an integer representing the maximum cache size. Returns -1 if     * there is no limit set.     */    public int getMaxSize() {        return maxCacheEntries;    }    /**     * Builds the cache key that is used to reference the cache entries in the     * lookup table. This method actually does much more in the case of user     * agent to group mappings. If the mapping doesn't exist yet, it creates it     * and if the automatic serialization mode is active, it saves the new user     * agent in the default group in the configuration file.     */    private String buildCacheKey(String pageID, String userName, String userAgent) {        String cacheKey;        if (useGroupMap) {            String groupName = DEFAULT_GROUP_NAME;            if (userAgentMap.containsKey(userAgent)) {                groupName = (String) userAgentMap.get(userAgent);            } else {                Enumeration userAgentEnum = userAgentGroupList.elements();                boolean hasBeenMatched = false;

⌨️ 快捷键说明

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