1
2
3
4
5
6
7
8
9
10
11
12package security.user;
13
14public class UserContext
15{
16 private static final ThreadLocal<UserContext> CURRENT = new ThreadLocal<UserContext>();
17
18 private User _user;
19
20 public static UserContext begin(User user)
21 {
22 checkNotActive();
23 UserContext context = new UserContext(user);
24 CURRENT.set(context);
25 return context;
26 }
27
28 private static void checkNotActive()
29 {
30 UserContext active = CURRENT.get();
31 if (active != null)
32 {
33 throw new IllegalStateException("There is already an active user context: " + active);
34 }
35 }
36
37 public static UserContext get()
38 {
39 UserContext active = CURRENT.get();
40 if (active == null)
41 {
42 throw new IllegalStateException("There is no active user context");
43 }
44 return active;
45 }
46
47 private UserContext(User user)
48 {
49 _user = user;
50 }
51
52 public void end()
53 {
54 CURRENT.set(null);
55 _user = null;
56 }
57
58 public String toString()
59 {
60 return getClass().getSimpleName() + ":" + _user.toString();
61 }
62
63 public User getUser()
64 {
65 return _user;
66 }
67}
// Copyright 2007, Tim Vernum
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package security.user;
public class UserContext
{
private static final ThreadLocal CURRENT = new ThreadLocal();
private User _user;
public static UserContext begin(User user)
{
checkNotActive();
UserContext context = new UserContext(user);
CURRENT.set(context);
return context;
}
private static void checkNotActive()
{
UserContext active = CURRENT.get();
if (active != null)
{
throw new IllegalStateException("There is already an active user context: " + active);
}
}
public static UserContext get()
{
UserContext active = CURRENT.get();
if (active == null)
{
throw new IllegalStateException("There is no active user context");
}
return active;
}
private UserContext(User user)
{
_user = user;
}
public void end()
{
CURRENT.set(null);
_user = null;
}
public String toString()
{
return getClass().getSimpleName() + ":" + _user.toString();
}
public User getUser()
{
return _user;
}
}