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.util.cache;
020:
021: import java.util.Calendar;
022: import java.util.Date;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.roller.RollerException;
031: import org.apache.roller.business.runnable.Job;
032: import org.apache.roller.business.RollerFactory;
033: import org.apache.roller.business.UserManager;
034: import org.apache.roller.business.WeblogManager;
035: import org.apache.roller.pojos.WeblogEntryData;
036: import org.apache.roller.pojos.WebsiteData;
037:
038: /**
039: * Trigger cache invalidations for entries published into the future.
040: *
041: * This job is meant to be run at timed intervals, it will not do any
042: * good to run this job only a single time.
043: *
044: * We do things in a somewhat counterintuitive manner, but it makes things
045: * easier on us from an operational point of view. We start by looking up
046: * all entries published between now and some time XX mins in the future. We
047: * then save that list and when XX mins has passed we invalidate the list and
048: * query for entries published in the next XX mins.
049: *
050: * Basically we are building a short term list of future published entries
051: * and expiring them once our wait period is over. This prevents us from
052: * having to somehow determine which entries published in the last XX mins
053: * had previously been published into the future.
054: */
055: public class FuturePostingsInvalidationJob implements Job {
056:
057: private static Log log = LogFactory
058: .getLog(FuturePostingsInvalidationJob.class);
059:
060: // inputs from the user
061: private Map inputs = null;
062:
063: // the set of entries we expire at the start of the next run
064: private Set nextExpirations = null;
065:
066: // how far into the future we will look ahead, in minutes
067: int peerTime = 5;
068:
069: public void execute() {
070:
071: log.debug("starting");
072:
073: try {
074: WeblogManager wMgr = RollerFactory.getRoller()
075: .getWeblogManager();
076: UserManager uMgr = RollerFactory.getRoller()
077: .getUserManager();
078:
079: Date now = new Date();
080:
081: if (nextExpirations != null) {
082: String websiteid = null;
083: WebsiteData weblog = null;
084:
085: Iterator weblogs = nextExpirations.iterator();
086: while (weblogs.hasNext()) {
087: websiteid = (String) weblogs.next();
088:
089: try {
090: // lookup the actual entry
091: weblog = uMgr.getWebsite(websiteid);
092:
093: log.debug("expiring" + weblog.getHandle());
094:
095: // to expire weblog content we have to update the
096: // last modified time of the weblog and save it
097: weblog.setLastModified(now);
098: uMgr.saveWebsite(weblog);
099:
100: } catch (RollerException ex) {
101: log.warn("couldn't lookup entry " + websiteid);
102: }
103: }
104:
105: // commit the changes
106: RollerFactory.getRoller().flush();
107: }
108:
109: // XX mins in the future
110: Calendar cal = Calendar.getInstance();
111: cal.setTime(now);
112: cal.add(Calendar.MINUTE, this .peerTime);
113: Date end = cal.getTime();
114:
115: log.debug("looking up entries between " + now + " and "
116: + end);
117:
118: // get all published entries between start and end date
119: List expiringEntries = wMgr.getWeblogEntries(null, null,
120: now, end, null, null, null,
121: WeblogEntryData.PUBLISHED, null, 0, -1);
122:
123: // we only really want the weblog ids
124: Set expiringWeblogs = new HashSet();
125: Iterator it = expiringEntries.iterator();
126: while (it.hasNext()) {
127: expiringWeblogs.add(((WeblogEntryData) it.next())
128: .getWebsite().getId());
129: }
130:
131: this .nextExpirations = expiringWeblogs;
132:
133: } catch (Exception e) {
134: log.error(e);
135: }
136:
137: log.debug("finished");
138: }
139:
140: public Map output() {
141: return null;
142: }
143:
144: public void input(Map input) {
145: this .inputs = input;
146:
147: // extract peer time if possible
148: Integer pTime = (Integer) this .inputs.get("peerTime");
149: if (pTime != null) {
150: this .peerTime = pTime.intValue();
151: }
152:
153: log.info("Peeking " + this .peerTime
154: + " minutes into the future each pass");
155: }
156:
157: }
|