📄 cacheservermemoryservice.java
字号:
while (userAgentEnum.hasMoreElements()) { UserAgentGroup curGroup = (UserAgentGroup) userAgentEnum.nextElement(); if (curGroup.matchesInsertCriterias(userAgent)) { // this user agent matches one or more of the regular // expressions, let's add it to this group. curGroup.setUserAgent(userAgent); groupName = curGroup.getName(); userAgentMap.put(userAgent, groupName); hasBeenMatched = true; break; } } if (!hasBeenMatched) { userAgentMap.put(userAgent, DEFAULT_GROUP_NAME); groupName = DEFAULT_GROUP_NAME; if (defaultUserAgentGroup != null) { defaultUserAgentGroup.setUserAgent(userAgent); } } JahiaConsole.println("CacheServerMemoryService.buildCacheKey", "Inserting user agent " + userAgent + " into group " + groupName + "... "); if (automaticUserAgentAdd) { serializeCacheConfig(); } } cacheKey = pageID + KEY_SEPARATOR + userName + KEY_SEPARATOR + groupName; } else { cacheKey = pageID + KEY_SEPARATOR + userName + KEY_SEPARATOR + userAgent; } return cacheKey; } /** * This method returns the text node value of a given parent tag, ie * <name>text</name> will return the string "text" given the parameter of * the tag "name". * @param parentTag parentTag of the text node value * @return a String containing the value of the text node, concatenated if * there are multiple ones (such as in the case of carriage returns). Returns * empty strings if there are no childs */ private String getTextNodeValue (Element parentTag) { Node currentChild = parentTag.getFirstChild(); if (currentChild == null) { return ""; } StringBuffer result = new StringBuffer(); while (currentChild != null) { if (currentChild.getNodeType() == Node.TEXT_NODE) { result.append(currentChild.getNodeValue()); } else { JahiaConsole.println("CacheServerMemoryService.getTextNodeValue", "Unexpected non text node " + currentChild.getNodeName() + ", ignoring..."); } currentChild = currentChild.getNextSibling(); } return result.toString(); } /** * Parses the XML configuration file that contains the user agent to group * mappings. If the file cannot be loaded for whatever reason, the mapping * system is simply deactivated. * @param configFile the configuration file */ private void processConfigFile (File configFile) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document configDom = docBuilder.parse(configFile); NodeList parametersList = configDom.getElementsByTagName(TAG_PARAMETERS); if (parametersList != null) { JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Found parameters in config file, loading..."); if (parametersList.getLength() != 1) { JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Only one tag <" + TAG_PARAMETERS + "> is allowed in the configuration file! Ignoring parameters, using defaults... " ); } else { Element parameterList = (Element) parametersList.item(0); // max entries parameter retrieval NodeList maxEntriesList = parameterList.getElementsByTagName(TAG_MAX_ENTRIES); if (maxEntriesList != null) { if (maxEntriesList.getLength() != 1) { JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Only one tag <" + TAG_MAX_ENTRIES + "> is allowed in the configuration file! Ignoring parameters, using defaults... " ); } else { Element maxEntries = (Element) maxEntriesList.item(0); String maxEntriesString = getTextNodeValue(maxEntries).trim(); int maxEntriesInt = Integer.parseInt(maxEntriesString); setMaxCacheSize(maxEntriesInt); } } NodeList debugModeList = parameterList.getElementsByTagName(TAG_DEBUG_MODE); if (debugModeList != null) { if (debugModeList.getLength() != 1) { JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Only one tag <" + TAG_DEBUG_MODE + "> is allowed in the configuration file! Ignoring parameters, using defaults... " ); } else { Element debugModeElem = (Element) debugModeList.item(0); String debugModeString = getTextNodeValue(debugModeElem).trim().toLowerCase(); if ("on".equals(debugModeString)) { debugActivated = true; } } } } } NodeList groupList = configDom.getElementsByTagName(TAG_GROUP); if (groupList == null) { JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Missing group list, aborting processing"); return; } for (int i = 0; i < groupList.getLength(); i++) { Element currentGroup = (Element) groupList.item(i); NodeList nameList = currentGroup.getElementsByTagName(TAG_NAME); if (nameList.getLength() != 1) { JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Group can only have one name, ignoring group"); continue; } Element groupNameElement = (Element) nameList.item(0); String groupName = getTextNodeValue(groupNameElement).trim(); if ("".equals(groupName)) { JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Empty group name, ignoring group... "); continue; } Vector regexpVector = new Vector(); NodeList regexpList = currentGroup.getElementsByTagName(TAG_REGEXP); if (regexpList == null) { JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Empty regexp list, no problem, moving to agent list... "); } else { /* JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Found " + Integer.toString(regexpList.getLength()) + " <regexp> tags for group " + groupName); */ // there are regular expressions defined for this group, // let's load them... for (int j=0; j < regexpList.getLength(); j++) { Element regexpEl = (Element) regexpList.item(j); String regexpValue = regexpEl.getAttribute(ATTR_VALUE); if (!("".equals(regexpValue))) { /* JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Adding regexp "+ regexpValue + " for group " + groupName); */ regexpVector.add(regexpValue); } } } // now let's process the agents NodeList agentList = currentGroup.getElementsByTagName(TAG_AGENT); if (agentList == null) { JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Empty agent list, ignoring group"); continue; } Set agentSet = new HashSet(); for (int j=0; j < agentList.getLength(); j++) { Element agent = (Element) agentList.item(j); String agentValue = agent.getAttribute(ATTR_VALUE); if (!("".equals(agentValue))) { // only add agent string if it isn't empty. agentSet.add(agentValue); } } // we now have the group name and the agent set, let's build // the entries in the lookup table. We do this backwards, we // map the userAgent to the group, repeating the group everytime // purely to optimize lookup time during request processing... Iterator agentIter = agentSet.iterator(); while (agentIter.hasNext()) { String curAgent = (String) agentIter.next(); userAgentMap.put(curAgent, groupName); } // we can now also build a user agent group, notably used to // re-serialize the data to the XML file as well as insert new // unknown user agents based on the evaluation of regular // expressions UserAgentGroup curGroup = new UserAgentGroup(groupName, regexpVector, agentSet); if (DEFAULT_GROUP_NAME.equals(groupName)) { // for speed of access we use this internal reference to // the default group. defaultUserAgentGroup = curGroup; } userAgentGroupList.add(curGroup); } if (userAgentMap.size() > 0) { // only activate userAgent group map if the table has entries useGroupMap = true; JahiaConsole.println("CacheServerMemoryService.processConfigFile", "Using user agent group mapping techology..."); } } catch (ParserConfigurationException pce) { JahiaConsole.printe("CacheServerMemoryService.processConfigFile", pce); } catch (IOException ioe) { JahiaConsole.printe("CacheServerMemoryService.processConfigFile", ioe); } catch (org.xml.sax.SAXException saxe) { JahiaConsole.printe("CacheServerMemoryService.processConfigFile", saxe); } } private void setMaxCacheSize(int maxCacheSize) { if (maxCacheSize < 1) { return; } this.maxCacheEntries = maxCacheSize; synchronized (cacheTable) { if (cacheTable.size() >= maxCacheEntries) { JahiaConsole.println("CacheServerMemoryService.setMaxCacheSize", "New maximum cache size (" + Integer.toString(maxCacheEntries) + ") smaller than current cache size, purging elements starting with least accessed."); while (cacheTable.size() >= maxCacheEntries) { CacheNode firstNode = beforeFirstNode.getNextNode(); // remove entry from linked list beforeFirstNode.setNextNode(firstNode.getNextNode()); firstNode.getNextNode().setPreviousNode(beforeFirstNode); // remove entry from cache table cacheTable.remove(firstNode.getCacheKey()); } } } } private void debugDisplayCacheAccessList (String methodName) { if (debugActivated) { CacheNode curNode = beforeFirstNode; JahiaConsole.println("CacheServerMemoryService." + methodName, "Cache access list dump START"); int count = 0; while (curNode != null) { String curCacheKey = curNode.getCacheKey(); JahiaConsole.println("CacheServerMemoryService." + methodName, "CacheNode list entry #" + Integer.toString(count) + "=" + curCacheKey); curNode = curNode.getNextNode(); count++; } JahiaConsole.println("CacheServerMemoryService." + methodName, "Cache access list dump END"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -