1 package info.magnolia.cms.cache; 2 3 import info.magnolia.cms.beans.config.ConfigurationException; 4 import info.magnolia.cms.core.Content; 5 import info.magnolia.cms.core.ItemType; 6 import info.magnolia.cms.core.NodeData; 7 import info.magnolia.cms.core.Path; 8 import info.magnolia.cms.util.SimpleUrlPattern; 9 import info.magnolia.cms.util.UrlPattern; 10 11 import java.util.Collections ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 import javax.jcr.Node; 17 import javax.jcr.RepositoryException; 18 import javax.jcr.observation.Event; 19 import javax.jcr.observation.EventListener; 20 import javax.jcr.observation.ObservationManager; 21 import javax.servlet.http.HttpServletRequest ; 22 23 import org.apache.commons.lang.BooleanUtils; 24 import org.apache.commons.lang.StringUtils; 25 26 27 33 public class CacheConfig { 34 35 private static final String DEFAULT_CACHE_IMPLEMENTATION = "info.magnolia.cms.cache.simple.CacheImpl"; 36 37 private boolean active; 38 39 private String cacheImplementation; 40 41 private CacheManager cacheManager; 42 43 46 private Map compressionList; 47 48 private final Content content; 49 50 private Map uriMapping; 51 52 56 protected CacheConfig(CacheManager cacheManager, Content content) throws ConfigurationException { 57 this.cacheManager = cacheManager; 58 this.content = content; 59 60 loadConfig(); 61 registerEventListener(); 62 } 63 64 public boolean canCompress(String key) { 65 return this.compressionList.containsKey(key.trim().toLowerCase()); 66 } 67 68 public String getCacheImplementation() { 69 return this.cacheImplementation; 70 } 71 72 public Content getContent() { 73 return this.content; 74 } 75 76 public Content getContent(String name) throws RepositoryException { 77 return this.content.getContent(name); 78 } 79 80 public Node getNode() { 81 return this.content.getJCRNode(); 82 } 83 84 public boolean isActive() { 85 return this.active; 86 } 87 88 91 public boolean isUriCacheable(HttpServletRequest request) { 92 93 String uri = Path.getURI(request); 94 boolean isAllowed = false; 95 int lastMatchedPatternlength = 0; 96 97 for (Iterator listEnum = this.uriMapping.keySet().iterator(); listEnum.hasNext();) { 98 UrlPattern p = (UrlPattern) listEnum.next(); 99 if (p.match(uri)) { 100 int patternLength = p.getLength(); 101 if (lastMatchedPatternlength < patternLength) { 102 lastMatchedPatternlength = patternLength; 103 isAllowed = ((Boolean ) this.uriMapping.get(p)).booleanValue(); 104 } 105 } 106 } 107 108 return isAllowed; 109 } 110 111 114 private Map cacheCacheableURIMappings(Content nodeList, Map mappings, boolean allow) { 115 for (Iterator it = nodeList.getChildren(ItemType.CONTENTNODE).iterator(); it.hasNext();) { 116 Content container = (Content) it.next(); 117 NodeData uri = container.getNodeData("URI"); 118 UrlPattern p = new SimpleUrlPattern(uri.getString()); 119 mappings.put(p, BooleanUtils.toBooleanObject(allow)); 120 } 121 122 return mappings; 123 } 124 125 private void loadConfig() throws ConfigurationException { 126 this.active = this.content.getNodeData("active").getBoolean(); 127 128 try { 130 Content contentNode = this.content.getContent("URI/allow"); 131 Map mappings = new HashMap (); 132 cacheCacheableURIMappings(contentNode, mappings, true); 133 contentNode = this.content.getContent("URI/deny"); 134 cacheCacheableURIMappings(contentNode, mappings, false); 135 this.uriMapping = Collections.unmodifiableMap(mappings); 136 Content compressionListNode = this.content.getContent("compression"); 137 this.compressionList = Collections.unmodifiableMap(updateCompressionList(compressionListNode)); 138 } 139 catch (RepositoryException e) { 140 throw new ConfigurationException("Could not load cache configuration: " + e.getMessage(), e); 141 } 142 143 String cacheImplementation = this.content.getNodeData("cacheImplementation").getString(); 144 if (StringUtils.isBlank(cacheImplementation)) { 145 cacheImplementation = DEFAULT_CACHE_IMPLEMENTATION; 146 } 147 148 this.cacheImplementation = cacheImplementation; 149 150 } 151 152 protected void reload() throws ConfigurationException { 153 this.loadConfig(); 154 } 155 156 private void registerEventListener() throws ConfigurationException { 157 try { 158 Node node = this.content.getJCRNode(); 159 ObservationManager observationManager = node.getSession().getWorkspace().getObservationManager(); 160 EventListener listener = new CacheConfigListener(this.cacheManager, this); 161 int events = Event.NODE_ADDED 162 | Event.NODE_REMOVED 163 | Event.PROPERTY_ADDED 164 | Event.PROPERTY_CHANGED 165 | Event.PROPERTY_REMOVED; 166 observationManager.addEventListener(listener, events, node.getPath(), true, null, null, false); 167 } 168 catch (Exception e) { 169 throw new ConfigurationException("Could not register JCR EventLister."); 170 } 171 } 172 173 176 private Map updateCompressionList(Content list) { 177 Map compressionList = new HashMap (); 178 179 if (list == null) { 180 return compressionList; 181 } 182 183 Iterator it = list.getChildren().iterator(); 184 while (it.hasNext()) { 185 Content node = (Content) it.next(); 186 compressionList.put(node.getNodeData("extension").getString(), node.getNodeData("type").getString()); 187 } 188 189 return compressionList; 190 } 191 } 192 | Popular Tags |