KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > security > UserLogonStats


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16
17 package org.apache.jetspeed.services.security;
18
19 import java.util.Date JavaDoc;
20
21 /**
22  * A User's statistics for logon attempts.
23  *
24  *
25  * @author <a HREF="mailto:david@bluesunrise.com">David Sean Taylor</a>
26  * @version $Id: UserLogonStats.java,v 1.2 2004/02/23 03:58:11 jford Exp $
27  */

28 public class UserLogonStats
29 {
30     private int failures = 0;
31     private int total = 0;
32     private long firstLogon = 0;
33     private final String JavaDoc username;
34     private boolean disabled = false;
35     private Object JavaDoc sem;
36
37     UserLogonStats(String JavaDoc username)
38     {
39         this.username = username;
40         sem = new Object JavaDoc();
41     }
42
43     public int getFailures()
44     {
45         return failures;
46     }
47
48     public int getTotalFailures()
49     {
50         return total;
51     }
52
53     public long getFirstLogon()
54     {
55         return firstLogon;
56     }
57
58     public String JavaDoc getUserName()
59     {
60         return username;
61     }
62
63     public boolean failCheck(int allowed, long secondsAllowed, int max)
64     {
65         synchronized(sem)
66         {
67             if (disabled)
68                 return true;
69
70             failures = failures + 1;
71             total = total + 1;
72     
73             if (total >= max)
74             {
75                 reset();
76                 disabled = true;
77                 return true;
78             }
79         
80             long msAllowed = secondsAllowed * 1000;
81             long now = new Date JavaDoc().getTime();
82            
83             if (firstLogon == 0)
84                 firstLogon = now;
85     
86             long diff = now - firstLogon;
87     
88             if (diff > msAllowed)
89                 reset();
90     
91             if (failures >= allowed)
92             {
93                 reset();
94                 disabled = true;
95                 return true;
96             }
97             return false;
98         }
99     }
100
101     public void reset()
102     {
103         synchronized(sem)
104         {
105             failures = 0;
106             Date JavaDoc now = new Date JavaDoc();
107             firstLogon = now.getTime();
108             disabled = false;
109         }
110     }
111 }
112
Popular Tags