/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. The ASF licenses this file to You
* under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. For additional information regarding
* copyright in this work, please see the NOTICE file in the top level
* directory of this distribution.
*/
package org.apache.roller.business.runnable;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.RollerException;
import org.apache.roller.business.RollerFactory;
import org.apache.roller.business.WeblogManager;
/**
* Reset weblog hit counts.
*/
public class ResetHitCountsTask extends RollerTask {
private static Log log = LogFactory.getLog(ResetHitCountsTask.class);
// a String description of when to start this task
private String startTimeDesc = "startOfDay";
// interval at which the task is run, default is 1 day
private int interval = 1440;
// lease time given to task lock, default is 30 minutes
private int leaseTime = 30;
public String getName() {
return "ResetHitCountsTask";
}
public Date getStartTime(Date currentTime) {
return getAdjustedTime(currentTime, startTimeDesc);
}
public int getInterval() {
return this.interval;
}
public int getLeaseTime() {
return this.leaseTime;
}
public void init() throws RollerException {
// get relevant props
Properties props = this.getTaskProperties();
// extract start time
String startTimeStr = props.getProperty("startTime");
if(startTimeStr != null) {
this.startTimeDesc = startTimeStr;
}
// extract interval
String intervalStr = props.getProperty("interval");
if(intervalStr != null) {
try {
this.interval = Integer.parseInt(intervalStr);
} catch (NumberFormatException ex) {
log.warn("Invalid interval: "+intervalStr);
}
}
// extract lease time
String leaseTimeStr = props.getProperty("leaseTime");
if(leaseTimeStr != null) {
try {
this.leaseTime = Integer.parseInt(leaseTimeStr);
} catch (NumberFormatException ex) {
log.warn("Invalid leaseTime: "+leaseTimeStr);
}
}
}
/**
* Execute the task.
*/
public void runTask() {
try {
log.info("task started");
WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
mgr.resetAllHitCounts();
RollerFactory.getRoller().flush();
log.info("task completed");
} catch (RollerException e) {
log.error("Error while checking for referer turnover", e);
} catch (Exception ee) {
log.error("unexpected exception", ee);
} finally {
// always release
RollerFactory.getRoller().release();
}
}
/**
* Main method so that this task may be run from outside the webapp.
*/
public static void main(String[] args) throws Exception {
try {
TurnoverReferersTask task = new TurnoverReferersTask();
task.init();
task.run();
System.exit(0);
} catch (RollerException ex) {
ex.printStackTrace();
System.exit(-1);
}
}
}