001
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
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 }