KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > cache > CacheConfig


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 JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
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 JavaDoc;
22
23 import org.apache.commons.lang.BooleanUtils;
24 import org.apache.commons.lang.StringUtils;
25
26
27 /**
28  * @author Andreas Brenk
29  * @author Fabrizio Giustina
30  * @since 3.0
31  * @version $Id:CacheConfig.java 6314 2006-09-11 08:24:51Z scharles $
32  */

33 public class CacheConfig {
34
35     private static final String JavaDoc DEFAULT_CACHE_IMPLEMENTATION = "info.magnolia.cms.cache.simple.CacheImpl";
36
37     private boolean active;
38
39     private String JavaDoc cacheImplementation;
40
41     private CacheManager cacheManager;
42
43     /**
44      * Compression wont work for these pre compressed formats.
45      */

46     private Map JavaDoc compressionList;
47
48     private final Content content;
49
50     private Map JavaDoc uriMapping;
51
52     /**
53      * Create a new CacheConfig and loads the config from the repository.
54      * @throws ConfigurationException
55      */

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 JavaDoc key) {
65         return this.compressionList.containsKey(key.trim().toLowerCase());
66     }
67
68     public String JavaDoc getCacheImplementation() {
69         return this.cacheImplementation;
70     }
71
72     public Content getContent() {
73         return this.content;
74     }
75
76     public Content getContent(String JavaDoc 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     /**
89      * @return true if the uri is allowed to be cached, false otherwise
90      */

91     public boolean isUriCacheable(HttpServletRequest JavaDoc request) {
92
93         String JavaDoc uri = Path.getURI(request);
94         boolean isAllowed = false;
95         int lastMatchedPatternlength = 0;
96
97         for (Iterator JavaDoc 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 JavaDoc) this.uriMapping.get(p)).booleanValue();
104                 }
105             }
106         }
107
108         return isAllowed;
109     }
110
111     /**
112      * todo refactor
113      */

114     private Map JavaDoc cacheCacheableURIMappings(Content nodeList, Map JavaDoc mappings, boolean allow) {
115         for (Iterator JavaDoc 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         // load mandatory config
129
try {
130             Content contentNode = this.content.getContent("URI/allow");
131             Map JavaDoc mappings = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc e) {
169             throw new ConfigurationException("Could not register JCR EventLister.");
170         }
171     }
172
173     /**
174      * todo refactor
175      */

176     private Map JavaDoc updateCompressionList(Content list) {
177         Map JavaDoc compressionList = new HashMap JavaDoc();
178
179         if (list == null) {
180             return compressionList;
181         }
182
183         Iterator JavaDoc 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