File Source: ScheduledEntriesTask.java
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.weblogger.business.runnable;
20
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Properties;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.weblogger.WebloggerException;
28 import org.apache.roller.weblogger.business.WebloggerFactory;
29 import org.apache.roller.weblogger.business.WeblogManager;
30 import org.apache.roller.weblogger.business.search.IndexManager;
31 import org.apache.roller.weblogger.pojos.WeblogEntry;
32 import org.apache.roller.weblogger.util.cache.CacheManager;
33
34
35 /**
36 * This task is used to promote SCHEDULED weblog entries to the PUBLISHED
37 * status when their publication time has been reached.
38 */
/*
P/P * Method: void org.apache.roller.weblogger.business.runnable.ScheduledEntriesTask()
*
* Postconditions:
* this.clientId == null
* this.interval == 1
* this.leaseTime == 30
* this.startTimeDesc == &"immediate"
*/
39 public class ScheduledEntriesTask extends RollerTaskWithLeasing {
40
/*
P/P * Method: org.apache.roller.weblogger.business.runnable.ScheduledEntriesTask__static_init
*
* Postconditions:
* init'ed(log)
*/
41 private static Log log = LogFactory.getLog(ScheduledEntriesTask.class);
42
43 // a unique id for this specific task instance
44 // this is meant to be unique for each client in a clustered environment
45 private String clientId = null;
46
47 // a String description of when to start this task
48 private String startTimeDesc = "immediate";
49
50 // interval at which the task is run, default is once per minute
51 private int interval = 1;
52
53 // lease time given to task lock, default is 30 minutes
54 private int leaseTime = 30;
55
56
57 public String getName() {
/*
P/P * Method: String getName()
*
* Postconditions:
* return_value == &"ScheduledEntriesTask"
*/
58 return "ScheduledEntriesTask";
59 }
60
61 public String getClientId() {
/*
P/P * Method: String getClientId()
*
* Preconditions:
* init'ed(this.clientId)
*
* Postconditions:
* return_value == this.clientId
* init'ed(return_value)
*/
62 return clientId;
63 }
64
65 public Date getStartTime(Date currentTime) {
/*
P/P * Method: Date getStartTime(Date)
*
* Preconditions:
* init'ed(this.startTimeDesc)
*
* Postconditions:
* init'ed(return_value)
*/
66 return getAdjustedTime(currentTime, startTimeDesc);
67 }
68
69 public String getStartTimeDesc() {
/*
P/P * Method: String getStartTimeDesc()
*
* Preconditions:
* init'ed(this.startTimeDesc)
*
* Postconditions:
* return_value == this.startTimeDesc
* init'ed(return_value)
*/
70 return startTimeDesc;
71 }
72
73 public int getInterval() {
/*
P/P * Method: int getInterval()
*
* Preconditions:
* init'ed(this.interval)
*
* Postconditions:
* return_value == this.interval
* init'ed(return_value)
*/
74 return this.interval;
75 }
76
77 public int getLeaseTime() {
/*
P/P * Method: int getLeaseTime()
*
* Preconditions:
* init'ed(this.leaseTime)
*
* Postconditions:
* return_value == this.leaseTime
* init'ed(return_value)
*/
78 return this.leaseTime;
79 }
80
81
82 public void init() throws WebloggerException {
83
84 // get relevant props
/*
P/P * Method: void init()
*
* Preconditions:
* org/apache/roller/weblogger/config/WebloggerConfig.config != null
* org/apache/roller/weblogger/config/WebloggerConfig.log != null
* (soft) log != null
*
* Postconditions:
* possibly_updated(this.clientId)
* possibly_updated(this.interval)
* possibly_updated(this.leaseTime)
* possibly_updated(this.startTimeDesc)
*
* Test Vectors:
* java.util.Properties:getProperty(...)@100: Addr_Set{null}, Inverse{null}
* java.util.Properties:getProperty(...)@110: Addr_Set{null}, Inverse{null}
* java.util.Properties:getProperty(...)@88: Addr_Set{null}, Inverse{null}
* java.util.Properties:getProperty(...)@94: Addr_Set{null}, Inverse{null}
*/
85 Properties props = this.getTaskProperties();
86
87 // extract clientId
88 String client = props.getProperty("clientId");
89 if(client != null) {
90 this.clientId = client;
91 }
92
93 // extract start time
94 String startTimeStr = props.getProperty("startTime");
95 if(startTimeStr != null) {
96 this.startTimeDesc = startTimeStr;
97 }
98
99 // extract interval
100 String intervalStr = props.getProperty("interval");
101 if(intervalStr != null) {
102 try {
103 this.interval = Integer.parseInt(intervalStr);
104 } catch (NumberFormatException ex) {
105 log.warn("Invalid interval: "+intervalStr);
106 }
107 }
108
109 // extract lease time
110 String leaseTimeStr = props.getProperty("leaseTime");
111 if(leaseTimeStr != null) {
112 try {
113 this.leaseTime = Integer.parseInt(leaseTimeStr);
114 } catch (NumberFormatException ex) {
115 log.warn("Invalid leaseTime: "+leaseTimeStr);
116 }
117 }
118 }
119
120
121 /**
122 * Execute the task.
123 */
124 public void runTask() {
125
/*
P/P * Method: void runTask()
*
* Preconditions:
* log != null
* org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
* org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
* (soft) org/apache/roller/weblogger/business/WebloggerImpl.log != null
* (soft) org/apache/roller/weblogger/business/jpa/JPAAutoPingManagerImpl.logger != null
* (soft) org/apache/roller/weblogger/business/jpa/JPAPingQueueManagerImpl.log != null
* (soft) org/apache/roller/weblogger/business/search/IndexManagerImpl.mLogger != null
* (soft) org/apache/roller/weblogger/config/WebloggerRuntimeConfig.log != null
* (soft) org/apache/roller/weblogger/pojos/AutoPing.pcInheritedFieldCount <= 232-3
*
* Presumptions:
* getWeblogger(...).autoPingManager != null
* getWeblogger(...).bookmarkManager != null
* getWeblogger(...).fileManager != null
* getWeblogger(...).indexManager != null
* getWeblogger(...).pingQueueManager != null
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@155: {0}, {1}
*/
126 log.debug("task started");
127
128 try {
129 WeblogManager wMgr = WebloggerFactory.getWeblogger().getWeblogManager();
130 IndexManager searchMgr = WebloggerFactory.getWeblogger().getIndexManager();
131
132 Date now = new Date();
133
134 log.debug("looking up scheduled entries older than "+now);
135
136 // get all published entries older than current time
137 List scheduledEntries = wMgr.getWeblogEntries(
138
139 null, // website
140 null, // user
141 null, // startDate
142 now, // endDate
143 null, // catName
144 null,WeblogEntry.SCHEDULED, // status
145 null, // text
146 null, // sortBy
147 null, // sortOrder
148 null, // locale
149 0, -1); // offset, length
150
151 log.debug("promoting "+scheduledEntries.size()+" entries to PUBLISHED state");
152
153 WeblogEntry entry = null;
154 Iterator it = scheduledEntries.iterator();
155 while(it.hasNext()) {
156 entry = (WeblogEntry) it.next();
157
158 // update status to PUBLISHED and save
159 entry.setStatus(WeblogEntry.PUBLISHED);
160 wMgr.saveWeblogEntry(entry);
161 }
162
163 // commit the changes
164 WebloggerFactory.getWeblogger().flush();
165
166 // take a second pass to trigger reindexing and cache invalidations
167 // this is because we need the updated entries flushed first
168 it = scheduledEntries.iterator();
169 while(it.hasNext()) {
170 entry = (WeblogEntry) it.next();
171
172 // trigger a cache invalidation
173 CacheManager.invalidate(entry);
174
175 // trigger search index on entry
176 searchMgr.addEntryReIndexOperation(entry);
177 }
178
179 } catch (WebloggerException e) {
180 log.error("Error getting scheduled entries", e);
181 } catch(Exception e) {
182 log.error("Unexpected exception running task", e);
183 } finally {
184 // always release
185 WebloggerFactory.getWeblogger().release();
186 }
187
188 log.debug("task completed");
189
190 }
191
192
193 /**
194 * Main method so that this task may be run from outside the webapp.
195 */
196 public static void main(String[] args) throws Exception {
197 try {
/*
P/P * Method: void main(String[])
* main does not return
*
* Preconditions:
* (soft) log != null
* (soft) org/apache/roller/weblogger/config/WebloggerConfig.config != null
* (soft) org/apache/roller/weblogger/config/WebloggerConfig.log != null
*/
198 ScheduledEntriesTask task = new ScheduledEntriesTask();
199 task.init();
200 task.run();
201 System.exit(0);
202 } catch (WebloggerException ex) {
203 ex.printStackTrace();
204 System.exit(-1);
205 }
206 }
207
208 }
SofCheck Inspector Build Version : 2.18479
| ScheduledEntriesTask.java |
2009-Jan-02 14:24:58 |
| ScheduledEntriesTask.class |
2009-Sep-04 03:12:31 |