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