File Source: PingQueueTask.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.pings;
    20  
    21  import java.util.Calendar;
    22  import java.util.Date;
    23  import java.util.Properties;
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26  import org.apache.roller.weblogger.WebloggerException;
    27  import org.apache.roller.weblogger.business.runnable.RollerTaskWithLeasing;
    28  import org.apache.roller.weblogger.config.PingConfig;
    29  import org.apache.roller.weblogger.business.Weblogger;
    30  import org.apache.roller.weblogger.business.WebloggerFactory;
    31  
    32  
    33  /**
    34   * Task for processing the ping queue at fixed intervals.   This is set up during context initialization by {@link
    35   * org.apache.roller.weblogger.ui.core.RollerContext}.  The queue processing interval is currently set from the configuration {@link
    36   * org.apache.roller.weblogger.config.PingConfig} at startup time only.
    37   *
    38   * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
    39   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.business.pings.PingQueueTask()
          * 
          *  Postconditions:
          *    this.clientId == null
          *    this.interval == 5
          *    this.leaseTime == 30
          *    this.startTimeDesc == &"immediate"
          */
    40  public class PingQueueTask extends RollerTaskWithLeasing {
    41      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.business.pings.PingQueueTask__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    42      private static Log log = LogFactory.getLog(PingQueueTask.class);
    43      
    44      // a unique id for this specific task instance
    45      // this is meant to be unique for each client in a clustered environment
    46      private String clientId = null;
    47      
    48      // a String description of when to start this task
    49      private String startTimeDesc = "immediate";
    50      
    51      // interval at which the task is run, default is 5 minutes
    52      private int interval = 5;
    53      
    54      // lease time given to task lock, default is 30 minutes
    55      private int leaseTime = 30;
    56      
    57      
    58      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Postconditions:
                  *    return_value == &"PingQueueTask"
                  */
    59          return "PingQueueTask";
    60      }
    61      
    62      public String getClientId() {
                 /* 
    P/P           *  Method: String getClientId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.clientId)
                  * 
                  *  Postconditions:
                  *    return_value == this.clientId
                  *    init'ed(return_value)
                  */
    63          return clientId;
    64      }
    65      
    66      public Date getStartTime(Date currentTime) {
                 /* 
    P/P           *  Method: Date getStartTime(Date)
                  * 
                  *  Preconditions:
                  *    init'ed(this.startTimeDesc)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    67          return getAdjustedTime(currentTime, startTimeDesc);
    68      }
    69      
    70      public String getStartTimeDesc() {
                 /* 
    P/P           *  Method: String getStartTimeDesc()
                  * 
                  *  Preconditions:
                  *    init'ed(this.startTimeDesc)
                  * 
                  *  Postconditions:
                  *    return_value == this.startTimeDesc
                  *    init'ed(return_value)
                  */
    71          return startTimeDesc;
    72      }
    73      
    74      public int getInterval() {
                 /* 
    P/P           *  Method: int getInterval()
                  * 
                  *  Preconditions:
                  *    init'ed(this.interval)
                  * 
                  *  Postconditions:
                  *    return_value == this.interval
                  *    init'ed(return_value)
                  */
    75          return this.interval;
    76      }
    77      
    78      public int getLeaseTime() {
                 /* 
    P/P           *  Method: int getLeaseTime()
                  * 
                  *  Preconditions:
                  *    init'ed(this.leaseTime)
                  * 
                  *  Postconditions:
                  *    return_value == this.leaseTime
                  *    init'ed(return_value)
                  */
    79          return this.leaseTime;
    80      }
    81      
    82      
    83      public void init() throws WebloggerException {
    84          
    85          // get relevant props
                 /* 
    P/P           *  Method: void init()
                  * 
                  *  Preconditions:
                  *    init'ed(org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance)
                  *    org/apache/roller/weblogger/config/WebloggerConfig.config != null
                  *    org/apache/roller/weblogger/config/WebloggerConfig.log != null
                  *    (soft) log != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                  *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                  * 
                  *  Postconditions:
                  *    org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance == One-of{old org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance, &new PingQueueProcessor(init#1*)}
                  *    org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance != null
                  *    possibly_updated(this.clientId)
                  *    possibly_updated(this.interval)
                  *    possibly_updated(this.leaseTime)
                  *    possibly_updated(this.startTimeDesc)
                  *    new PingQueueProcessor(init#1*) num objects <= 1
                  *    init'ed(new PingQueueProcessor(init#1*).pingQueueMgr)
                  * 
                  *  Test Vectors:
                  *    java.util.Properties:getProperty(...)@101: Addr_Set{null}, Inverse{null}
                  *    java.util.Properties:getProperty(...)@111: Addr_Set{null}, Inverse{null}
                  *    java.util.Properties:getProperty(...)@89: Addr_Set{null}, Inverse{null}
                  *    java.util.Properties:getProperty(...)@95: Addr_Set{null}, Inverse{null}
                  */
    86          Properties props = this.getTaskProperties();
    87          
    88          // extract clientId
    89          String client = props.getProperty("clientId");
    90          if(client != null) {
    91              this.clientId = client;
    92          }
    93          
    94          // extract start time
    95          String startTimeStr = props.getProperty("startTime");
    96          if(startTimeStr != null) {
    97              this.startTimeDesc = startTimeStr;
    98          }
    99          
   100          // extract interval
   101          String intervalStr = props.getProperty("interval");
   102          if(intervalStr != null) {
   103              try {
   104                  this.interval = Integer.parseInt(intervalStr);
   105              } catch (NumberFormatException ex) {
   106                  log.warn("Invalid interval: "+intervalStr);
   107              }
   108          }
   109          
   110          // extract lease time
   111          String leaseTimeStr = props.getProperty("leaseTime");
   112          if(leaseTimeStr != null) {
   113              try {
   114                  this.leaseTime = Integer.parseInt(leaseTimeStr);
   115              } catch (NumberFormatException ex) {
   116                  log.warn("Invalid leaseTime: "+leaseTimeStr);
   117              }
   118          }
   119          
   120          // initialize queue processor
   121          PingQueueProcessor.init();
   122      }
   123      
   124  
   125      /**
   126       * Run the task once.
   127       */
   128      public void runTask() {
   129          
   130          try {
                     /* 
    P/P               *  Method: void runTask()
                      * 
                      *  Preconditions:
                      *    log != null
                      *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                      *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                      *    (soft) org/apache/roller/weblogger/business/WebloggerImpl.log != null
                      *    (soft) org/apache/roller/weblogger/business/jpa/JPAPingQueueManagerImpl.log != null
                      *    (soft) org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance != null
                      *    (soft) org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance.pingQueueMgr != null
                      *    (soft) org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance.pingQueueMgr.strategy != null
                      *    (soft) org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance.pingQueueMgr.strategy.emf != null
                      *    (soft) org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance.pingQueueMgr.strategy.threadLocalEntityManager != null
                      *    ...
                      * 
                      *  Presumptions:
                      *    getWeblogger(...).autoPingManager != null
                      *    getWeblogger(...).bookmarkManager != null
                      *    getWeblogger(...).fileManager != null
                      *    getWeblogger(...).pingQueueManager != null
                      *    getWeblogger(...).pingTargetManager != null
                      *    ...
                      */
   131              log.debug("task started");
   132              
   133              PingQueueProcessor.getInstance().processQueue();
   134              WebloggerFactory.getWeblogger().flush();
   135              
   136              log.debug("task completed");
   137              
   138          } catch (WebloggerException e) {
   139              log.error("Error while processing ping queue", e);
   140          } catch (Exception ee) {
   141              log.error("unexpected exception", ee);
   142          } finally {
   143              // always release
   144              WebloggerFactory.getWeblogger().release();
   145          }
   146          
   147      }
   148      
   149      
   150      /**
   151       * Main method so that this task may be run from outside the webapp.
   152       */
   153      public static void main(String[] args) throws Exception {
   154          try {
                     /* 
    P/P               *  Method: void main(String[])
                      *    main does not return
                      * 
                      *  Preconditions:
                      *    (soft) log != null
                      *    (soft) init'ed(org/apache/roller/weblogger/business/pings/PingQueueProcessor.theInstance)
                      *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
                      *    (soft) org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
                      *    (soft) org/apache/roller/weblogger/config/WebloggerConfig.config != null
                      *    (soft) org/apache/roller/weblogger/config/WebloggerConfig.log != null
                      */
   155              PingQueueTask task = new PingQueueTask();
   156              task.init();
   157              task.run();
   158              System.exit(0);
   159          } catch (WebloggerException ex) {
   160              ex.printStackTrace();
   161              System.exit(-1);
   162          }
   163      }
   164      
   165  }








SofCheck Inspector Build Version : 2.18479
PingQueueTask.java 2009-Jan-02 14:24:54
PingQueueTask.class 2009-Sep-04 03:12:32