Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart7.png 33% of files have more coverage
19   95   10   3,17
6   47   0,53   6
6     1,67  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  TrackBackTokenManager       Line # 13 19 0% 10 12 61,3% 0.61290324
 
  (8)
 
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   
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 */
25    private Map<String,Date> tokens = new HashMap<String,Date>();
26   
27    /**
28    * Private constructor for the singleton pattern.
29    */
 
30  2 toggle private TrackBackTokenManager() {
31    // create a new TimerTask that will purge invalid tokens
32  2 TimerTask task = new TimerTask() {
 
33  0 toggle public void run() {
34  0 synchronized (TrackBackTokenManager.this) {
35  0 log.debug("Purging expired tokens");
36  0 Iterator it = tokens.keySet().iterator();
37  0 while (it.hasNext()) {
38  0 String token = (String)it.next();
39  0 if (!isValid(token)) {
40  0 it.remove();
41    }
42    }
43    }
44    }
45    };
46   
47  2 Timer timer = new Timer();
48  2 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  8 toggle public static TrackBackTokenManager getInstance() {
57  8 return instance;
58    }
59   
60    /**
61    * Generates a new token with a fixed time to live.
62    *
63    * @return a new token
64    */
 
65  12 toggle public synchronized String generateToken() {
66  12 String token = "" + random.nextLong();
67  12 tokens.put(token, new Date());
68  12 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  12 toggle public synchronized boolean isValid(String token) {
78  12 if (token == null || token.length() == 0) {
79  4 return false;
80    } else {
81  8 Date date = tokens.get(token);
82  8 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  2 toggle public synchronized void expire(String token) {
92  2 tokens.remove(token);
93    }
94   
95    }