001    /*
002     * ----------------------------------------------------------------------
003     * Copyright (C) 2009 Enrique Lara (k957@68k.org)
004     *
005     * TinLizard is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public License
007     * as published by the Free Software Foundation; either version 3.0
008     * of the License, or (at your option) any later version.
009     *
010     * TinLizard is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013     * GNU Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public License
016     * along with TinLizard. If not, see http://www.gnu.org/licenses/.
017     * ----------------------------------------------------------------------
018     */
019    package tinlizard.model;
020    
021    import java.util.Locale;
022    
023    import org.apache.log4j.Logger;
024    
025    /**
026     * User for the current thread.
027     */
028    public final class CurrentUser {
029        protected static final Logger LOG = Logger.getLogger(CurrentUser.class);
030        private static final ThreadLocal<ThreadLocalUser> CURRENT_USER = new ThreadLocal<ThreadLocalUser>();
031    
032        private CurrentUser() {
033        }
034    
035        private static ThreadLocalUser getUser() {
036            ThreadLocalUser user = CURRENT_USER.get();
037    
038            if (user == null) {
039                user = new ThreadLocalUser();
040                user.username = "ANONYMOUS";
041                CURRENT_USER.set(user);
042            }
043    
044            return user;
045        }
046    
047        public static String getUsername() {
048            return getUser().username;
049        }
050    
051        public static Locale getLocale() {
052            return getUser().locale;
053        }
054    
055        public static void setUsername(final String username) {
056            getUser().username = username;
057        }
058    
059        //XXX where to set this?
060        public static void setLocale(final Locale locale) {
061            getUser().locale = locale;
062        }
063    
064        public static void clearCurrentUser() {
065            CURRENT_USER.set(null);
066        }
067    
068        private static class ThreadLocalUser {
069            String username;
070            Locale locale = Locale.getDefault();
071        }
072    }