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.Roller;
027: import org.apache.roller.business.RollerFactory;
028:
029: /**
030: * Reset referer counts.
031: */
032: public class TurnoverReferersTask extends RollerTask {
033:
034: private static Log log = LogFactory
035: .getLog(TurnoverReferersTask.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 "TurnoverReferersTask";
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: Roller roller = RollerFactory.getRoller();
103: roller.getRefererManager().clearReferrers();
104: roller.flush();
105:
106: log.info("task completed");
107:
108: } catch (RollerException e) {
109: log.error("Error while checking for referer turnover", e);
110: } catch (Exception ee) {
111: log.error("unexpected exception", ee);
112: } finally {
113: // always release
114: RollerFactory.getRoller().release();
115: }
116:
117: }
118:
119: /**
120: * Main method so that this task may be run from outside the webapp.
121: */
122: public static void main(String[] args) throws Exception {
123: try {
124: TurnoverReferersTask task = new TurnoverReferersTask();
125: task.init();
126: task.run();
127: System.exit(0);
128: } catch (RollerException ex) {
129: ex.printStackTrace();
130: System.exit(-1);
131: }
132: }
133:
134: }
|