001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.auth;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class PrincipalThreadLocal {
026    
027            public static String getName() {
028                    String name = _name.get();
029    
030                    if (_log.isDebugEnabled()) {
031                            _log.debug("getName " + name);
032                    }
033    
034                    return name;
035            }
036    
037            public static String getPassword() {
038                    return _password.get();
039            }
040    
041            public static long getUserId() {
042                    return GetterUtil.getLong(getName());
043            }
044    
045            public static void setName(long name) {
046                    setName(String.valueOf(name));
047            }
048    
049            public static void setName(String name) {
050                    if (_log.isDebugEnabled()) {
051                            _log.debug("setName " + name);
052                    }
053    
054                    _name.set(name);
055            }
056    
057            public static void setPassword(String password) {
058                    _password.set(password);
059            }
060    
061            private static Log _log = LogFactoryUtil.getLog(PrincipalThreadLocal.class);
062    
063            private static ThreadLocal<String> _name = new AutoResetThreadLocal<String>(
064                    PrincipalThreadLocal.class + "._name");
065            private static ThreadLocal<String> _password =
066                    new AutoResetThreadLocal<String>(
067                            PrincipalThreadLocal.class + "._password");
068    
069    }