File Source: RefreshRollerPlanetTask.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.planet.tasks;
    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.planet.business.GuicePlanetProvider;
    26  import org.apache.roller.planet.business.startup.PlanetStartup;
    27  import org.apache.roller.weblogger.business.runnable.RollerTaskWithLeasing;
    28  import org.apache.roller.planet.business.PlanetFactory;
    29  import org.apache.roller.planet.business.PlanetProvider;
    30  import org.apache.roller.planet.business.updater.FeedUpdater;
    31  import org.apache.roller.planet.business.updater.SingleThreadedFeedUpdater;
    32  import org.apache.roller.weblogger.WebloggerException;
    33  import org.apache.roller.weblogger.business.WebloggerFactory;
    34  import org.apache.roller.weblogger.config.WebloggerConfig;
    35  
    36  
    37  /**
    38   * Updates Planet aggregator's database of feed entries.
    39   * <pre>
    40   * - Designed to be run outside of Roller via the TaskRunner class
    41   * - Calls Planet business layer to refresh entries
    42   * </pre>
    43   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.planet.tasks.RefreshRollerPlanetTask()
          * 
          *  Postconditions:
          *    this.clientId == &"unspecifiedClientId"
          *    this.interval == 60
          *    this.leaseTime == 10
          *    this.startTimeDesc == &"immediate"
          */
    44  public class RefreshRollerPlanetTask extends RollerTaskWithLeasing {
    45      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.planet.tasks.RefreshRollerPlanetTask__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    46      private static Log log = LogFactory.getLog(RefreshRollerPlanetTask.class);
    47      
    48      // a unique id for this specific task instance
    49      // this is meant to be unique for each client in a clustered environment
    50      private String clientId = "unspecifiedClientId";
    51      
    52      // a String description of when to start this task
    53      private String startTimeDesc = "immediate";
    54      
    55      // interval at which the task is run, default is 60 minutes
    56      private int interval = 60;
    57      
    58      // lease time given to task, default is 10 minutes
    59      private int leaseTime = 10;
    60      
    61      
    62      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Postconditions:
                  *    return_value == &"RefreshRollerPlanetTask"
                  */
    63          return "RefreshRollerPlanetTask";
    64      }
    65      
    66      public String getClientId() {
                 /* 
    P/P           *  Method: String getClientId()
                  * 
                  *  Preconditions:
                  *    init'ed(this.clientId)
                  * 
                  *  Postconditions:
                  *    return_value == this.clientId
                  *    init'ed(return_value)
                  */
    67          return clientId;
    68      }
    69      
    70      public Date getStartTime(Date currentTime) {
                 /* 
    P/P           *  Method: Date getStartTime(Date)
                  * 
                  *  Preconditions:
                  *    init'ed(this.startTimeDesc)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    71          return getAdjustedTime(currentTime, startTimeDesc);
    72      }
    73      
    74      public String getStartTimeDesc() {
                 /* 
    P/P           *  Method: String getStartTimeDesc()
                  * 
                  *  Preconditions:
                  *    init'ed(this.startTimeDesc)
                  * 
                  *  Postconditions:
                  *    return_value == this.startTimeDesc
                  *    init'ed(return_value)
                  */
    75          return startTimeDesc;
    76      }
    77      
    78      public int getInterval() {
                 /* 
    P/P           *  Method: int getInterval()
                  * 
                  *  Preconditions:
                  *    init'ed(this.interval)
                  * 
                  *  Postconditions:
                  *    return_value == this.interval
                  *    init'ed(return_value)
                  */
    79          return this.interval;
    80      }
    81      
    82      public int getLeaseTime() {
                 /* 
    P/P           *  Method: int getLeaseTime()
                  * 
                  *  Preconditions:
                  *    init'ed(this.leaseTime)
                  * 
                  *  Postconditions:
                  *    return_value == this.leaseTime
                  *    init'ed(return_value)
                  */
    83          return this.leaseTime;
    84      }
    85      
    86      
    87      @Override
    88      public void init() throws WebloggerException {
    89          
    90          // get relevant props
                 /* 
    P/P           *  Method: void init()
                  * 
                  *  Preconditions:
                  *    (soft) log != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.planet.tasks.RefreshRollerPlanetTask:getTaskProperties(...)@91 != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.clientId)
                  *    possibly_updated(this.interval)
                  *    possibly_updated(this.leaseTime)
                  *    possibly_updated(this.startTimeDesc)
                  * 
                  *  Test Vectors:
                  *    java.util.Properties:getProperty(...)@100: Addr_Set{null}, Inverse{null}
                  *    java.util.Properties:getProperty(...)@106: Addr_Set{null}, Inverse{null}
                  *    java.util.Properties:getProperty(...)@116: Addr_Set{null}, Inverse{null}
                  *    java.util.Properties:getProperty(...)@94: Addr_Set{null}, Inverse{null}
                  */
    91          Properties props = this.getTaskProperties();
    92          
    93          // extract clientId
    94          String client = props.getProperty("clientId");
    95          if(client != null) {
    96              this.clientId = client;
    97          }
    98          
    99          // extract start time
   100          String startTimeStr = props.getProperty("startTime");
   101          if(startTimeStr != null) {
   102              this.startTimeDesc = startTimeStr;
   103          }
   104          
   105          // extract interval
   106          String intervalStr = props.getProperty("interval");
   107          if(intervalStr != null) {
   108              try {
   109                  this.interval = Integer.parseInt(intervalStr);
   110              } catch (NumberFormatException ex) {
   111                  log.warn("Invalid interval: "+intervalStr);
   112              }
   113          }
   114          
   115          // extract lease time
   116          String leaseTimeStr = props.getProperty("leaseTime");
   117          if(leaseTimeStr != null) {
   118              try {
   119                  this.leaseTime = Integer.parseInt(leaseTimeStr);
   120              } catch (NumberFormatException ex) {
   121                  log.warn("Invalid leaseTime: "+leaseTimeStr);
   122              }
   123          }
   124      }
   125      
   126      
   127      public void runTask() {
   128          try {
                     /* 
    P/P               *  Method: void runTask()
                      * 
                      *  Preconditions:
                      *    log != null
                      * 
                      *  Presumptions:
                      *    org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@139 != null
                      *    org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@138 != null
                      */
   129              log.info("Refreshing Planet subscriptions");
   130              
   131              FeedUpdater updater = new SingleThreadedFeedUpdater();
   132              updater.updateSubscriptions();
   133              
   134          } catch (Throwable t) {
   135              log.error("ERROR refreshing planet", t);
   136          } finally {
   137              // always release
   138              WebloggerFactory.getWeblogger().release();
   139              PlanetFactory.getPlanet().release();
   140          }
   141      }
   142      
   143      
   144      public static void main(String[] args) throws Exception {
   145          
   146          // before we can do anything we need to bootstrap the planet backend
                 /* 
    P/P           *  Method: void main(String[])
                  * 
                  *  Preconditions:
                  *    (soft) log != null
                  */
   147          PlanetStartup.prepare();
   148          
   149          // we need to use our own planet provider for integration
   150          String guiceModule = WebloggerConfig.getProperty("planet.aggregator.guice.module");
   151          PlanetProvider provider = new GuicePlanetProvider(guiceModule);
   152          PlanetFactory.bootstrap(provider);
   153                          
   154          RefreshRollerPlanetTask task = new RefreshRollerPlanetTask();
   155          task.init();
   156          task.run();
   157      }
   158      
   159  }








SofCheck Inspector Build Version : 2.18479
RefreshRollerPlanetTask.java 2009-Jan-02 14:25:42
RefreshRollerPlanetTask.class 2009-Sep-04 03:12:46