trackbacktokenmanager.java

### File Source: trackbacktokenmanager.java

             /*
        P/P   *  Method: void net.sourceforge.pebble.trackback.TrackBackTokenManager$1(TrackBackTokenManager)
              */
         1  package net.sourceforge.pebble.trackback;
         2
         3  import org.apache.commons.logging.Log;
         4  import org.apache.commons.logging.LogFactory;
         5
         6  import java.util.*;
         7
         8  /**
         9   * Manages tokens for generating TrackBack links.
        10   *
        11   * @author    Simon Brown
        12   */
        13  public class TrackBackTokenManager {
        14
               /*
        P/P     *  Method: Log access$0()
                *
                *  Postconditions:
                *    init'ed(return_value)
                */
        15    private static final Log log = LogFactory.getLog(TrackBackTokenManager.class);
        16
        17    private static final TrackBackTokenManager instance = new TrackBackTokenManager();
        18
        19    /** the time to live for new tokens */
        20    private static final long TIME_TO_LIVE = 1000 * 60 * 10; // 10 minutes
        21
        22    private Random random = new Random();
        23
        24    /** the map of tokens */
               /*
        P/P     *  Method: Map access$1(TrackBackTokenManager)
                *
                *  Preconditions:
                *    Param_0 != null
                *    init'ed(Param_0.tokens)
                *
                *  Postconditions:
                *    return_value == Param_0.tokens
                *    init'ed(return_value)
                */
        25    private Map<String,Date> tokens = new HashMap<String,Date>();
        26
        27    /**
        28     * Private constructor for the singleton pattern.
        29     */
               /*
        P/P     *  Method: void net.sourceforge.pebble.trackback.TrackBackTokenManager()
                *
                *  Postconditions:
                *    this.random == &new Random(TrackBackTokenManager#1)
                *    this.tokens == &new HashMap(TrackBackTokenManager#2)
                *    new HashMap(TrackBackTokenManager#2) num objects == 1
                *    new Random(TrackBackTokenManager#1) num objects == 1
                */
        30    private TrackBackTokenManager() {
        31      // create a new TimerTask that will purge invalid tokens
        32      TimerTask task = new TimerTask() {
        33        public void run() {
                     /*
        P/P           *  Method: void run()
                      *
                      *  Preconditions:
                      *    this.tokens != null
                      *
                      *  Presumptions:
                      *    java.util.Map:keySet(...)@36 != null
                      *    org.apache.commons.logging.LogFactory:getLog(...)@15 != null
                      *
                      *  Test Vectors:
                      *    java.util.Iterator:hasNext(...)@37: {1}, {0}
                      */
        34          synchronized (TrackBackTokenManager.this) {
        35            log.debug("Purging expired tokens");
        36            Iterator it = tokens.keySet().iterator();
        37            while (it.hasNext()) {
        38              String token = (String)it.next();
        39              if (!isValid(token)) {
        40                it.remove();
        41              }
        42            }
        43          }
        44        }
        45      };
        46
        47      Timer timer = new Timer();
        48      timer.schedule(task, 2 * TIME_TO_LIVE);
        49    }
        50
        51    /**
        52     * Gets the singleton instance of this class.
        53     *
        54     * @return    a TrackBackTokenManager instance
        55     */
        56    public static TrackBackTokenManager getInstance() {
                 /*
        P/P       *  Method: TrackBackTokenManager getInstance()
                  *
                  *  Postconditions:
                  *    return_value == &new TrackBackTokenManager(TrackBackTokenManager__static_init#1)
                  */
        57      return instance;
        58    }
        59
        60    /**
        61     * Generates a new token with a fixed time to live.
        62     *
        63     * @return  a new token
        64     */
        65    public synchronized String generateToken() {
                 /*
        P/P       *  Method: String generateToken()
                  *
                  *  Preconditions:
                  *    this.random != null
                  *    this.tokens != null
                  *
                  *  Postconditions:
                  *    return_value != null
                  */
        66      String token = "" + random.nextLong();
        67      tokens.put(token, new Date());
        68      return token;
        69    }
        70
        71    /**
        72     * Determines whether a given token is valid.
        73     *
        74     * @param token   the token to test
        75     * @return  true if the token is valid and hasn't expired, false otherwise
        76     */
        77    public synchronized boolean isValid(String token) {
                 /*
        P/P       *  Method: bool isValid(String)
                  *
                  *  Preconditions:
                  *    (soft) this.tokens != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    token: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@78: {1..232-1}, {0}
                  *    java.util.Map:get(...)@81: Addr_Set{null}, Inverse{null}
                  */
        78      if (token == null || token.length() == 0) {
        79        return false;
        80      } else {
        81        Date date = tokens.get(token);
        82        return (date != null) && (new Date().getTime() - date.getTime() <= TIME_TO_LIVE);
        83      }
        84    }
        85
        86    /**
        87     * Expires a given token.
        88     *
        89     * @param token   the token to be expired
        90     */
        91    public synchronized void expire(String token) {
                 /*
        P/P       *  Method: void expire(String)
                  *
                  *  Preconditions:
                  *    this.tokens != null
                  */
        92      tokens.remove(token);
        93    }
        94
        95  }

SofCheck Inspector Build Version : 2.22510

trackbacktokenmanager.java | 2010-Jun-25 19:40:32
trackbacktokenmanager.class | 2010-Jul-19 20:23:38
trackbacktokenmanager$1.class | 2010-Jul-19 20:23:38