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