KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > util > WeblogPageCache


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.util;
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.config.RollerConfig;
30 import org.apache.roller.util.Utilities;
31 import org.apache.roller.util.cache.Cache;
32 import org.apache.roller.util.cache.CacheManager;
33 import org.apache.roller.util.cache.LazyExpiringCacheEntry;
34
35
36 /**
37  * Cache for weblog page content.
38  */

39 public class WeblogPageCache {
40     
41     private static Log log = LogFactory.getLog(WeblogPageCache.class);
42     
43     // a unique identifier for this cache, this is used as the prefix for
44
// roller config properties that apply to this cache
45
public static final String JavaDoc CACHE_ID = "cache.weblogpage";
46     
47     // keep cached content
48
private boolean cacheEnabled = true;
49     private Cache contentCache = null;
50     
51     // reference to our singleton instance
52
private static WeblogPageCache singletonInstance = new WeblogPageCache();
53     
54     
55     private WeblogPageCache() {
56         
57         cacheEnabled = RollerConfig.getBooleanProperty(CACHE_ID+".enabled");
58         
59         Map JavaDoc cacheProps = new HashMap JavaDoc();
60         cacheProps.put("id", CACHE_ID);
61         Enumeration JavaDoc allProps = RollerConfig.keys();
62         String JavaDoc prop = null;
63         while(allProps.hasMoreElements()) {
64             prop = (String JavaDoc) allProps.nextElement();
65             
66             // we are only interested in props for this cache
67
if(prop.startsWith(CACHE_ID+".")) {
68                 cacheProps.put(prop.substring(CACHE_ID.length()+1),
69                         RollerConfig.getProperty(prop));
70             }
71         }
72         
73         log.info(cacheProps);
74         
75         if(cacheEnabled) {
76             contentCache = CacheManager.constructCache(null, cacheProps);
77         } else {
78             log.warn("Caching has been DISABLED");
79         }
80     }
81     
82     
83     public static WeblogPageCache getInstance() {
84         return singletonInstance;
85     }
86     
87     
88     public Object JavaDoc get(String JavaDoc key, long lastModified) {
89         
90         if(!cacheEnabled)
91             return null;
92         
93         Object JavaDoc entry = null;
94         
95         LazyExpiringCacheEntry lazyEntry =
96                 (LazyExpiringCacheEntry) this.contentCache.get(key);
97         if(lazyEntry != null) {
98             entry = lazyEntry.getValue(lastModified);
99             
100             if(entry != null) {
101                 log.debug("HIT "+key);
102             } else {
103                 log.debug("HIT-EXPIRED "+key);
104             }
105             
106         } else {
107             log.debug("MISS "+key);
108         }
109         
110         return entry;
111     }
112     
113     
114     public void put(String JavaDoc key, Object JavaDoc value) {
115         
116         if(!cacheEnabled)
117             return;
118         
119         contentCache.put(key, new LazyExpiringCacheEntry(value));
120         log.debug("PUT "+key);
121     }
122     
123     
124     public void remove(String JavaDoc key) {
125         
126         if(!cacheEnabled)
127             return;
128         
129         contentCache.remove(key);
130         log.debug("REMOVE "+key);
131     }
132     
133     
134     public void clear() {
135         
136         if(!cacheEnabled)
137             return;
138         
139         contentCache.clear();
140         log.debug("CLEAR");
141     }
142     
143     
144     /**
145      * Generate a cache key from a parsed weblog page request.
146      * This generates a key of the form ...
147      *
148      * <handle>/<ctx>[/anchor][/language][/user]
149      * or
150      * <handle>/<ctx>[/weblogPage][/date][/category][/language][/user]
151      *
152      *
153      * examples ...
154      *
155      * foo/en
156      * foo/entry_anchor
157      * foo/20051110/en
158      * foo/MyCategory/en/user=myname
159      *
160      */

161     public String JavaDoc generateKey(WeblogPageRequest pageRequest) {
162         
163         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
164         
165         key.append(this.CACHE_ID).append(":");
166         key.append(pageRequest.getWeblogHandle());
167         
168         if(pageRequest.getWeblogAnchor() != null) {
169             
170             String JavaDoc anchor = null;
171             try {
172                 // may contain spaces or other bad chars
173
anchor = URLEncoder.encode(pageRequest.getWeblogAnchor(), "UTF-8");
174             } catch(UnsupportedEncodingException JavaDoc ex) {
175                 // ignored
176
}
177             
178             key.append("/entry/").append(anchor);
179         } else {
180             
181             if(pageRequest.getWeblogPageName() != null) {
182                 key.append("/page/").append(pageRequest.getWeblogPageName());
183             }
184             
185             if(pageRequest.getWeblogDate() != null) {
186                 key.append("/").append(pageRequest.getWeblogDate());
187             }
188             
189             if(pageRequest.getWeblogCategoryName() != null) {
190                 String JavaDoc cat = null;
191                 try {
192                     // may contain spaces or other bad chars
193
cat = URLEncoder.encode(pageRequest.getWeblogCategoryName(), "UTF-8");
194                 } catch(UnsupportedEncodingException JavaDoc ex) {
195                     // ignored
196
}
197                 
198                 key.append("/").append(cat);
199             }
200         }
201         
202         if(pageRequest.getLocale() != null) {
203             key.append("/").append(pageRequest.getLocale());
204         }
205         
206         // add page number when applicable
207
if(pageRequest.getWeblogAnchor() == null) {
208             key.append("/page=").append(pageRequest.getPageNum());
209         }
210         
211         // add login state
212
if(pageRequest.getAuthenticUser() != null) {
213             key.append("/user=").append(pageRequest.getAuthenticUser());
214         }
215         
216         // we allow for arbitrary query params for custom pages
217
if(pageRequest.getWeblogPageName() != null &&
218                 pageRequest.getCustomParams().size() > 0) {
219             String JavaDoc queryString = paramsToString(pageRequest.getCustomParams());
220             
221             key.append("/qp=").append(queryString);
222         }
223         
224         return key.toString();
225     }
226     
227     
228     private String JavaDoc paramsToString(Map JavaDoc map) {
229         
230         if(map == null) {
231             return null;
232         }
233         
234         StringBuffer JavaDoc string = new StringBuffer JavaDoc();
235         
236         String JavaDoc key = null;
237         String JavaDoc[] value = null;
238         Iterator JavaDoc keys = map.keySet().iterator();
239         while(keys.hasNext()) {
240             key = (String JavaDoc) keys.next();
241             value = (String JavaDoc[]) map.get(key);
242             
243             if(value != null) {
244                 string.append(",").append(key).append("=").append(value[0]);
245             }
246         }
247         
248         return Utilities.toBase64(string.toString().substring(1).getBytes());
249     }
250     
251 }
252
Popular Tags