001
014
015 package com.liferay.portal.util;
016
017 import com.liferay.portal.events.EventsProcessorUtil;
018 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.search.SearchEngineHelperUtil;
022 import com.liferay.portal.kernel.util.ArrayUtil;
023 import com.liferay.portal.kernel.util.CookieKeys;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.HttpUtil;
026 import com.liferay.portal.kernel.util.PropsKeys;
027 import com.liferay.portal.kernel.util.SetUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.kernel.util.WebKeys;
030 import com.liferay.portal.model.Company;
031 import com.liferay.portal.model.Group;
032 import com.liferay.portal.model.LayoutSet;
033 import com.liferay.portal.model.PortletCategory;
034 import com.liferay.portal.model.User;
035 import com.liferay.portal.model.VirtualHost;
036 import com.liferay.portal.security.auth.CompanyThreadLocal;
037 import com.liferay.portal.security.auth.PrincipalThreadLocal;
038 import com.liferay.portal.service.CompanyLocalServiceUtil;
039 import com.liferay.portal.service.GroupLocalServiceUtil;
040 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
041 import com.liferay.portal.service.PortletLocalServiceUtil;
042 import com.liferay.portal.service.UserLocalServiceUtil;
043 import com.liferay.portal.service.VirtualHostLocalServiceUtil;
044
045 import java.sql.Connection;
046 import java.sql.PreparedStatement;
047 import java.sql.ResultSet;
048 import java.sql.SQLException;
049
050 import java.util.ArrayList;
051 import java.util.List;
052 import java.util.Set;
053
054 import javax.servlet.ServletContext;
055 import javax.servlet.http.HttpServletRequest;
056
057
063 public class PortalInstances {
064
065 public static void addCompanyId(long companyId) {
066 _instance._addCompanyId(companyId);
067 }
068
069 public static long getCompanyId(HttpServletRequest request) {
070 return _instance._getCompanyId(request);
071 }
072
073 public static long[] getCompanyIds() {
074 return _instance._getCompanyIds();
075 }
076
077 public static long[] getCompanyIdsBySQL() throws SQLException {
078 return _instance._getCompanyIdsBySQL();
079 }
080
081 public static long getDefaultCompanyId() {
082 return _instance._getDefaultCompanyId();
083 }
084
085 public static String[] getWebIds() {
086 return _instance._getWebIds();
087 }
088
089 public static long initCompany(
090 ServletContext servletContext, String webId) {
091
092 return _instance._initCompany(servletContext, webId);
093 }
094
095 public static boolean isAutoLoginIgnoreHost(String host) {
096 return _instance._isAutoLoginIgnoreHost(host);
097 }
098
099 public static boolean isAutoLoginIgnorePath(String path) {
100 return _instance._isAutoLoginIgnorePath(path);
101 }
102
103 public static boolean isCompanyActive(long companyId) {
104 return _instance._isCompanyActive(companyId);
105 }
106
107 public static boolean isVirtualHostsIgnoreHost(String host) {
108 return _instance._isVirtualHostsIgnoreHost(host);
109 }
110
111 public static boolean isVirtualHostsIgnorePath(String path) {
112 return _instance._isVirtualHostsIgnorePath(path);
113 }
114
115 public static void reload(ServletContext servletContext) {
116 _instance._reload(servletContext);
117 }
118
119 public static void removeCompany(long companyId) {
120 _instance._removeCompanyId(companyId);
121 }
122
123 private PortalInstances() {
124 _companyIds = new long[0];
125 _autoLoginIgnoreHosts = SetUtil.fromArray(
126 PropsUtil.getArray(PropsKeys.AUTO_LOGIN_IGNORE_HOSTS));
127 _autoLoginIgnorePaths = SetUtil.fromArray(
128 PropsUtil.getArray(PropsKeys.AUTO_LOGIN_IGNORE_PATHS));
129 _virtualHostsIgnoreHosts = SetUtil.fromArray(
130 PropsUtil.getArray(PropsKeys.VIRTUAL_HOSTS_IGNORE_HOSTS));
131 _virtualHostsIgnorePaths = SetUtil.fromArray(
132 PropsUtil.getArray(PropsKeys.VIRTUAL_HOSTS_IGNORE_PATHS));
133 }
134
135 private void _addCompanyId(long companyId) {
136 if (ArrayUtil.contains(_companyIds, companyId)) {
137 return;
138 }
139
140 long[] companyIds = new long[_companyIds.length + 1];
141
142 System.arraycopy(_companyIds, 0, companyIds, 0, _companyIds.length);
143
144 companyIds[_companyIds.length] = companyId;
145
146 _companyIds = companyIds;
147 }
148
149 private long _getCompanyId(HttpServletRequest request) {
150 if (_log.isDebugEnabled()) {
151 _log.debug("Get company id");
152 }
153
154 Long companyIdObj = (Long)request.getAttribute(WebKeys.COMPANY_ID);
155
156 if (_log.isDebugEnabled()) {
157 _log.debug("Company id from request " + companyIdObj);
158 }
159
160 if (companyIdObj != null) {
161 return companyIdObj.longValue();
162 }
163
164 long companyId = _getCompanyIdByVirtualHosts(request);
165
166 if (_log.isDebugEnabled()) {
167 _log.debug("Company id from host " + companyId);
168 }
169
170 if (companyId <= 0) {
171 long cookieCompanyId = GetterUtil.getLong(
172 CookieKeys.getCookie(request, CookieKeys.COMPANY_ID, false));
173
174 if (cookieCompanyId > 0) {
175 try {
176 if (CompanyLocalServiceUtil.fetchCompanyById(
177 cookieCompanyId) == null) {
178
179 if (_log.isWarnEnabled()) {
180 _log.warn(
181 "Company id from cookie " + cookieCompanyId +
182 " does not exist");
183 }
184 }
185 else {
186 companyId = cookieCompanyId;
187
188 if (_log.isDebugEnabled()) {
189 _log.debug("Company id from cookie " + companyId);
190 }
191 }
192 }
193 catch (Exception e) {
194 _log.error(e, e);
195 }
196 }
197 }
198
199 if (companyId <= 0) {
200 companyId = _getDefaultCompanyId();
201
202 if (_log.isDebugEnabled()) {
203 _log.debug("Default company id " + companyId);
204 }
205 }
206
207 if (_log.isDebugEnabled()) {
208 _log.debug("Set company id " + companyId);
209 }
210
211 request.setAttribute(WebKeys.COMPANY_ID, Long.valueOf(companyId));
212
213 CompanyThreadLocal.setCompanyId(companyId);
214
215 if (Validator.isNotNull(PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME) &&
216 (request.getAttribute(WebKeys.VIRTUAL_HOST_LAYOUT_SET) == null)) {
217
218 try {
219 Group group = GroupLocalServiceUtil.getGroup(
220 companyId, PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);
221
222 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
223 group.getGroupId(), false);
224
225 if (Validator.isNull(layoutSet.getVirtualHostname())) {
226 request.setAttribute(
227 WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
228 }
229 }
230 catch (Exception e) {
231 _log.error(e, e);
232 }
233 }
234
235 return companyId;
236 }
237
238 private long _getCompanyIdByVirtualHosts(HttpServletRequest request) {
239 String host = PortalUtil.getHost(request);
240
241 if (_log.isDebugEnabled()) {
242 _log.debug("Host " + host);
243 }
244
245 if (Validator.isNull(host) || _isVirtualHostsIgnoreHost(host)) {
246 return 0;
247 }
248
249 try {
250 VirtualHost virtualHost =
251 VirtualHostLocalServiceUtil.fetchVirtualHost(host);
252
253 if (virtualHost == null) {
254 return 0;
255 }
256
257 if (virtualHost.getLayoutSetId() != 0) {
258 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
259 virtualHost.getLayoutSetId());
260
261 if (_log.isDebugEnabled()) {
262 _log.debug(
263 "Company " + virtualHost.getCompanyId() +
264 " is associated with layout set " +
265 virtualHost.getLayoutSetId());
266 }
267
268 request.setAttribute(
269 WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
270 }
271
272 return virtualHost.getCompanyId();
273 }
274 catch (Exception e) {
275 _log.error(e, e);
276 }
277
278 return 0;
279 }
280
281 private long[] _getCompanyIds() {
282 return _companyIds;
283 }
284
285 private long[] _getCompanyIdsBySQL() throws SQLException {
286 List<Long> companyIds = new ArrayList<>();
287
288 Connection con = null;
289 PreparedStatement ps = null;
290 ResultSet rs = null;
291
292 try {
293 con = DataAccess.getConnection();
294
295 ps = con.prepareStatement(_GET_COMPANY_IDS);
296
297 rs = ps.executeQuery();
298
299 while (rs.next()) {
300 long companyId = rs.getLong("companyId");
301
302 companyIds.add(companyId);
303 }
304 }
305 finally {
306 DataAccess.cleanUp(con, ps, rs);
307 }
308
309 return ArrayUtil.toArray(
310 companyIds.toArray(new Long[companyIds.size()]));
311 }
312
313 private long _getDefaultCompanyId() {
314 return _companyIds[0];
315 }
316
317 private String[] _getWebIds() {
318 if (_webIds != null) {
319 return _webIds;
320 }
321
322 if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
323 throw new RuntimeException("Default web id must not be null");
324 }
325
326 try {
327 List<Company> companies = CompanyLocalServiceUtil.getCompanies(
328 false);
329
330 List<String> webIdsList = new ArrayList<>(companies.size());
331
332 for (Company company : companies) {
333 String webId = company.getWebId();
334
335 if (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
336 webIdsList.add(0, webId);
337 }
338 else {
339 webIdsList.add(webId);
340 }
341 }
342
343 _webIds = webIdsList.toArray(new String[webIdsList.size()]);
344 }
345 catch (Exception e) {
346 _log.error(e, e);
347 }
348
349 if (ArrayUtil.isEmpty(_webIds)) {
350 _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID};
351 }
352
353 return _webIds;
354 }
355
356 private long _initCompany(ServletContext servletContext, String webId) {
357
358
359
360 if (_log.isDebugEnabled()) {
361 _log.debug("Begin initializing company with web id " + webId);
362 }
363
364 long companyId = 0;
365
366 try {
367 Company company = CompanyLocalServiceUtil.checkCompany(webId);
368
369 companyId = company.getCompanyId();
370 }
371 catch (Exception e) {
372 _log.error(e, e);
373 }
374
375 Long currentThreadCompanyId = CompanyThreadLocal.getCompanyId();
376
377 String currentThreadPrincipalName = PrincipalThreadLocal.getName();
378
379 try {
380 CompanyThreadLocal.setCompanyId(companyId);
381
382 String principalName = null;
383
384 try {
385 User user = UserLocalServiceUtil.getUser(
386 PrincipalThreadLocal.getUserId());
387
388 if (user.getCompanyId() == companyId) {
389 principalName = currentThreadPrincipalName;
390 }
391 }
392 catch (Exception e) {
393 }
394
395 PrincipalThreadLocal.setName(principalName);
396
397
398
399 if (_log.isDebugEnabled()) {
400 _log.debug("Initialize display");
401 }
402
403 try {
404 String xml = HttpUtil.URLtoString(
405 servletContext.getResource("/WEB-INF/liferay-display.xml"));
406
407 PortletCategory portletCategory =
408 (PortletCategory)WebAppPool.get(
409 companyId, WebKeys.PORTLET_CATEGORY);
410
411 if (portletCategory == null) {
412 portletCategory = new PortletCategory();
413 }
414
415 PortletCategory newPortletCategory =
416 PortletLocalServiceUtil.getEARDisplay(xml);
417
418 portletCategory.merge(newPortletCategory);
419
420 for (int i = 0; i < _companyIds.length; i++) {
421 long currentCompanyId = _companyIds[i];
422
423 PortletCategory currentPortletCategory =
424 (PortletCategory)WebAppPool.get(
425 currentCompanyId, WebKeys.PORTLET_CATEGORY);
426
427 if (currentPortletCategory != null) {
428 portletCategory.merge(currentPortletCategory);
429 }
430 }
431
432 WebAppPool.put(
433 companyId, WebKeys.PORTLET_CATEGORY, portletCategory);
434 }
435 catch (Exception e) {
436 _log.error(e, e);
437 }
438
439
440
441 if (_log.isDebugEnabled()) {
442 _log.debug("Process application startup events");
443 }
444
445 try {
446 EventsProcessorUtil.process(
447 PropsKeys.APPLICATION_STARTUP_EVENTS,
448 PropsValues.APPLICATION_STARTUP_EVENTS,
449 new String[] {String.valueOf(companyId)});
450 }
451 catch (Exception e) {
452 _log.error(e, e);
453 }
454
455
456
457 if (_log.isDebugEnabled()) {
458 _log.debug(
459 "End initializing company with web id " + webId +
460 " and company id " + companyId);
461 }
462
463 addCompanyId(companyId);
464 }
465 finally {
466 CompanyThreadLocal.setCompanyId(currentThreadCompanyId);
467
468 PrincipalThreadLocal.setName(currentThreadPrincipalName);
469 }
470
471 return companyId;
472 }
473
474 private boolean _isAutoLoginIgnoreHost(String host) {
475 return _autoLoginIgnoreHosts.contains(host);
476 }
477
478 private boolean _isAutoLoginIgnorePath(String path) {
479 return _autoLoginIgnorePaths.contains(path);
480 }
481
482 private boolean _isCompanyActive(long companyId) {
483 try {
484 Company company = CompanyLocalServiceUtil.fetchCompanyById(
485 companyId);
486
487 if (company != null) {
488 return company.isActive();
489 }
490 }
491 catch (Exception e) {
492 _log.error(e, e);
493 }
494
495 return false;
496 }
497
498 private boolean _isVirtualHostsIgnoreHost(String host) {
499 return _virtualHostsIgnoreHosts.contains(host);
500 }
501
502 private boolean _isVirtualHostsIgnorePath(String path) {
503 return _virtualHostsIgnorePaths.contains(path);
504 }
505
506 private void _reload(ServletContext servletContext) {
507 _companyIds = new long[0];
508 _webIds = null;
509
510 String[] webIds = _getWebIds();
511
512 for (String webId : webIds) {
513 _initCompany(servletContext, webId);
514 }
515 }
516
517 private void _removeCompanyId(long companyId) {
518 _companyIds = ArrayUtil.remove(_companyIds, companyId);
519 _webIds = null;
520
521 _getWebIds();
522
523 SearchEngineHelperUtil.removeCompany(companyId);
524
525 WebAppPool.remove(companyId, WebKeys.PORTLET_CATEGORY);
526 }
527
528 private static final String _GET_COMPANY_IDS =
529 "select companyId from Company";
530
531 private static final Log _log = LogFactoryUtil.getLog(
532 PortalInstances.class);
533
534 private static final PortalInstances _instance = new PortalInstances();
535
536 private final Set<String> _autoLoginIgnoreHosts;
537 private final Set<String> _autoLoginIgnorePaths;
538 private long[] _companyIds;
539 private final Set<String> _virtualHostsIgnoreHosts;
540 private final Set<String> _virtualHostsIgnorePaths;
541 private String[] _webIds;
542
543 }