001    /**
002     * Copyright (c) 2000-2011 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.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(long companyId) {
042                    if (_log.isDebugEnabled()) {
043                            _log.debug("setCompanyId " + companyId);
044                    }
045    
046                    if (companyId > 0) {
047                            try {
048                                    Company company = CompanyLocalServiceUtil.getCompany(companyId);
049    
050                                    LocaleThreadLocal.setLocale(company.getLocale());
051                                    TimeZoneThreadLocal.setTimeZone(company.getTimeZone());
052                            }
053                            catch (Exception e) {
054                                    _log.error(e, e);
055                            }
056    
057                            _companyId.set(companyId);
058                    }
059                    else {
060                            LocaleThreadLocal.setLocale(null);
061                            TimeZoneThreadLocal.setTimeZone(null);
062    
063                            _companyId.set(CompanyConstants.SYSTEM);
064                    }
065            }
066    
067            private static Log _log = LogFactoryUtil.getLog(CompanyThreadLocal.class);
068    
069            private static ThreadLocal<Long> _companyId =
070                    new AutoResetThreadLocal<Long>(
071                            CompanyThreadLocal.class + "._companyId", CompanyConstants.SYSTEM);
072    
073    }