01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: PurgingRememberManager.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.authentication.remembermanagers;
09:
10: import com.uwyn.rife.authentication.RememberManager;
11: import com.uwyn.rife.authentication.exceptions.RememberManagerException;
12: import com.uwyn.rife.config.RifeConfig;
13: import java.util.Random;
14:
15: public class PurgingRememberManager implements RememberManager {
16: private int mRememberPurgeFrequency = RifeConfig.Authentication
17: .getRememberPurgeFrequency();
18: private int mRememberPurgeScale = RifeConfig.Authentication
19: .getRememberPurgeScale();
20:
21: private final Random mRandom = new Random();
22:
23: private RememberManager mRememberManager = null;
24:
25: public PurgingRememberManager(RememberManager rememberManager) {
26: if (null == rememberManager)
27: throw new IllegalArgumentException(
28: "rememberManager can't be null");
29:
30: mRememberManager = rememberManager;
31: }
32:
33: public RememberManager getRememberManager() {
34: return mRememberManager;
35: }
36:
37: public int getRememberPurgeFrequency() {
38: return mRememberPurgeFrequency;
39: }
40:
41: public void setRememberPurgeFrequency(int frequency) {
42: mRememberPurgeFrequency = frequency;
43: }
44:
45: public int getRememberPurgeScale() {
46: return mRememberPurgeScale;
47: }
48:
49: public void setRememberPurgeScale(int scale) {
50: mRememberPurgeScale = scale;
51: }
52:
53: public long getRememberDuration() {
54: return mRememberManager.getRememberDuration();
55: }
56:
57: public void setRememberDuration(long milliseconds) {
58: mRememberManager.setRememberDuration(milliseconds);
59: }
60:
61: public String createRememberId(long userId, String hostIp)
62: throws RememberManagerException {
63: int purge_decision = -1;
64: synchronized (mRandom) {
65: purge_decision = mRandom.nextInt(mRememberPurgeScale);
66: }
67: if (purge_decision <= mRememberPurgeFrequency) {
68: purgeRememberIds();
69: }
70:
71: return mRememberManager.createRememberId(userId, hostIp);
72: }
73:
74: public boolean eraseRememberId(String rememberId)
75: throws RememberManagerException {
76: return mRememberManager.eraseRememberId(rememberId);
77: }
78:
79: public boolean eraseUserRememberIds(long userId)
80: throws RememberManagerException {
81: return mRememberManager.eraseUserRememberIds(userId);
82: }
83:
84: public void eraseAllRememberIds() throws RememberManagerException {
85: mRememberManager.eraseAllRememberIds();
86: }
87:
88: public long getRememberedUserId(String rememberId)
89: throws RememberManagerException {
90: return mRememberManager.getRememberedUserId(rememberId);
91: }
92:
93: public void purgeRememberIds() throws RememberManagerException {
94: mRememberManager.purgeRememberIds();
95: }
96: }
|