1 40 41 package org.jahia.services.cache.treecache; 42 43 import java.io.File ; 44 import java.io.IOException ; 45 import java.util.ArrayList ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.Map ; 50 import java.util.Properties ; 51 import java.util.Set ; 52 import java.util.TreeSet ; 53 54 import org.apache.commons.digester.Digester; 55 import org.apache.commons.digester.Rule; 56 import org.jahia.exceptions.JahiaInitializationException; 57 import org.jahia.services.cache.HtmlCache; 58 import org.jahia.services.htmlcache.UserAgentGroup; 59 import org.jahia.settings.SettingsBean; 60 import org.jboss.cache.CacheException; 61 import org.jboss.cache.Fqn; 62 import org.jboss.cache.TreeCache; 63 import org.xml.sax.SAXException ; 64 65 90 public class JahiaTreeHtmlCache extends JahiaTreeCache implements HtmlCache { 91 92 public static final String HTML_CACHE = "HTMLCache"; 94 95 96 private static final String KEY_SEPARATOR = "###"; 97 98 99 private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger 100 .getLogger(HtmlCache.class); 101 102 private static final String CONFIG_FILE_NAME = "cacheconfig.xml"; 103 104 private Digester digester = null; 105 106 protected ArrayList userAgentGroupList = new ArrayList (); 107 108 private Map userAgentMap = new HashMap (); 109 110 private static final String DEFAULT_GROUP_NAME = "defaultgroup"; 111 112 protected UserAgentGroup defaultUserAgentGroup = null; 113 114 123 protected JahiaTreeHtmlCache(boolean propagated) 124 throws JahiaInitializationException { 125 super(HTML_CACHE, propagated); 126 127 logger.debug("***** HTML Cache successfully instanciated. *****"); 129 } 130 131 145 public synchronized void invalidatePageEntries(String pageID) { 146 if (pageID == null) { 147 logger.debug("Cannot remove a null page ID fromt the cache!"); 148 return; 149 } 150 151 logger.debug("Removing cache entries for page [" + pageID + "]"); 152 153 remove(pageID); 154 } 155 156 170 public synchronized void invalidatePageEntries(String pageID, 171 int workflowState) { 172 if (pageID == null) { 173 logger.debug("Cannot remove a null page ID fromt the cache!"); 174 return; 175 } 176 177 logger.debug("Removing cache entries for page [" + pageID + "]"); 178 179 List key = new ArrayList (2); 180 key.add(pageID); 181 key.add(Integer.toString(workflowState)); 182 remove(key); 183 } 184 185 199 public synchronized void invalidateUserEntries(String username) { 200 if (username == null) { 201 logger.debug("Cannot remove a null username fromt the cache!"); 202 return; 203 } 204 205 logger.debug("Removing cache entries for user [" + username + "]"); 206 TreeCache htmlCache = getTreeCache(); 207 try { 208 Set pageKeys = htmlCache.getChildrenNames(getFqn()); 209 if (pageKeys != null) { 210 for (Iterator it = pageKeys.iterator(); it.hasNext();) { 211 Object pageKey = it.next(); 212 Set workflowKeys = htmlCache.getChildrenNames(new Fqn( 213 getFqn(), pageKey)); 214 if (workflowKeys != null) { 215 for (Iterator it2 = workflowKeys.iterator(); it2 216 .hasNext();) { 217 Object workflowKey = it2.next(); 218 Fqn fqn = new Fqn(getFqn(), pageKey, workflowKey, 219 username); 220 if (htmlCache.exists(fqn)) { 221 htmlCache.remove(fqn); 222 } 223 } 224 } 225 } 226 } 227 } catch (CacheException e) { 228 logger.warn(getName() + username, e); 229 } 230 } 231 232 258 public Object computeEntryKey(String pageID, String userName, 259 String languageCode, int workflowState, String userAgent, String scheme) { 260 List key = new ArrayList (3); 261 key.add(pageID); 262 key.add(Integer.toString(workflowState)); 263 key.add(userName); 264 265 StringBuffer buffer = new StringBuffer (); 266 buffer.append(languageCode); 267 if (userAgentGroupList.size() > 0) { 268 String groupName = DEFAULT_GROUP_NAME; 269 if (userAgentMap.containsKey(userAgent)) { 270 groupName = (String ) userAgentMap.get(userAgent); 271 } else { 272 Iterator userAgentEnum = userAgentGroupList.iterator(); 273 boolean hasBeenMatched = false; 274 while (userAgentEnum.hasNext()) { 275 UserAgentGroup curGroup = (UserAgentGroup) userAgentEnum 276 .next(); 277 if (curGroup.matchesInsertCriterias(userAgent)) { 278 curGroup.setUserAgent(userAgent); 281 groupName = curGroup.getName(); 282 userAgentMap.put(userAgent, groupName); 283 hasBeenMatched = true; 284 break; 285 } 286 } 287 if (!hasBeenMatched) { 288 userAgentMap.put(userAgent, DEFAULT_GROUP_NAME); 289 groupName = DEFAULT_GROUP_NAME; 290 if (defaultUserAgentGroup != null) { 291 defaultUserAgentGroup.setUserAgent(userAgent); 292 } 293 } 294 logger.debug("Inserting user agent " + userAgent 295 + " into group " + groupName + "... "); 296 } 297 298 buffer.append(KEY_SEPARATOR); 299 buffer.append(groupName); 300 } 301 302 if (!"http".equals(scheme)) { 303 buffer.append(KEY_SEPARATOR); 304 buffer.append(scheme); 305 } 306 307 key.add(buffer.toString()); 308 309 return key; 310 } 311 312 public void init(SettingsBean jSettings) 313 throws JahiaInitializationException { 314 try { 315 String configPath = jSettings.getJahiaOutputCacheConfigDiskPath(); 316 317 File configFile = new File (configPath + File.separator 318 + CONFIG_FILE_NAME); 319 if (configFile.exists()) { 320 321 String configFileName = configPath + File.separator 322 + CONFIG_FILE_NAME; 323 324 loadAgentGroupConfig(configFileName); 325 326 defaultUserAgentGroup = new UserAgentGroup(DEFAULT_GROUP_NAME, 327 new ArrayList (), new TreeSet ()); 328 329 } else { 330 logger.error("Config file not found in " + configPath 331 + File.separator + CONFIG_FILE_NAME); 332 } 333 logger.debug("Initialized"); 334 } catch (Throwable t) { 335 throw new JahiaInitializationException( 336 "Error while loading output cache configuration file", t); 337 } 338 } 339 340 private void loadAgentGroupConfig(String agentGroupConfigFileName) 341 throws IOException , SAXException { 342 initDigester(); 343 File agentGroupConfigFile = new File (agentGroupConfigFileName); 344 this.digester.parse(agentGroupConfigFile); 345 } 346 347 private void initDigester() { 348 this.digester = new Digester(); 349 350 AddGroupRule addGroupRule = new AddGroupRule(); 351 digester.addRule("cacheconfig/agentgroups/group", addGroupRule); 352 digester.addRule("cacheconfig/agentgroups/group/name", 353 addGroupRule.groupNameRule); 354 digester.addRule("cacheconfig/agentgroups/group/regexps/regexp", 355 addGroupRule.groupRegexpRule); 356 } 357 358 final class AddGroupRule extends Rule { 359 360 private Properties properties = new Properties (); 361 362 private String groupName; 363 364 protected ArrayList regexpList = new ArrayList (); 365 366 protected AddGroupNameRule groupNameRule = new AddGroupNameRule(); 367 368 protected AddGroupRegexpRule groupRegexpRule = new AddGroupRegexpRule(); 369 370 public void begin(String namespace, String name, 371 org.xml.sax.Attributes attributes) throws Exception { 372 for (int i = 0; i < attributes.getLength(); i++) { 373 properties.setProperty(attributes.getQName(i), attributes 374 .getValue(i)); 375 } 376 } 377 378 public void end(String namespace, String name) throws Exception { 379 groupName = groupNameRule.tagText; 380 385 UserAgentGroup curGroup = new UserAgentGroup(groupName, regexpList, 386 new TreeSet ()); 387 388 if (DEFAULT_GROUP_NAME.equals(groupName)) { 389 defaultUserAgentGroup = curGroup; 392 } 393 394 userAgentGroupList.add(curGroup); 395 groupName = null; 396 regexpList = new ArrayList (); 397 properties = new Properties (); 398 } 399 400 final class AddGroupNameRule extends Rule { 401 402 private String tagName = null; 403 404 protected String tagText = null; 405 406 public AddGroupNameRule() { 407 } 408 409 public void body(String namespace, String name, String text) 410 throws Exception { 411 this.tagName = name; 412 this.tagText = text; 413 } 414 415 } 416 417 final class AddGroupRegexpRule extends Rule { 418 419 Properties properties = new Properties (); 420 421 public AddGroupRegexpRule() { 422 } 423 424 public void begin(String namespace, String name, 425 org.xml.sax.Attributes attributes) throws Exception { 426 for (int i = 0; i < attributes.getLength(); i++) { 427 properties.setProperty(attributes.getQName(i), attributes 428 .getValue(i)); 429 } 430 } 431 432 public void end(String namespace, String name) throws Exception { 433 String curRegexp = properties.getProperty("value"); 434 if (curRegexp != null) { 435 regexpList.add(curRegexp); 436 } 437 properties = new Properties (); 438 } 439 440 } 441 442 } 443 444 } 445 | Popular Tags |