1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
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  /**
27   * <a href="CompanyThreadLocal.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
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  }