2 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
\r
4 * This library is free software; you can redistribute it and/or modify it under
\r
5 * the terms of the GNU Lesser General Public License as published by the Free
\r
6 * Software Foundation; either version 2.1 of the License, or (at your option)
\r
9 * This library is distributed in the hope that it will be useful, but WITHOUT
\r
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
\r
15 package com.liferay.portal.util;
\r
17 import java.sql.Connection;
\r
18 import java.sql.PreparedStatement;
\r
19 import java.sql.ResultSet;
\r
20 import java.sql.SQLException;
\r
21 import java.util.ArrayList;
\r
22 import java.util.List;
\r
23 import java.util.Set;
\r
25 import javax.servlet.ServletContext;
\r
26 import javax.servlet.http.HttpServletRequest;
\r
28 import com.liferay.portal.NoSuchCompanyException;
\r
29 import com.liferay.portal.events.EventsProcessorUtil;
\r
30 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
\r
31 import com.liferay.portal.kernel.dao.shard.ShardUtil;
\r
32 import com.liferay.portal.kernel.log.Log;
\r
33 import com.liferay.portal.kernel.log.LogFactoryUtil;
\r
34 import com.liferay.portal.kernel.util.ArrayUtil;
\r
35 import com.liferay.portal.kernel.util.GetterUtil;
\r
36 import com.liferay.portal.kernel.util.HttpUtil;
\r
37 import com.liferay.portal.kernel.util.PropsKeys;
\r
38 import com.liferay.portal.kernel.util.SetUtil;
\r
39 import com.liferay.portal.kernel.util.Validator;
\r
40 import com.liferay.portal.model.Company;
\r
41 import com.liferay.portal.model.Group;
\r
42 import com.liferay.portal.model.LayoutSet;
\r
43 import com.liferay.portal.model.PortletCategory;
\r
44 import com.liferay.portal.model.VirtualHost;
\r
45 import com.liferay.portal.search.lucene.LuceneHelperUtil;
\r
46 import com.liferay.portal.security.auth.CompanyThreadLocal;
\r
47 import com.liferay.portal.security.ldap.LDAPSettingsUtil;
\r
48 import com.liferay.portal.security.ldap.PortalLDAPUtil;
\r
49 import com.liferay.portal.service.CompanyLocalServiceUtil;
\r
50 import com.liferay.portal.service.GroupLocalServiceUtil;
\r
51 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
\r
52 import com.liferay.portal.service.PortletLocalServiceUtil;
\r
53 import com.liferay.portal.service.VirtualHostLocalServiceUtil;
\r
54 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
\r
55 import com.pentila.entSavoie.synchroLdap.impl.SynchronizationManager;
\r
58 * @author Brian Wing Shun Chan
\r
59 * @author Jose Oliver
\r
60 * @author Atul Patel
\r
61 * @author Mika Koivisto
\r
63 public class PortalInstances {
\r
65 public static void addCompanyId(long companyId) {
\r
66 _instance._addCompanyId(companyId);
\r
69 public static long getCompanyId(HttpServletRequest request) {
\r
70 return _instance._getCompanyId(request);
\r
73 public static long[] getCompanyIds() {
\r
74 return _instance._getCompanyIds();
\r
77 public static long[] getCompanyIdsBySQL() throws SQLException {
\r
78 return _instance._getCompanyIdsBySQL();
\r
81 public static long getDefaultCompanyId() {
\r
82 return _instance._getDefaultCompanyId();
\r
85 public static String[] getWebIds() {
\r
86 return _instance._getWebIds();
\r
89 public static long initCompany(
\r
90 ServletContext servletContext, String webId) {
\r
92 return _instance._initCompany(servletContext, webId);
\r
95 public static boolean isAutoLoginIgnoreHost(String host) {
\r
96 return _instance._isAutoLoginIgnoreHost(host);
\r
99 public static boolean isAutoLoginIgnorePath(String path) {
\r
100 return _instance._isAutoLoginIgnorePath(path);
\r
103 public static boolean isCompanyActive(long companyId) {
\r
104 return _instance._isCompanyActive(companyId);
\r
107 public static boolean isVirtualHostsIgnoreHost(String host) {
\r
108 return _instance._isVirtualHostsIgnoreHost(host);
\r
111 public static boolean isVirtualHostsIgnorePath(String path) {
\r
112 return _instance._isVirtualHostsIgnorePath(path);
\r
115 public static void reload(ServletContext servletContext) {
\r
116 _instance._reload(servletContext);
\r
119 private PortalInstances() {
\r
120 _companyIds = new long[0];
\r
121 _autoLoginIgnoreHosts = SetUtil.fromArray(
\r
122 PropsUtil.getArray(PropsKeys.AUTO_LOGIN_IGNORE_HOSTS));
\r
123 _autoLoginIgnorePaths = SetUtil.fromArray(
\r
124 PropsUtil.getArray(PropsKeys.AUTO_LOGIN_IGNORE_PATHS));
\r
125 _virtualHostsIgnoreHosts = SetUtil.fromArray(
\r
126 PropsUtil.getArray(PropsKeys.VIRTUAL_HOSTS_IGNORE_HOSTS));
\r
127 _virtualHostsIgnorePaths = SetUtil.fromArray(
\r
128 PropsUtil.getArray(PropsKeys.VIRTUAL_HOSTS_IGNORE_PATHS));
\r
131 private void _addCompanyId(long companyId) {
\r
132 if (ArrayUtil.contains(_companyIds, companyId)) {
\r
136 long[] companyIds = new long[_companyIds.length + 1];
\r
138 System.arraycopy(_companyIds, 0, companyIds, 0, _companyIds.length);
\r
140 companyIds[_companyIds.length] = companyId;
\r
142 _companyIds = companyIds;
\r
145 private long _getCompanyId(HttpServletRequest request) {
\r
146 if (_log.isDebugEnabled()) {
\r
147 _log.debug("Get company id");
\r
150 Long companyIdObj = (Long)request.getAttribute(WebKeys.COMPANY_ID);
\r
152 if (_log.isDebugEnabled()) {
\r
153 _log.debug("Company id from request " + companyIdObj);
\r
156 if (companyIdObj != null) {
\r
157 return companyIdObj.longValue();
\r
160 long companyId = _getCompanyIdByVirtualHosts(request);
\r
162 if (_log.isDebugEnabled()) {
\r
163 _log.debug("Company id from host " + companyId);
\r
166 if (companyId <= 0) {
\r
167 long cookieCompanyId = GetterUtil.getLong(
\r
168 CookieKeys.getCookie(request, CookieKeys.COMPANY_ID, false));
\r
170 if (cookieCompanyId > 0) {
\r
172 CompanyLocalServiceUtil.getCompanyById(cookieCompanyId);
\r
174 companyId = cookieCompanyId;
\r
176 if (_log.isDebugEnabled()) {
\r
177 _log.debug("Company id from cookie " + companyId);
\r
180 catch (NoSuchCompanyException nsce) {
\r
181 if (_log.isWarnEnabled()) {
\r
183 "Company id from cookie " + cookieCompanyId +
\r
184 " does not exist");
\r
187 catch (Exception e) {
\r
193 if (companyId <= 0) {
\r
194 companyId = _getDefaultCompanyId();
\r
196 if (_log.isDebugEnabled()) {
\r
197 _log.debug("Default company id " + companyId);
\r
201 if (_log.isDebugEnabled()) {
\r
202 _log.debug("Set company id " + companyId);
\r
205 request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
\r
207 CompanyThreadLocal.setCompanyId(companyId);
\r
209 if (Validator.isNotNull(PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME) &&
\r
210 (request.getAttribute(WebKeys.VIRTUAL_HOST_LAYOUT_SET) == null)) {
\r
213 Group group = GroupLocalServiceUtil.getGroup(
\r
214 companyId, PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);
\r
216 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
\r
217 group.getGroupId(), false);
\r
219 if (Validator.isNull(layoutSet.getVirtualHostname())) {
\r
220 request.setAttribute(
\r
221 WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
\r
224 catch (Exception e) {
\r
232 private long _getCompanyIdByVirtualHosts(HttpServletRequest request) {
\r
233 String host = PortalUtil.getHost(request);
\r
235 if (_log.isDebugEnabled()) {
\r
236 _log.debug("Host " + host);
\r
239 if (Validator.isNull(host) || _isVirtualHostsIgnoreHost(host)) {
\r
244 VirtualHost virtualHost =
\r
245 VirtualHostLocalServiceUtil.fetchVirtualHost(host);
\r
247 if (virtualHost == null) {
\r
251 if (virtualHost.getLayoutSetId() != 0) {
\r
252 LayoutSet layoutSet = null;
\r
255 ShardUtil.pushCompanyService(virtualHost.getCompanyId());
\r
257 layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
\r
258 virtualHost.getLayoutSetId());
\r
261 ShardUtil.popCompanyService();
\r
264 if (_log.isDebugEnabled()) {
\r
266 "Company " + virtualHost.getCompanyId() +
\r
267 " is associated with layout set " +
\r
268 virtualHost.getLayoutSetId());
\r
271 request.setAttribute(
\r
272 WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
\r
275 return virtualHost.getCompanyId();
\r
277 catch (Exception e) {
\r
284 private long[] _getCompanyIds() {
\r
285 return _companyIds;
\r
288 private long[] _getCompanyIdsBySQL() throws SQLException {
\r
289 List<Long> companyIds = new ArrayList<Long>();
\r
291 Connection con = null;
\r
292 PreparedStatement ps = null;
\r
293 ResultSet rs = null;
\r
296 con = DataAccess.getConnection();
\r
298 ps = con.prepareStatement(_GET_COMPANY_IDS);
\r
300 rs = ps.executeQuery();
\r
302 while (rs.next()) {
\r
303 long companyId = rs.getLong("companyId");
\r
305 companyIds.add(companyId);
\r
309 DataAccess.cleanUp(con, ps, rs);
\r
312 return ArrayUtil.toArray(
\r
313 companyIds.toArray(new Long[companyIds.size()]));
\r
316 private long _getDefaultCompanyId() {
\r
317 return _companyIds[0];
\r
320 private String[] _getWebIds() {
\r
321 if (_webIds != null) {
\r
325 if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
\r
326 throw new RuntimeException("Default web id must not be null");
\r
330 List<Company> companies = CompanyLocalServiceUtil.getCompanies(
\r
333 List<String> webIdsList = new ArrayList<String>(companies.size());
\r
335 for (Company company : companies) {
\r
336 String webId = company.getWebId();
\r
338 if (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
\r
339 webIdsList.add(0, webId);
\r
342 webIdsList.add(webId);
\r
346 _webIds = webIdsList.toArray(new String[webIdsList.size()]);
\r
348 catch (Exception e) {
\r
352 if ((_webIds == null) || (_webIds.length == 0)) {
\r
353 _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID};
\r
359 private long _initCompany(ServletContext servletContext, String webId) {
\r
361 // Begin initializing company
\r
363 if (_log.isDebugEnabled()) {
\r
364 _log.debug("Begin initializing company with web id " + webId);
\r
367 long companyId = 0;
\r
370 Company company = CompanyLocalServiceUtil.checkCompany(webId);
\r
372 companyId = company.getCompanyId();
\r
374 catch (Exception e) {
\r
378 CompanyThreadLocal.setCompanyId(companyId);
\r
382 LuceneHelperUtil.startup(companyId);
\r
384 // Initialize display
\r
386 if (_log.isDebugEnabled()) {
\r
387 _log.debug("Initialize display");
\r
391 String xml = HttpUtil.URLtoString(servletContext.getResource(
\r
392 "/WEB-INF/liferay-display.xml"));
\r
394 PortletCategory portletCategory = (PortletCategory)WebAppPool.get(
\r
395 companyId, WebKeys.PORTLET_CATEGORY);
\r
397 if (portletCategory == null) {
\r
398 portletCategory = new PortletCategory();
\r
401 PortletCategory newPortletCategory =
\r
402 PortletLocalServiceUtil.getEARDisplay(xml);
\r
404 portletCategory.merge(newPortletCategory);
\r
406 for (int i = 0; i < _companyIds.length; i++) {
\r
407 long currentCompanyId = _companyIds[i];
\r
409 PortletCategory currentPortletCategory =
\r
410 (PortletCategory)WebAppPool.get(
\r
411 currentCompanyId, WebKeys.PORTLET_CATEGORY);
\r
413 if (currentPortletCategory != null) {
\r
414 portletCategory.merge(currentPortletCategory);
\r
419 companyId, WebKeys.PORTLET_CATEGORY, portletCategory);
\r
421 catch (Exception e) {
\r
425 // Check journal content search
\r
427 if (_log.isDebugEnabled()) {
\r
428 _log.debug("Check journal content search");
\r
431 if (GetterUtil.getBoolean(
\r
433 PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) {
\r
436 JournalContentSearchLocalServiceUtil.checkContentSearches(
\r
439 catch (Exception e) {
\r
447 if (LDAPSettingsUtil.isImportOnStartup(companyId)) {
\r
448 SynchronizationManager.runSynchronization();
\r
451 catch (Exception e) {
\r
455 // Process application startup events
\r
457 if (_log.isDebugEnabled()) {
\r
458 _log.debug("Process application startup events");
\r
462 EventsProcessorUtil.process(
\r
463 PropsKeys.APPLICATION_STARTUP_EVENTS,
\r
464 PropsValues.APPLICATION_STARTUP_EVENTS,
\r
465 new String[] {String.valueOf(companyId)});
\r
467 catch (Exception e) {
\r
471 // End initializing company
\r
473 if (_log.isDebugEnabled()) {
\r
475 "End initializing company with web id " + webId +
\r
476 " and company id " + companyId);
\r
479 addCompanyId(companyId);
\r
484 private boolean _isAutoLoginIgnoreHost(String host) {
\r
485 return _autoLoginIgnoreHosts.contains(host);
\r
488 private boolean _isAutoLoginIgnorePath(String path) {
\r
489 return _autoLoginIgnorePaths.contains(path);
\r
492 private boolean _isCompanyActive(long companyId) {
\r
494 Company company = CompanyLocalServiceUtil.fetchCompanyById(
\r
497 if (company != null) {
\r
498 return company.isActive();
\r
501 catch (Exception e) {
\r
508 private boolean _isVirtualHostsIgnoreHost(String host) {
\r
509 return _virtualHostsIgnoreHosts.contains(host);
\r
512 private boolean _isVirtualHostsIgnorePath(String path) {
\r
513 return _virtualHostsIgnorePaths.contains(path);
\r
516 private void _reload(ServletContext servletContext) {
\r
517 _companyIds = new long[0];
\r
520 String[] webIds = _getWebIds();
\r
522 for (String webId : webIds) {
\r
523 _initCompany(servletContext, webId);
\r
527 private static final String _GET_COMPANY_IDS =
\r
528 "select companyId from Company";
\r
530 private static Log _log = LogFactoryUtil.getLog(PortalInstances.class);
\r
532 private static PortalInstances _instance = new PortalInstances();
\r
534 private Set<String> _autoLoginIgnoreHosts;
\r
535 private Set<String> _autoLoginIgnorePaths;
\r
536 private long[] _companyIds;
\r
537 private Set<String> _virtualHostsIgnoreHosts;
\r
538 private Set<String> _virtualHostsIgnorePaths;
\r
539 private String[] _webIds;
\r