File Source: ResetHitCountsTask.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.Properties;
    23  import org.apache.commons.logging.Log;
    24  import org.apache.commons.logging.LogFactory;
    25  import org.apache.roller.weblogger.WebloggerException;
    26  import org.apache.roller.weblogger.business.WebloggerFactory;
    27  import org.apache.roller.weblogger.business.WeblogManager;
    28  
    29  
    30  /**
    31   * Reset weblog hit counts.
    32   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.business.runnable.ResetHitCountsTask()
          * 
          *  Postconditions:
          *    this.clientId == null
          *    this.interval == 1_440
          *    this.leaseTime == 30
          *    this.startTimeDesc == &"startOfDay"
          */
    33  public class ResetHitCountsTask extends RollerTaskWithLeasing {
    34      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.business.runnable.ResetHitCountsTask__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    35      private static Log log = LogFactory.getLog(ResetHitCountsTask.class);
    36      
    37      // a unique id for this specific task instance
    38      // this is meant to be unique for each client in a clustered environment
    39      private String clientId = null;
    40      
    41      // a String description of when to start this task
    42      private String startTimeDesc = "startOfDay";
    43      
    44      // interval at which the task is run, default is 1 day
    45      private int interval = 1440;
    46      
    47      // lease time given to task lock, default is 30 minutes
    48      private int leaseTime = 30;
    49      
    50      
    51      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Postconditions:
                  *    return_value == &"ResetHitCountsTask"
                  */
    52          return "ResetHitCountsTask";
    53      }
    54      
    55      public String getClientId() {
                 /* 
    P/P           *  Method: String getClientId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.clientId)
                  * 
                  *  Postconditions:
                  *    return_value == this.clientId
                  *    init'ed(return_value)
                  */
    56          return clientId;
    57      }
    58      
    59      public Date getStartTime(Date currentTime) {
                 /* 
    P/P           *  Method: Date getStartTime(Date)
                  * 
                  *  Preconditions:
                  *    init'ed(this.startTimeDesc)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    60          return getAdjustedTime(currentTime, startTimeDesc);
    61      }
    62      
    63      public String getStartTimeDesc() {
                 /* 
    P/P           *  Method: String getStartTimeDesc()
                  * 
                  *  Preconditions:
                  *    init'ed(this.startTimeDesc)
                  * 
                  *  Postconditions:
                  *    return_value == this.startTimeDesc
                  *    init'ed(return_value)
                  */
    64          return startTimeDesc;
    65      }
    66      
    67      public int getInterval() {
                 /* 
    P/P           *  Method: int getInterval()
                  * 
                  *  Preconditions:
                  *    init'ed(this.interval)
                  * 
                  *  Postconditions:
                  *    return_value == this.interval
                  *    init'ed(return_value)
                  */
    68          return this.interval;
    69      }
    70      
    71      public int getLeaseTime() {
                 /* 
    P/P           *  Method: int getLeaseTime()
                  * 
                  *  Preconditions:
                  *    init'ed(this.leaseTime)
                  * 
                  *  Postconditions:
                  *    return_value == this.leaseTime
                  *    init'ed(return_value)
                  */
    72          return this.leaseTime;
    73      }
    74      
    75      
    76      public void init() throws WebloggerException {
    77          
    78          // 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(...)@104: Addr_Set{null}, Inverse{null}
                  *    java.util.Properties:getProperty(...)@82: 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}
                  */
    79          Properties props = this.getTaskProperties();
    80          
    81          // extract clientId
    82          String client = props.getProperty("clientId");
    83          if(client != null) {
    84              this.clientId = client;
    85          }
    86          
    87          // extract start time
    88          String startTimeStr = props.getProperty("startTime");
    89          if(startTimeStr != null) {
    90              this.startTimeDesc = startTimeStr;
    91          }
    92          
    93          // extract interval
    94          String intervalStr = props.getProperty("interval");
    95          if(intervalStr != null) {
    96              try {
    97                  this.interval = Integer.parseInt(intervalStr);
    98              } catch (NumberFormatException ex) {
    99                  log.warn("Invalid interval: "+intervalStr);
   100              }
   101          }
   102          
   103          // extract lease time
   104          String leaseTimeStr = props.getProperty("leaseTime");
   105          if(leaseTimeStr != null) {
   106              try {
   107                  this.leaseTime = Integer.parseInt(leaseTimeStr);
   108              } catch (NumberFormatException ex) {
   109                  log.warn("Invalid leaseTime: "+leaseTimeStr);
   110              }
   111          }
   112      }
   113      
   114      
   115      /**
   116       * Execute the task.
   117       */
   118      public void runTask() {
   119          
   120          try {
                     /* 
    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
                      * 
                      *  Presumptions:
                      *    getWeblogger(...).autoPingManager != null
                      *    getWeblogger(...).bookmarkManager != null
                      *    getWeblogger(...).fileManager != null
                      *    getWeblogger(...).pingQueueManager != null
                      *    getWeblogger(...).pingTargetManager != null
                      *    ...
                      */
   121              log.info("task started");
   122              
   123              WeblogManager mgr = WebloggerFactory.getWeblogger().getWeblogManager();
   124              mgr.resetAllHitCounts();
   125              WebloggerFactory.getWeblogger().flush();
   126              
   127              log.info("task completed");
   128              
   129          } catch (WebloggerException e) {
   130              log.error("Error while checking for referer turnover", e);
   131          } catch (Exception ee) {
   132              log.error("unexpected exception", ee);
   133          } finally {
   134              // always release
   135              WebloggerFactory.getWeblogger().release();
   136          }
   137          
   138      }
   139      
   140      
   141      /**
   142       * Main method so that this task may be run from outside the webapp.
   143       */
   144      public static void main(String[] args) throws Exception {
   145          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
                      */
   146              ResetHitCountsTask task = new ResetHitCountsTask();
   147              task.init();
   148              task.run();
   149              System.exit(0);
   150          } catch (WebloggerException ex) {
   151              ex.printStackTrace();
   152              System.exit(-1);
   153          }
   154      }
   155      
   156  }








SofCheck Inspector Build Version : 2.18479
ResetHitCountsTask.java 2009-Jan-02 14:25:18
ResetHitCountsTask.class 2009-Sep-04 03:12:31