View Javadoc

1   /*
2    * ----------------------------------------------------------------------
3    * Copyright (C) 2009 Enrique Lara (k957@68k.org)
4    *
5    * TinLizard is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public License
7    * as published by the Free Software Foundation; either version 3.0
8    * of the License, or (at your option) any later version.
9    *
10   * TinLizard is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with TinLizard. If not, see http://www.gnu.org/licenses/.
17   * ----------------------------------------------------------------------
18   */
19  package tinlizard.model;
20  
21  import java.util.Locale;
22  
23  import org.apache.log4j.Logger;
24  
25  /***
26   * User for the current thread.
27   */
28  public final class CurrentUser {
29      protected static final Logger LOG = Logger.getLogger(CurrentUser.class);
30      private static final ThreadLocal<ThreadLocalUser> CURRENT_USER = new ThreadLocal<ThreadLocalUser>();
31  
32      private CurrentUser() {
33      }
34  
35      private static ThreadLocalUser getUser() {
36          ThreadLocalUser user = CURRENT_USER.get();
37  
38          if (user == null) {
39              user = new ThreadLocalUser();
40              user.username = "ANONYMOUS";
41              CURRENT_USER.set(user);
42          }
43  
44          return user;
45      }
46  
47      public static String getUsername() {
48          return getUser().username;
49      }
50  
51      public static Locale getLocale() {
52          return getUser().locale;
53      }
54  
55      public static void setUsername(final String username) {
56          getUser().username = username;
57      }
58  
59      //XXX where to set this?
60      public static void setLocale(final Locale locale) {
61          getUser().locale = locale;
62      }
63  
64      public static void clearCurrentUser() {
65          CURRENT_USER.set(null);
66      }
67  
68      private static class ThreadLocalUser {
69          String username;
70          Locale locale = Locale.getDefault();
71      }
72  }