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