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(long companyId) {
44 if (_log.isDebugEnabled()) {
45 _log.debug("setCompanyId " + companyId);
46 }
47
48 if (companyId > 0) {
49 try {
50 Company company = CompanyLocalServiceUtil.getCompany(companyId);
51
52 LocaleThreadLocal.setLocale(company.getLocale());
53 TimeZoneThreadLocal.setTimeZone(company.getTimeZone());
54 }
55 catch (Exception e) {
56 _log.error(e, e);
57 }
58
59 _companyId.set(companyId);
60 }
61 else {
62 LocaleThreadLocal.setLocale(null);
63 TimeZoneThreadLocal.setTimeZone(null);
64
65 _companyId.set(CompanyConstants.SYSTEM);
66 }
67 }
68
69 private static Log _log = LogFactoryUtil.getLog(CompanyThreadLocal.class);
70
71 private static ThreadLocal<Long> _companyId =
72 new AutoResetThreadLocal<Long>(
73 CompanyThreadLocal.class + "._companyId", CompanyConstants.SYSTEM);
74
75 }