1
14
15 package com.liferay.portal.util;
16
17 import com.liferay.portal.NoSuchCompanyException;
18 import com.liferay.portal.events.EventsProcessorUtil;
19 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
20 import com.liferay.portal.kernel.log.Log;
21 import com.liferay.portal.kernel.log.LogFactoryUtil;
22 import com.liferay.portal.kernel.util.ArrayUtil;
23 import com.liferay.portal.kernel.util.GetterUtil;
24 import com.liferay.portal.kernel.util.HttpUtil;
25 import com.liferay.portal.kernel.util.PropsKeys;
26 import com.liferay.portal.kernel.util.SetUtil;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.model.Company;
29 import com.liferay.portal.model.Group;
30 import com.liferay.portal.model.LayoutSet;
31 import com.liferay.portal.model.PortletCategory;
32 import com.liferay.portal.security.auth.CompanyThreadLocal;
33 import com.liferay.portal.security.ldap.PortalLDAPUtil;
34 import com.liferay.portal.service.CompanyLocalServiceUtil;
35 import com.liferay.portal.service.GroupLocalServiceUtil;
36 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
37 import com.liferay.portal.service.PortletLocalServiceUtil;
38 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
39
40 import java.sql.Connection;
41 import java.sql.PreparedStatement;
42 import java.sql.ResultSet;
43 import java.sql.SQLException;
44
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Set;
48
49 import javax.servlet.ServletContext;
50 import javax.servlet.http.HttpServletRequest;
51
52
60 public class PortalInstances {
61
62 public static final String DEFAULT_VIRTUAL_HOST = "localhost";
63
64 public static void addCompanyId(long companyId) {
65 _instance._addCompanyId(companyId);
66 }
67
68 public static long getCompanyId(HttpServletRequest request) {
69 return _instance._getCompanyId(request);
70 }
71
72 public static long[] getCompanyIds() {
73 return _instance._getCompanyIds();
74 }
75
76 public static long[] getCompanyIdsBySQL() throws SQLException {
77 return _instance._getCompanyIdsBySQL();
78 }
79
80 public static long getDefaultCompanyId() {
81 return _instance._getDefaultCompanyId();
82 }
83
84 public static String[] getWebIds() {
85 return _instance._getWebIds();
86 }
87
88 public static long initCompany(
89 ServletContext servletContext, String webId) {
90
91 return _instance._initCompany(servletContext, webId);
92 }
93
94 public static boolean isAutoLoginIgnoreHost(String host) {
95 return _instance._isAutoLoginIgnoreHost(host);
96 }
97
98 public static boolean isAutoLoginIgnorePath(String path) {
99 return _instance._isAutoLoginIgnorePath(path);
100 }
101
102 public static boolean isVirtualHostsIgnoreHost(String host) {
103 return _instance._isVirtualHostsIgnoreHost(host);
104 }
105
106 public static boolean isVirtualHostsIgnorePath(String path) {
107 return _instance._isVirtualHostsIgnorePath(path);
108 }
109
110 private PortalInstances() {
111 _companyIds = new long[0];
112 _autoLoginIgnoreHosts = SetUtil.fromArray(PropsUtil.getArray(
113 PropsKeys.AUTO_LOGIN_IGNORE_HOSTS));
114 _autoLoginIgnorePaths = SetUtil.fromArray(PropsUtil.getArray(
115 PropsKeys.AUTO_LOGIN_IGNORE_PATHS));
116 _virtualHostsIgnoreHosts = SetUtil.fromArray(PropsUtil.getArray(
117 PropsKeys.VIRTUAL_HOSTS_IGNORE_HOSTS));
118 _virtualHostsIgnorePaths = SetUtil.fromArray(PropsUtil.getArray(
119 PropsKeys.VIRTUAL_HOSTS_IGNORE_PATHS));
120 }
121
122 private void _addCompanyId(long companyId) {
123 if (ArrayUtil.contains(_companyIds, companyId)) {
124 return;
125 }
126
127 long[] companyIds = new long[_companyIds.length + 1];
128
129 System.arraycopy(
130 _companyIds, 0, companyIds, 0, _companyIds.length);
131
132 companyIds[_companyIds.length] = companyId;
133
134 _companyIds = companyIds;
135 }
136
137 private long _getCompanyId(HttpServletRequest request) {
138 if (_log.isDebugEnabled()) {
139 _log.debug("Get company id");
140 }
141
142 Long companyIdObj = (Long)request.getAttribute(WebKeys.COMPANY_ID);
143
144 if (_log.isDebugEnabled()) {
145 _log.debug("Company id from request " + companyIdObj);
146 }
147
148 if (companyIdObj != null) {
149 return companyIdObj.longValue();
150 }
151
152 String host = PortalUtil.getHost(request);
153
154 if (_log.isDebugEnabled()) {
155 _log.debug("Host " + host);
156 }
157
158 long companyId = _getCompanyIdByVirtualHosts(host);
159
160 if (_log.isDebugEnabled()) {
161 _log.debug("Company id from host " + companyId);
162 }
163
164 if (companyId <= 0) {
165 LayoutSet layoutSet = _getLayoutSetByVirtualHosts(host);
166
167 if (layoutSet != null) {
168 companyId = layoutSet.getCompanyId();
169
170 if (_log.isDebugEnabled()) {
171 _log.debug(
172 "Company id " + companyId + " is associated with " +
173 "layout set " + layoutSet.getLayoutSetId());
174 }
175
176 request.setAttribute(
177 WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
178 }
179 }
180 else if (Validator.isNotNull(
181 PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME)) {
182
183 try {
184 Group group = GroupLocalServiceUtil.getGroup(
185 companyId,
186 PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME);
187
188 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
189 group.getGroupId(), false);
190
191 if (Validator.isNull(layoutSet.getVirtualHost())) {
192 request.setAttribute(
193 WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
194 }
195 }
196 catch (Exception e) {
197 _log.error(e, e);
198 }
199 }
200
201 if (companyId <= 0) {
202 companyId = GetterUtil.getLong(
203 CookieKeys.getCookie(request, CookieKeys.COMPANY_ID));
204
205 if (_log.isDebugEnabled()) {
206 _log.debug("Company id from cookie " + companyId);
207 }
208 }
209
210 if (companyId <= 0) {
211 companyId = _getDefaultCompanyId();
212
213 if (_log.isDebugEnabled()) {
214 _log.debug("Default company id " + companyId);
215 }
216 }
217
218 if (_log.isDebugEnabled()) {
219 _log.debug("Set company id " + companyId);
220 }
221
222 request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
223
224 CompanyThreadLocal.setCompanyId(companyId);
225
226 return companyId;
227 }
228
229 private long _getCompanyIdByVirtualHosts(String host) {
230 if (Validator.isNull(host)) {
231 return 0;
232 }
233
234 try {
235 Company company = CompanyLocalServiceUtil.getCompanyByVirtualHost(
236 host);
237
238 return company.getCompanyId();
239 }
240 catch (NoSuchCompanyException nsce) {
241 }
242 catch (Exception e) {
243 _log.error(e, e);
244 }
245
246 return 0;
247 }
248
249 private long[] _getCompanyIds() {
250 return _companyIds;
251 }
252
253 private long[] _getCompanyIdsBySQL() throws SQLException {
254 List<Long> companyIds = new ArrayList<Long>();
255
256 Connection con = null;
257 PreparedStatement ps = null;
258 ResultSet rs = null;
259
260 try {
261 con = DataAccess.getConnection();
262
263 ps = con.prepareStatement(_GET_COMPANY_IDS);
264
265 rs = ps.executeQuery();
266
267 while (rs.next()) {
268 long companyId = rs.getLong("companyId");
269
270 companyIds.add(companyId);
271 }
272 }
273 finally {
274 DataAccess.cleanUp(con, ps, rs);
275 }
276
277 return ArrayUtil.toArray(
278 companyIds.toArray(new Long[companyIds.size()]));
279 }
280
281 private long _getDefaultCompanyId() {
282 return _companyIds[0];
283 }
284
285 private LayoutSet _getLayoutSetByVirtualHosts(String host) {
286 if (Validator.isNull(host)) {
287 return null;
288 }
289
290 if (_isVirtualHostsIgnoreHost(host)) {
291 return null;
292 }
293
294 try {
295 return LayoutSetLocalServiceUtil.getLayoutSet(host);
296 }
297 catch (Exception e) {
298 return null;
299 }
300 }
301
302 private String[] _getWebIds() {
303 if (_webIds != null) {
304 return _webIds;
305 }
306
307 if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
308 throw new RuntimeException("Default web id must not be null");
309 }
310
311 try {
312 List<Company> companies = CompanyLocalServiceUtil.getCompanies(
313 false);
314
315 List<String> webIdsList = new ArrayList<String>(companies.size());
316
317 for (Company company : companies) {
318 webIdsList.add(company.getWebId());
319 }
320
321 _webIds = webIdsList.toArray(new String[webIdsList.size()]);
322 }
323 catch (Exception e) {
324 _log.error(e, e);
325 }
326
327 if ((_webIds == null) || (_webIds.length == 0)) {
328 _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID};
329 }
330
331 return _webIds;
332 }
333
334 private long _initCompany(ServletContext servletContext, String webId) {
335
336
338 if (_log.isDebugEnabled()) {
339 _log.debug("Begin initializing company with web id " + webId);
340 }
341
342 long companyId = 0;
343
344 try {
345 Company company = CompanyLocalServiceUtil.checkCompany(webId);
346
347 companyId = company.getCompanyId();
348 }
349 catch (Exception e) {
350 _log.error(e, e);
351 }
352
353 CompanyThreadLocal.setCompanyId(companyId);
354
355
357 if (_log.isDebugEnabled()) {
358 _log.debug("Initialize display");
359 }
360
361 try {
362 String xml = HttpUtil.URLtoString(servletContext.getResource(
363 "/WEB-INF/liferay-display.xml"));
364
365 PortletCategory portletCategory =
366 (PortletCategory)WebAppPool.get(
367 String.valueOf(companyId), WebKeys.PORTLET_CATEGORY);
368
369 if (portletCategory == null) {
370 portletCategory = new PortletCategory();
371 }
372
373 PortletCategory newPortletCategory =
374 PortletLocalServiceUtil.getEARDisplay(xml);
375
376 portletCategory.merge(newPortletCategory);
377
378 WebAppPool.put(
379 String.valueOf(companyId), WebKeys.PORTLET_CATEGORY,
380 portletCategory);
381 }
382 catch (Exception e) {
383 _log.error(e, e);
384 }
385
386
388 if (_log.isDebugEnabled()) {
389 _log.debug("Check journal content search");
390 }
391
392 if (GetterUtil.getBoolean(PropsUtil.get(
393 PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) {
394
395 try {
396 JournalContentSearchLocalServiceUtil.checkContentSearches(
397 companyId);
398 }
399 catch (Exception e) {
400 _log.error(e, e);
401 }
402 }
403
404
406 try {
407 if (PortalLDAPUtil.isImportOnStartup(companyId)) {
408 PortalLDAPUtil.importFromLDAP(companyId);
409 }
410 }
411 catch (Exception e) {
412 _log.error(e, e);
413 }
414
415
417 if (_log.isDebugEnabled()) {
418 _log.debug("Process application startup events");
419 }
420
421 try {
422 EventsProcessorUtil.process(
423 PropsKeys.APPLICATION_STARTUP_EVENTS,
424 PropsValues.APPLICATION_STARTUP_EVENTS,
425 new String[] {String.valueOf(companyId)});
426 }
427 catch (Exception e) {
428 _log.error(e, e);
429 }
430
431
433 if (_log.isDebugEnabled()) {
434 _log.debug(
435 "End initializing company with web id " + webId +
436 " and company id " + companyId);
437 }
438
439 addCompanyId(companyId);
440
441 return companyId;
442 }
443
444 private boolean _isAutoLoginIgnoreHost(String host) {
445 return _autoLoginIgnoreHosts.contains(host);
446 }
447
448 private boolean _isAutoLoginIgnorePath(String path) {
449 return _autoLoginIgnorePaths.contains(path);
450 }
451
452 private boolean _isVirtualHostsIgnoreHost(String host) {
453 return _virtualHostsIgnoreHosts.contains(host);
454 }
455
456 private boolean _isVirtualHostsIgnorePath(String path) {
457 return _virtualHostsIgnorePaths.contains(path);
458 }
459
460 private static final String _GET_COMPANY_IDS =
461 "select companyId from Company";
462
463 private static Log _log = LogFactoryUtil.getLog(PortalInstances.class);
464
465 private static PortalInstances _instance = new PortalInstances();
466
467 private long[] _companyIds;
468 private String[] _webIds;
469 private Set<String> _autoLoginIgnoreHosts;
470 private Set<String> _autoLoginIgnorePaths;
471 private Set<String> _virtualHostsIgnoreHosts;
472 private Set<String> _virtualHostsIgnorePaths;
473
474 }