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