1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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(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  }