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