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