| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TrackBackTokenManager |
|
| 2.0;2 | ||||
| TrackBackTokenManager$1 |
|
| 2.0;2 |
| 1 | /* | |
| 2 | * Copyright (c) 2003-2011, Simon Brown | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions are met: | |
| 7 | * | |
| 8 | * - Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * | |
| 11 | * - Redistributions in binary form must reproduce the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer in | |
| 13 | * the documentation and/or other materials provided with the | |
| 14 | * distribution. | |
| 15 | * | |
| 16 | * - Neither the name of Pebble nor the names of its contributors may | |
| 17 | * be used to endorse or promote products derived from this software | |
| 18 | * without specific prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 30 | * POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | ||
| 33 | package net.sourceforge.pebble.trackback; | |
| 34 | ||
| 35 | import org.apache.commons.logging.Log; | |
| 36 | import org.apache.commons.logging.LogFactory; | |
| 37 | ||
| 38 | import java.util.*; | |
| 39 | ||
| 40 | /** | |
| 41 | * Manages tokens for generating TrackBack links. | |
| 42 | * | |
| 43 | * @author Simon Brown | |
| 44 | */ | |
| 45 | 0 | public class TrackBackTokenManager { |
| 46 | ||
| 47 | 4 | private static final Log log = LogFactory.getLog(TrackBackTokenManager.class); |
| 48 | ||
| 49 | 4 | private static final TrackBackTokenManager instance = new TrackBackTokenManager(); |
| 50 | ||
| 51 | /** the time to live for new tokens */ | |
| 52 | private static final long TIME_TO_LIVE = 1000 * 60 * 10; // 10 minutes | |
| 53 | ||
| 54 | 4 | private Random random = new Random(); |
| 55 | ||
| 56 | /** the map of tokens */ | |
| 57 | 4 | private Map<String,Date> tokens = new HashMap<String,Date>(); |
| 58 | ||
| 59 | /** | |
| 60 | * Private constructor for the singleton pattern. | |
| 61 | */ | |
| 62 | 4 | private TrackBackTokenManager() { |
| 63 | // create a new TimerTask that will purge invalid tokens | |
| 64 | 4 | TimerTask task = new TimerTask() { |
| 65 | public void run() { | |
| 66 | 0 | synchronized (TrackBackTokenManager.this) { |
| 67 | 0 | log.debug("Purging expired tokens"); |
| 68 | 0 | Iterator it = tokens.keySet().iterator(); |
| 69 | 0 | while (it.hasNext()) { |
| 70 | 0 | String token = (String)it.next(); |
| 71 | 0 | if (!isValid(token)) { |
| 72 | 0 | it.remove(); |
| 73 | } | |
| 74 | 0 | } |
| 75 | 0 | } |
| 76 | 0 | } |
| 77 | }; | |
| 78 | ||
| 79 | 4 | Timer timer = new Timer(); |
| 80 | 4 | timer.schedule(task, 2 * TIME_TO_LIVE); |
| 81 | 4 | } |
| 82 | ||
| 83 | /** | |
| 84 | * Gets the singleton instance of this class. | |
| 85 | * | |
| 86 | * @return a TrackBackTokenManager instance | |
| 87 | */ | |
| 88 | public static TrackBackTokenManager getInstance() { | |
| 89 | 16 | return instance; |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * Generates a new token with a fixed time to live. | |
| 94 | * | |
| 95 | * @return a new token | |
| 96 | */ | |
| 97 | public synchronized String generateToken() { | |
| 98 | 24 | String token = "" + random.nextLong(); |
| 99 | 24 | tokens.put(token, new Date()); |
| 100 | 24 | return token; |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Determines whether a given token is valid. | |
| 105 | * | |
| 106 | * @param token the token to test | |
| 107 | * @return true if the token is valid and hasn't expired, false otherwise | |
| 108 | */ | |
| 109 | public synchronized boolean isValid(String token) { | |
| 110 | 24 | if (token == null || token.length() == 0) { |
| 111 | 8 | return false; |
| 112 | } else { | |
| 113 | 16 | Date date = tokens.get(token); |
| 114 | 16 | return (date != null) && (new Date().getTime() - date.getTime() <= TIME_TO_LIVE); |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | /** | |
| 119 | * Expires a given token. | |
| 120 | * | |
| 121 | * @param token the token to be expired | |
| 122 | */ | |
| 123 | public synchronized void expire(String token) { | |
| 124 | 4 | tokens.remove(token); |
| 125 | 4 | } |
| 126 | ||
| 127 | } |