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.model.Company;
022 import com.liferay.portal.kernel.model.Group;
023 import com.liferay.portal.kernel.model.LayoutSet;
024 import com.liferay.portal.kernel.model.PortletCategory;
025 import com.liferay.portal.kernel.model.User;
026 import com.liferay.portal.kernel.model.VirtualHost;
027 import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
028 import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal;
029 import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
030 import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
031 import com.liferay.portal.kernel.service.LayoutSetLocalServiceUtil;
032 import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
033 import com.liferay.portal.kernel.service.UserLocalServiceUtil;
034 import com.liferay.portal.kernel.service.VirtualHostLocalServiceUtil;
035 import com.liferay.portal.kernel.util.ArrayUtil;
036 import com.liferay.portal.kernel.util.CookieKeys;
037 import com.liferay.portal.kernel.util.GetterUtil;
038 import com.liferay.portal.kernel.util.HttpUtil;
039 import com.liferay.portal.kernel.util.PortalUtil;
040 import com.liferay.portal.kernel.util.PropsKeys;
041 import com.liferay.portal.kernel.util.SetUtil;
042 import com.liferay.portal.kernel.util.Validator;
043 import com.liferay.portal.kernel.util.WebKeys;
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 long userId = PrincipalThreadLocal.getUserId();
385
386 if (userId > 0) {
387 User user = UserLocalServiceUtil.fetchUser(userId);
388
389 if ((user != null) && (user.getCompanyId() == companyId)) {
390 principalName = currentThreadPrincipalName;
391 }
392 }
393
394 PrincipalThreadLocal.setName(principalName);
395
396
397
398 if (_log.isDebugEnabled()) {
399 _log.debug("Initialize display");
400 }
401
402 try {
403 String xml = HttpUtil.URLtoString(
404 servletContext.getResource("/WEB-INF/liferay-display.xml"));
405
406 PortletCategory portletCategory =
407 (PortletCategory)WebAppPool.get(
408 companyId, WebKeys.PORTLET_CATEGORY);
409
410 if (portletCategory == null) {
411 portletCategory = new PortletCategory();
412 }
413
414 PortletCategory newPortletCategory =
415 PortletLocalServiceUtil.getEARDisplay(xml);
416
417 portletCategory.merge(newPortletCategory);
418
419 for (int i = 0; i < _companyIds.length; i++) {
420 long currentCompanyId = _companyIds[i];
421
422 PortletCategory currentPortletCategory =
423 (PortletCategory)WebAppPool.get(
424 currentCompanyId, WebKeys.PORTLET_CATEGORY);
425
426 if (currentPortletCategory != null) {
427 portletCategory.merge(currentPortletCategory);
428 }
429 }
430
431 WebAppPool.put(
432 companyId, WebKeys.PORTLET_CATEGORY, portletCategory);
433 }
434 catch (Exception e) {
435 _log.error(e, e);
436 }
437
438
439
440 if (_log.isDebugEnabled()) {
441 _log.debug("Process application startup events");
442 }
443
444 try {
445 EventsProcessorUtil.process(
446 PropsKeys.APPLICATION_STARTUP_EVENTS,
447 PropsValues.APPLICATION_STARTUP_EVENTS,
448 new String[] {String.valueOf(companyId)});
449 }
450 catch (Exception e) {
451 _log.error(e, e);
452 }
453
454
455
456 if (_log.isDebugEnabled()) {
457 _log.debug(
458 "End initializing company with web id " + webId +
459 " and company id " + companyId);
460 }
461
462 addCompanyId(companyId);
463 }
464 finally {
465 CompanyThreadLocal.setCompanyId(currentThreadCompanyId);
466
467 PrincipalThreadLocal.setName(currentThreadPrincipalName);
468 }
469
470 return companyId;
471 }
472
473 private boolean _isAutoLoginIgnoreHost(String host) {
474 return _autoLoginIgnoreHosts.contains(host);
475 }
476
477 private boolean _isAutoLoginIgnorePath(String path) {
478 return _autoLoginIgnorePaths.contains(path);
479 }
480
481 private boolean _isCompanyActive(long companyId) {
482 try {
483 Company company = CompanyLocalServiceUtil.fetchCompanyById(
484 companyId);
485
486 if (company != null) {
487 return company.isActive();
488 }
489 }
490 catch (Exception e) {
491 _log.error(e, e);
492 }
493
494 return false;
495 }
496
497 private boolean _isVirtualHostsIgnoreHost(String host) {
498 return _virtualHostsIgnoreHosts.contains(host);
499 }
500
501 private boolean _isVirtualHostsIgnorePath(String path) {
502 return _virtualHostsIgnorePaths.contains(path);
503 }
504
505 private void _reload(ServletContext servletContext) {
506 _companyIds = new long[0];
507 _webIds = null;
508
509 String[] webIds = _getWebIds();
510
511 for (String webId : webIds) {
512 _initCompany(servletContext, webId);
513 }
514 }
515
516 private void _removeCompanyId(long companyId) {
517 try {
518 EventsProcessorUtil.process(
519 PropsKeys.APPLICATION_SHUTDOWN_EVENTS,
520 PropsValues.APPLICATION_SHUTDOWN_EVENTS,
521 new String[] {String.valueOf(companyId)});
522 }
523 catch (Exception e) {
524 _log.error(e, e);
525 }
526
527 _companyIds = ArrayUtil.remove(_companyIds, companyId);
528 _webIds = null;
529
530 _getWebIds();
531
532 WebAppPool.remove(companyId, WebKeys.PORTLET_CATEGORY);
533 }
534
535 private static final String _GET_COMPANY_IDS =
536 "select companyId from Company";
537
538 private static final Log _log = LogFactoryUtil.getLog(
539 PortalInstances.class);
540
541 private static final PortalInstances _instance = new PortalInstances();
542
543 private final Set<String> _autoLoginIgnoreHosts;
544 private final Set<String> _autoLoginIgnorePaths;
545 private long[] _companyIds;
546 private final Set<String> _virtualHostsIgnoreHosts;
547 private final Set<String> _virtualHostsIgnorePaths;
548 private String[] _webIds;
549
550 }