1 40 41 42 package org.jahia.services.cache.simplecache; 43 44 import org.jahia.exceptions.JahiaInitializationException; 45 import java.io.IOException ; 46 import org.xml.sax.SAXException ; 47 import java.io.File ; 48 import org.apache.commons.digester.Digester; 49 import org.apache.commons.digester.Rule; 50 import java.util.Properties ; 51 import java.util.ArrayList ; 52 53 import org.jahia.services.cache.Cache; 54 import org.jahia.services.cache.HtmlCache; 55 import org.jahia.services.cache.HtmlCacheEntry; 56 import org.jahia.services.cache.JMSHub; 57 import org.jahia.services.htmlcache.UserAgentGroup; 58 import java.util.TreeSet ; 59 import java.util.Iterator ; 60 import java.util.Map ; 61 import java.util.HashMap ; 62 import org.jahia.settings.SettingsBean; 63 64 65 85 public class SimpleHtmlCache extends SimpleCache implements HtmlCache { 86 87 88 private static final String KEY_SEPARATOR = "###"; 89 90 91 private static final String PAGE_PREFIX = "PAGE-"; 92 93 94 private static final String HIERARCHY_SEPARATOR = ":"; 95 96 97 private static final org.apache.log4j.Logger logger = 98 org.apache.log4j.Logger.getLogger (SimpleHtmlCache.class); 99 100 private static final String CONFIG_FILE_NAME = "cacheconfig.xml"; 101 private Digester digester = null; 102 protected ArrayList userAgentGroupList = new ArrayList (); 103 private Map userAgentMap = new HashMap (); 104 private static final String DEFAULT_GROUP_NAME = "defaultgroup"; 105 protected UserAgentGroup defaultUserAgentGroup = null; 106 107 114 public SimpleHtmlCache (JMSHub hub) 115 throws JahiaInitializationException 116 { 117 super (HtmlCache.HTML_CACHE, hub); 118 119 logger.debug ("***** HTML Cache successfully instanciated. *****"); 121 } 122 123 124 132 public synchronized void invalidatePageEntries (String pageID) { 133 if (pageID == null) { 134 logger.debug ("Cannot remove a null page ID fromt the cache!"); 135 return; 136 } 137 138 logger.debug ("Removing cache entries for page ["+ pageID +"]"); 139 140 String pageKey = PAGE_PREFIX + pageID + HIERARCHY_SEPARATOR; 141 Object [] keys = keys(); 142 for (int i=0; i<keys.length; i++) { 143 if (keys[i] instanceof String ) { 144 String key = (String )keys[i]; 145 146 if (key.startsWith (pageKey)) { 148 remove(keys[i]); 149 } 150 } 151 } 152 } 153 154 162 public synchronized void invalidatePageEntries (String pageID, int workflowState) { 163 if (pageID == null) { 164 logger.debug ("Cannot remove a null page ID fromt the cache!"); 165 return; 166 } 167 168 logger.debug ("Removing cache entries for page ["+ pageID +"]"); 169 170 String pageKey = PAGE_PREFIX + pageID + HIERARCHY_SEPARATOR+"(.*)"+KEY_SEPARATOR+workflowState; 171 Object [] keys = keys(); 172 for (int i=0; i<keys.length; i++) { 173 if (keys[i] instanceof String ) { 174 String key = (String )keys[i]; 175 176 if (key.matches(pageKey)) { 178 remove(keys[i]); 179 } 180 } 181 } 182 } 183 184 192 public synchronized void invalidateUserEntries (String username) { 193 if (username == null) { 194 logger.debug ("Cannot remove a null username fromt the cache!"); 195 return; 196 } 197 198 logger.debug ("Removing cache entries for user ["+ username +"]"); 199 200 String userKey = HIERARCHY_SEPARATOR + username + HIERARCHY_SEPARATOR; 201 Object [] keys = keys(); 202 for (int i=0; i<keys.length; i++) { 203 if (keys[i] instanceof String ) { 204 String key = (String )keys[i]; 205 206 if (key.indexOf (userKey) != -1) { 208 remove(keys[i]); 209 } 210 } 211 } 212 } 213 214 215 233 public Object computeEntryKey (String pageID, String userName, String languageCode, 234 int workflowState, String userAgent, String scheme) 235 { 236 StringBuffer buffer = new StringBuffer (); 237 buffer.append (PAGE_PREFIX); 238 buffer.append (pageID); 239 buffer.append (HIERARCHY_SEPARATOR); 240 241 buffer.append (userName); 242 buffer.append (HIERARCHY_SEPARATOR); 243 buffer.append (languageCode); 244 buffer.append (KEY_SEPARATOR); 245 buffer.append (Integer.toString(workflowState)); 246 if (userAgentGroupList.size() > 0) { 247 String groupName = DEFAULT_GROUP_NAME; 248 if (userAgentMap.containsKey(userAgent)) { 249 groupName = (String ) userAgentMap.get(userAgent); 250 } else { 251 Iterator userAgentEnum = userAgentGroupList.iterator(); 252 boolean hasBeenMatched = false; 253 while (userAgentEnum.hasNext()) { 254 UserAgentGroup curGroup = (UserAgentGroup) userAgentEnum.next(); 255 if (curGroup.matchesInsertCriterias(userAgent)) { 256 curGroup.setUserAgent(userAgent); 259 groupName = curGroup.getName(); 260 userAgentMap.put(userAgent, groupName); 261 hasBeenMatched = true; 262 break; 263 } 264 } 265 if (!hasBeenMatched) { 266 userAgentMap.put(userAgent, DEFAULT_GROUP_NAME); 267 groupName = DEFAULT_GROUP_NAME; 268 if (defaultUserAgentGroup != null) { 269 defaultUserAgentGroup.setUserAgent(userAgent); 270 } 271 } 272 logger.debug("Inserting user agent " + userAgent + 273 " into group " + groupName + "... "); 274 } 275 276 buffer.append(KEY_SEPARATOR); 277 buffer.append(groupName); 278 if (!"http".equals(scheme)) { 279 buffer.append(KEY_SEPARATOR); 280 buffer.append(scheme); 281 } 282 } 283 284 return buffer.toString(); 285 } 286 287 public void init(SettingsBean jSettings) 288 throws JahiaInitializationException { 289 try { 290 String configPath = jSettings.getJahiaOutputCacheConfigDiskPath(); 291 292 File configFile = new File (configPath + File.separator + 293 CONFIG_FILE_NAME); 294 if (configFile.exists()) { 295 296 String configFileName = configPath + File.separator + 297 CONFIG_FILE_NAME; 298 299 loadAgentGroupConfig(configFileName); 300 301 defaultUserAgentGroup = new UserAgentGroup(DEFAULT_GROUP_NAME, new ArrayList (), new TreeSet ()); 302 303 } else { 304 logger.error("Config file not found in " + configPath + 305 File.separator + CONFIG_FILE_NAME); 306 } 307 logger.debug("Initialized"); 308 } catch (Throwable t) { 309 throw new JahiaInitializationException("Error while loading output cache configuration file", t); 310 } 311 } 312 313 private void loadAgentGroupConfig (String agentGroupConfigFileName) 314 throws IOException , SAXException { 315 initDigester(); 316 File agentGroupConfigFile = new File (agentGroupConfigFileName); 317 this.digester.parse(agentGroupConfigFile); 318 } 319 320 private void initDigester () { 321 this.digester = new Digester(); 322 323 AddGroupRule addGroupRule = 324 new AddGroupRule(); 325 digester.addRule( 326 "cacheconfig/agentgroups/group", addGroupRule); 327 digester.addRule( 328 "cacheconfig/agentgroups/group/name", 329 addGroupRule.groupNameRule); 330 digester.addRule( 331 "cacheconfig/agentgroups/group/regexps/regexp", 332 addGroupRule.groupRegexpRule); 333 } 334 335 final class AddGroupRule extends Rule { 336 337 private Properties properties = new Properties (); 338 private String groupName; 339 protected ArrayList regexpList = new ArrayList (); 340 341 protected AddGroupNameRule groupNameRule = new AddGroupNameRule(); 342 protected AddGroupRegexpRule groupRegexpRule = new 343 AddGroupRegexpRule(); 344 345 public void begin ( 346 String namespace, 347 String name, 348 org.xml.sax.Attributes attributes) 349 throws Exception { 350 for (int i = 0; i < attributes.getLength(); i++) { 351 properties.setProperty( 352 attributes.getQName(i), 353 attributes.getValue(i)); 354 } 355 } 356 357 public void end (String namespace, String name) 358 throws Exception { 359 groupName = groupNameRule.tagText; 360 365 UserAgentGroup curGroup = new UserAgentGroup(groupName, 366 regexpList, 367 new TreeSet ()); 368 369 if (DEFAULT_GROUP_NAME.equals(groupName)) { 370 defaultUserAgentGroup = curGroup; 373 } 374 375 userAgentGroupList.add(curGroup); 376 groupName = null; 377 regexpList = new ArrayList (); 378 properties = new Properties (); 379 } 380 381 final class AddGroupNameRule extends Rule { 382 383 protected String tagText = null; 384 385 public AddGroupNameRule () { 386 } 387 388 public void body (String namespace, String name, String text) 389 throws Exception { 390 this.tagText = text; 391 } 392 393 } 394 395 final class AddGroupRegexpRule extends Rule { 396 397 Properties regExpProperties = new Properties (); 398 399 public AddGroupRegexpRule () { 400 } 401 402 public void begin ( 403 String namespace, 404 String name, 405 org.xml.sax.Attributes attributes) 406 throws Exception { 407 for (int i = 0; i < attributes.getLength(); i++) { 408 regExpProperties.setProperty( 409 attributes.getQName(i), 410 attributes.getValue(i)); 411 } 412 } 413 414 public void end (String namespace, String name) 415 throws Exception { 416 String curRegexp = regExpProperties.getProperty("value"); 417 if (curRegexp != null) { 418 regexpList.add(curRegexp); 419 } 420 regExpProperties = new Properties (); 421 } 422 423 } 424 425 } 426 427 } 428 | Popular Tags |