1
14
15 package com.liferay.portal.security.auth;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.util.AutoResetThreadLocal;
20 import com.liferay.portal.kernel.util.LocaleThreadLocal;
21 import com.liferay.portal.kernel.util.TimeZoneThreadLocal;
22 import com.liferay.portal.model.Company;
23 import com.liferay.portal.model.CompanyConstants;
24 import com.liferay.portal.service.CompanyLocalServiceUtil;
25
26
31 public class CompanyThreadLocal {
32
33 public static Long getCompanyId() {
34 Long companyId = _companyId.get();
35
36 if (_log.isDebugEnabled()) {
37 _log.debug("getCompanyId " + companyId);
38 }
39
40 return companyId;
41 }
42
43 public static void setCompanyId(int companyId) {
44 setCompanyId(Long.valueOf(companyId));
45 }
46
47 public static void setCompanyId(Long companyId) {
48 if (_log.isDebugEnabled()) {
49 _log.debug("setCompanyId " + companyId);
50 }
51
52 if (companyId > 0) {
53 try {
54 Company company = CompanyLocalServiceUtil.getCompany(companyId);
55
56 LocaleThreadLocal.setLocale(company.getLocale());
57 TimeZoneThreadLocal.setTimeZone(company.getTimeZone());
58 }
59 catch (Exception e) {
60 _log.error(e, e);
61 }
62
63 _companyId.set(companyId);
64 }
65 else {
66 LocaleThreadLocal.setLocale(null);
67 TimeZoneThreadLocal.setTimeZone(null);
68
69 _companyId.set(CompanyConstants.SYSTEM);
70 }
71 }
72
73 private static Log _log = LogFactoryUtil.getLog(CompanyThreadLocal.class);
74
75 private static ThreadLocal<Long> _companyId =
76 new AutoResetThreadLocal<Long>(CompanyConstants.SYSTEM);
77
78 }