001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.LocaleThreadLocal;
021    import com.liferay.portal.kernel.util.TimeZoneThreadLocal;
022    import com.liferay.portal.model.Company;
023    import com.liferay.portal.model.CompanyConstants;
024    import com.liferay.portal.service.CompanyLocalServiceUtil;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class CompanyThreadLocal {
030    
031            public static Long getCompanyId() {
032                    Long companyId = _companyId.get();
033    
034                    if (_log.isDebugEnabled()) {
035                            _log.debug("getCompanyId " + companyId);
036                    }
037    
038                    return companyId;
039            }
040    
041            public static void setCompanyId(int companyId) {
042                    setCompanyId(Long.valueOf(companyId));
043            }
044    
045            public static void setCompanyId(Long companyId) {
046                    if (_log.isDebugEnabled()) {
047                            _log.debug("setCompanyId " + companyId);
048                    }
049    
050                    if (companyId > 0) {
051                            try {
052                                    Company company = CompanyLocalServiceUtil.getCompany(companyId);
053    
054                                    LocaleThreadLocal.setLocale(company.getLocale());
055                                    TimeZoneThreadLocal.setTimeZone(company.getTimeZone());
056                            }
057                            catch (Exception e) {
058                                    _log.error(e, e);
059                            }
060    
061                            _companyId.set(companyId);
062                    }
063                    else {
064                            LocaleThreadLocal.setLocale(null);
065                            TimeZoneThreadLocal.setTimeZone(null);
066    
067                            _companyId.set(CompanyConstants.SYSTEM);
068                    }
069            }
070    
071            private static Log _log = LogFactoryUtil.getLog(CompanyThreadLocal.class);
072    
073            private static ThreadLocal<Long> _companyId =
074                    new AutoResetThreadLocal<Long>(
075                            CompanyThreadLocal.class + "._companyId", CompanyConstants.SYSTEM);
076    
077    }