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