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(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.setDefaultLocale(company.getLocale());
055 TimeZoneThreadLocal.setDefaultTimeZone(company.getTimeZone());
056 }
057 catch (Exception e) {
058 _log.error(e, e);
059 }
060
061 _companyId.set(companyId);
062 }
063 else {
064 LocaleThreadLocal.setDefaultLocale(null);
065 TimeZoneThreadLocal.setDefaultTimeZone(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 }