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.business.runnable;
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.RollerFactory;
027: import org.apache.roller.business.WeblogManager;
028:
029: /**
030: * Reset weblog hit counts.
031: */
032: public class ResetHitCountsTask extends RollerTask {
033:
034: private static Log log = LogFactory
035: .getLog(ResetHitCountsTask.class);
036:
037: // a String description of when to start this task
038: private String startTimeDesc = "startOfDay";
039:
040: // interval at which the task is run, default is 1 day
041: private int interval = 1440;
042:
043: // lease time given to task lock, default is 30 minutes
044: private int leaseTime = 30;
045:
046: public String getName() {
047: return "ResetHitCountsTask";
048: }
049:
050: public Date getStartTime(Date currentTime) {
051: return getAdjustedTime(currentTime, startTimeDesc);
052: }
053:
054: public int getInterval() {
055: return this .interval;
056: }
057:
058: public int getLeaseTime() {
059: return this .leaseTime;
060: }
061:
062: public void init() throws RollerException {
063:
064: // get relevant props
065: Properties props = this .getTaskProperties();
066:
067: // extract start time
068: String startTimeStr = props.getProperty("startTime");
069: if (startTimeStr != null) {
070: this .startTimeDesc = startTimeStr;
071: }
072:
073: // extract interval
074: String intervalStr = props.getProperty("interval");
075: if (intervalStr != null) {
076: try {
077: this .interval = Integer.parseInt(intervalStr);
078: } catch (NumberFormatException ex) {
079: log.warn("Invalid interval: " + intervalStr);
080: }
081: }
082:
083: // extract lease time
084: String leaseTimeStr = props.getProperty("leaseTime");
085: if (leaseTimeStr != null) {
086: try {
087: this .leaseTime = Integer.parseInt(leaseTimeStr);
088: } catch (NumberFormatException ex) {
089: log.warn("Invalid leaseTime: " + leaseTimeStr);
090: }
091: }
092: }
093:
094: /**
095: * Execute the task.
096: */
097: public void runTask() {
098:
099: try {
100: log.info("task started");
101:
102: WeblogManager mgr = RollerFactory.getRoller()
103: .getWeblogManager();
104: mgr.resetAllHitCounts();
105: RollerFactory.getRoller().flush();
106:
107: log.info("task completed");
108:
109: } catch (RollerException e) {
110: log.error("Error while checking for referer turnover", e);
111: } catch (Exception ee) {
112: log.error("unexpected exception", ee);
113: } finally {
114: // always release
115: RollerFactory.getRoller().release();
116: }
117:
118: }
119:
120: /**
121: * Main method so that this task may be run from outside the webapp.
122: */
123: public static void main(String[] args) throws Exception {
124: try {
125: TurnoverReferersTask task = new TurnoverReferersTask();
126: task.init();
127: task.run();
128: System.exit(0);
129: } catch (RollerException ex) {
130: ex.printStackTrace();
131: System.exit(-1);
132: }
133: }
134:
135: }
|