KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > cache > FuturePostingsInvalidationJob


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.util.cache;
20
21 import java.util.Calendar JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.roller.RollerException;
31 import org.apache.roller.business.runnable.Job;
32 import org.apache.roller.model.RollerFactory;
33 import org.apache.roller.model.UserManager;
34 import org.apache.roller.model.WeblogManager;
35 import org.apache.roller.pojos.WeblogEntryData;
36 import org.apache.roller.pojos.WebsiteData;
37
38
39 /**
40  * Trigger cache invalidations for entries published into the future.
41  *
42  * This job is meant to be run at timed intervals, it will not do any
43  * good to run this job only a single time.
44  *
45  * We do things in a somewhat counterintuitive manner, but it makes things
46  * easier on us from an operational point of view. We start by looking up
47  * all entries published between now and some time XX mins in the future. We
48  * then save that list and when XX mins has passed we invalidate the list and
49  * query for entries published in the next XX mins.
50  *
51  * Basically we are building a short term list of future published entries
52  * and expiring them once our wait period is over. This prevents us from
53  * having to somehow determine which entries published in the last XX mins
54  * had previously been published into the future.
55  */

56 public class FuturePostingsInvalidationJob implements Job {
57     
58     private static Log log = LogFactory.getLog(FuturePostingsInvalidationJob.class);
59     
60     // inputs from the user
61
private Map JavaDoc inputs = null;
62     
63     // the set of entries we expire at the start of the next run
64
private Set JavaDoc nextExpirations = null;
65     
66     // how far into the future we will look ahead, in minutes
67
int peerTime = 5;
68     
69     public void execute() {
70         
71         log.debug("starting");
72         
73         try {
74             WeblogManager wMgr = RollerFactory.getRoller().getWeblogManager();
75             UserManager uMgr = RollerFactory.getRoller().getUserManager();
76             
77             Date JavaDoc now = new Date JavaDoc();
78             
79             if(nextExpirations != null) {
80                 String JavaDoc websiteid = null;
81                 WebsiteData weblog = null;
82                 
83                 Iterator JavaDoc weblogs = nextExpirations.iterator();
84                 while(weblogs.hasNext()) {
85                     websiteid = (String JavaDoc) weblogs.next();
86                     
87                     try {
88                         // lookup the actual entry
89
weblog = uMgr.getWebsite(websiteid);
90                         
91                         log.debug("expiring"+weblog.getHandle());
92                         
93                         // to expire weblog content we have to update the
94
// last modified time of the weblog and save it
95
weblog.setLastModified(now);
96                         uMgr.saveWebsite(weblog);
97                         
98                     } catch (RollerException ex) {
99                         log.warn("couldn't lookup entry "+websiteid);
100                     }
101                 }
102                 
103                 // commit the changes
104
RollerFactory.getRoller().flush();
105             }
106             
107             // XX mins in the future
108
Calendar JavaDoc cal = Calendar.getInstance();
109             cal.setTime(now);
110             cal.add(Calendar.MINUTE, this.peerTime);
111             Date JavaDoc end = cal.getTime();
112             
113             log.debug("looking up entries between "+now+" and "+end);
114             
115             // get all published entries between start and end date
116
List JavaDoc expiringEntries = wMgr.getWeblogEntries(null, null, now, end, null,
117                     null, WeblogEntryData.PUBLISHED, null, 0, -1);
118             
119             // we only really want the weblog ids
120
Set JavaDoc expiringWeblogs = new HashSet JavaDoc();
121             Iterator JavaDoc it = expiringEntries.iterator();
122             while(it.hasNext()) {
123                 expiringWeblogs.add(((WeblogEntryData) it.next()).getWebsite().getId());
124             }
125             
126             this.nextExpirations = expiringWeblogs;
127             
128         } catch(Exception JavaDoc e) {
129             log.error(e);
130         }
131         
132         log.debug("finished");
133     }
134     
135     
136     public Map JavaDoc output() {
137        return null;
138     }
139     
140     
141     public void input(Map JavaDoc input) {
142         this.inputs = input;
143         
144         // extract peer time if possible
145
Integer JavaDoc pTime = (Integer JavaDoc) this.inputs.get("peerTime");
146         if(pTime != null) {
147             this.peerTime = pTime.intValue();
148         }
149         
150         log.info("Peeking "+this.peerTime+" minutes into the future each pass");
151     }
152     
153 }
154
Popular Tags