001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.util.cache;
020:
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.roller.RollerException;
028: import org.apache.roller.business.runnable.Job;
029: import org.apache.roller.config.RollerConfig;
030: import org.apache.roller.business.RollerFactory;
031: import org.apache.roller.business.UserManager;
032: import org.apache.roller.pojos.StaticTemplate;
033: import org.apache.roller.pojos.Template;
034: import org.apache.roller.pojos.WebsiteData;
035: import org.apache.roller.ui.rendering.Renderer;
036: import org.apache.roller.ui.rendering.RendererManager;
037: import org.apache.roller.ui.rendering.model.ModelLoader;
038: import org.apache.roller.ui.rendering.util.cache.WeblogFeedCache;
039: import org.apache.roller.ui.rendering.util.WeblogFeedRequest;
040: import org.apache.roller.util.cache.CachedContent;
041:
042: /**
043: * EXPERIMENTAL!!
044: *
045: * A job which will "warm up" some of the rendering layer caches by iterating
046: * over all weblogs in the system and rendering a set of their content to put
047: * in the caches for later use.
048: *
049: * Currently only supports warming up the feed cache.
050: */
051: public class WeblogCacheWarmupJob implements Job {
052:
053: private static Log log = LogFactory
054: .getLog(WeblogCacheWarmupJob.class);
055:
056: // inputs from the user
057: private Map inputs = null;
058:
059: public void execute() {
060:
061: log.debug("starting");
062:
063: // check inputs to see what work we are going to do
064: if (inputs != null) {
065:
066: // what weblogs will we handle?
067: List weblogs = (List) inputs.get("weblogs");
068: if (weblogs == null) {
069: return;
070: }
071:
072: // should we do rss entries feeds?
073: if ("true".equals((String) inputs.get("feed-entries-rss"))) {
074: this .warmupFeedCache(weblogs, "entries", "rss");
075: }
076:
077: // should we do atom entries feeds?
078: if ("true".equals((String) inputs.get("feed-entries-atom"))) {
079: this .warmupFeedCache(weblogs, "entries", "atom");
080: }
081: }
082:
083: log.debug("finished");
084: }
085:
086: public Map output() {
087: return null;
088: }
089:
090: public void input(Map input) {
091: this .inputs = input;
092: }
093:
094: private void warmupFeedCache(List weblogs, String type,
095: String format) {
096:
097: if (weblogs == null) {
098: return;
099: }
100:
101: // we are working on the feed cache
102: WeblogFeedCache feedCache = WeblogFeedCache.getInstance();
103:
104: long start = System.currentTimeMillis();
105:
106: Iterator allWeblogs = weblogs.iterator();
107: String weblogHandle = null;
108: while (allWeblogs.hasNext()) {
109: weblogHandle = (String) allWeblogs.next();
110: log.debug("doing weblog " + weblogHandle);
111:
112: try {
113: // we need a feed request to represent the data
114: WeblogFeedRequest feedRequest = new WeblogFeedRequest();
115: feedRequest.setWeblogHandle(weblogHandle);
116: feedRequest.setType(type);
117: feedRequest.setFormat(format);
118:
119: // populate the rendering model
120: HashMap model = new HashMap();
121: Map initData = new HashMap();
122: initData.put("request", null);
123: initData.put("feedRequest", feedRequest);
124: initData.put("weblogRequest", feedRequest);
125:
126: // Load models for feeds
127: String feedModels = RollerConfig
128: .getProperty("rendering.feedModels");
129: ModelLoader.loadModels(feedModels, model, initData,
130: true);
131:
132: // TODO: re-enable custom models when they are actually used
133: // Load weblog custom models
134: //ModelLoader.loadCustomModels(weblog, model, initData);
135:
136: // lookup Renderer we are going to use
137: Renderer renderer = null;
138: Template template = new StaticTemplate(
139: "templates/feeds/weblog-" + type + "-" + format
140: + ".vm", null, "velocity");
141: renderer = RendererManager.getRenderer(template);
142:
143: // render content. use default size of about 24K for a standard page
144: CachedContent rendererOutput = new CachedContent(24567);
145: renderer
146: .render(model, rendererOutput.getCachedWriter());
147:
148: // flush rendered output and close
149: rendererOutput.flush();
150: rendererOutput.close();
151:
152: // now just put it in the cache
153: String key = feedCache.generateKey(feedRequest);
154: feedCache.put(key, rendererOutput);
155:
156: } catch (Exception e) {
157: // bummer, error during rendering
158: log.error("Error rendering for weblog " + weblogHandle,
159: e);
160: }
161: }
162:
163: long end = System.currentTimeMillis();
164: long time = (end - start) * 1000;
165:
166: log.info("Completed warmup for " + type + "/" + format + " in "
167: + time + " secs.");
168:
169: }
170:
171: }
|