File Source: HitCountProcessingJob.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.HashMap;
    22  import java.util.Iterator;
    23  import java.util.List;
    24  import java.util.Map;
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27  import org.apache.roller.weblogger.WebloggerException;
    28  import org.apache.roller.weblogger.business.HitCountQueue;
    29  import org.apache.roller.weblogger.business.WebloggerFactory;
    30  import org.apache.roller.weblogger.business.UserManager;
    31  import org.apache.roller.weblogger.business.WeblogManager;
    32  import org.apache.roller.weblogger.pojos.Weblog;
    33  
    34  
    35  /**
    36   * A job which gathers the currently queued hits from the HitCountQueue and
    37   * stores them in the database.
    38   */
    39  public class HitCountProcessingJob implements Job {
    40      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.business.runnable.HitCountProcessingJob__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    41      private static Log log = LogFactory.getLog(HitCountProcessingJob.class);
    42      
    43      
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.business.runnable.HitCountProcessingJob()
              */
    44      public HitCountProcessingJob() {}
    45      
    46      
    47      /**
    48       * Execute the job.
    49       *
    50       * We want to extract the currently queued hits from the HitCounter and
    51       * then propogate them to the db for persistent storage.
    52       */
    53      public void execute() {
    54          
                 /* 
    P/P           *  Method: void execute()
                  * 
                  *  Preconditions:
                  *    log != null
                  *    init'ed(org/apache/roller/weblogger/business/HitCountQueue.instance.queue)
                  *    org/apache/roller/weblogger/business/HitCountQueue.instance != 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
                  *    (soft) org/apache/roller/weblogger/business/jpa/JPAUserManagerImpl.log != null
                  * 
                  *  Presumptions:
                  *    getWeblogger(...).autoPingManager != null
                  *    getWeblogger(...).bookmarkManager != null
                  *    getWeblogger(...).fileManager != null
                  *    getWeblogger(...).pingQueueManager != null
                  *    getWeblogger(...).pingTargetManager != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    init'ed(org/apache/roller/weblogger/business/HitCountQueue.instance.queue)
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@88: {0}, {1}
                  *    java.util.Map:get(...)@72: Inverse{null}, Addr_Set{null}
                  */
    55          UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
    56          WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
    57          
    58          HitCountQueue hitCounter = HitCountQueue.getInstance();
    59          
    60          // first get the current set of hits
    61          List currentHits = hitCounter.getHits();
    62          
    63          // now reset the queued hits
    64          hitCounter.resetHits();
    65          
    66          // tally the counts, grouped by weblog handle
    67          Map hitsTally = new HashMap();
    68          String weblogHandle = null;
    69          for(int i=0; i < currentHits.size(); i++) {
    70              weblogHandle = (String) currentHits.get(i);
    71              
    72              Long count = (Long) hitsTally.get(weblogHandle);
    73              if(count == null) {
    74                  count = new Long(1);
    75              } else {
    76                  count = new Long(count.longValue()+1);
    77              }
    78              hitsTally.put(weblogHandle, count);
    79          }
    80          
    81          // iterate over the tallied hits and store them in the db
    82          try {
    83              long startTime = System.currentTimeMillis();
    84              
    85              Weblog weblog = null;
    86              String key = null;
    87              Iterator it = hitsTally.keySet().iterator();
    88              while(it.hasNext()) {
    89                  key = (String) it.next();
    90                  
    91                  try {
    92                      weblog = umgr.getWebsiteByHandle(key);
+   93                      wmgr.incrementHitCount(weblog, ((Long)hitsTally.get(key)).intValue());
    94                  } catch (WebloggerException ex) {
    95                      log.error(ex);
    96                  }
    97              }
    98              
    99              // flush the results to the db
   100              WebloggerFactory.getWeblogger().flush();
   101              
   102              long endTime = System.currentTimeMillis();
   103              
   104              log.debug("Completed: "+ (endTime-startTime)/1000 + " secs");
   105              
   106          } catch (WebloggerException ex) {
   107              log.error("Error persisting updated hit counts", ex);
   108          } finally {
   109              // release session
   110              WebloggerFactory.getWeblogger().release();
   111          }
   112      }
   113      
   114      
   115      public void input(Map input) {
   116          // no-op
             /* 
    P/P       *  Method: void input(Map)
              */
   117      }
   118      
   119      public Map output() {
                 /* 
    P/P           *  Method: Map output()
                  * 
                  *  Postconditions:
                  *    return_value == null
                  */
   120          return null;
   121      }
   122      
   123  }








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