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.planet.tasks;
020:
021: import java.util.Date;
022: import java.util.Properties;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.roller.RollerException;
026: import org.apache.roller.business.runnable.RollerTask;
027: import org.apache.roller.business.Roller;
028: import org.apache.roller.business.RollerFactory;
029: import org.apache.roller.config.RollerConfig;
030:
031: /**
032: * Run the Planet Roller refresh-entries method to fetch and parse newsfeeds.
033: */
034: public class RefreshEntriesTask extends RollerTask {
035:
036: private static Log log = LogFactory
037: .getLog(RefreshEntriesTask.class);
038:
039: // a String description of when to start this task
040: private String startTimeDesc = "startOfHour";
041:
042: // interval at which the task is run, default is 60 minutes
043: private int interval = 60;
044:
045: // lease time given to task lock, default is 30 minutes
046: private int leaseTime = 30;
047:
048: public String getName() {
049: return "RefreshEntriesTask";
050: }
051:
052: public Date getStartTime(Date currentTime) {
053: return getAdjustedTime(currentTime, startTimeDesc);
054: }
055:
056: public int getInterval() {
057: return this .interval;
058: }
059:
060: public int getLeaseTime() {
061: return this .leaseTime;
062: }
063:
064: public void init() throws RollerException {
065:
066: // get relevant props
067: Properties props = this .getTaskProperties();
068:
069: // extract start time
070: String startTimeStr = props.getProperty("startTime");
071: if (startTimeStr != null) {
072: this .startTimeDesc = startTimeStr;
073: }
074:
075: // extract interval
076: String intervalStr = props.getProperty("interval");
077: if (intervalStr != null) {
078: try {
079: this .interval = Integer.parseInt(intervalStr);
080: } catch (NumberFormatException ex) {
081: log.warn("Invalid interval: " + intervalStr);
082: }
083: }
084:
085: // extract lease time
086: String leaseTimeStr = props.getProperty("leaseTime");
087: if (leaseTimeStr != null) {
088: try {
089: this .leaseTime = Integer.parseInt(leaseTimeStr);
090: } catch (NumberFormatException ex) {
091: log.warn("Invalid leaseTime: " + leaseTimeStr);
092: }
093: }
094: }
095:
096: public void runTask() {
097: try {
098: Roller roller = RollerFactory.getRoller();
099: roller
100: .getPlanetManager()
101: .refreshEntries(
102: RollerConfig
103: .getProperty("planet.aggregator.cache.dir"));
104: roller.flush();
105: } catch (RollerException e) {
106: log.error("ERROR refreshing entries", e);
107: } finally {
108: RollerFactory.getRoller().release();
109: }
110: }
111:
112: /**
113: * Task may be run from the command line
114: */
115: public static void main(String[] args) {
116: try {
117: RefreshEntriesTask task = new RefreshEntriesTask();
118: task.init();
119: task.run();
120: System.exit(0);
121: } catch (Throwable t) {
122: t.printStackTrace();
123: System.exit(-1);
124: }
125: }
126:
127: }
|