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.SetUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.language.LanguageResources;
35 import com.liferay.portal.model.Company;
36 import com.liferay.portal.model.LayoutSet;
37 import com.liferay.portal.model.PortletCategory;
38 import com.liferay.portal.security.auth.CompanyThreadLocal;
39 import com.liferay.portal.security.ldap.PortalLDAPUtil;
40 import com.liferay.portal.service.CompanyLocalServiceUtil;
41 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
42 import com.liferay.portal.service.PortletLocalServiceUtil;
43 import com.liferay.portal.struts.MultiMessageResources;
44 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
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 import org.apache.struts.Globals;
54
55
64 public class PortalInstances {
65
66 public static final String DEFAULT_VIRTUAL_HOST = "localhost";
67
68 public static void addCompanyId(long companyId) {
69 _instance._addCompanyId(companyId);
70 }
71
72 public static long getCompanyId(HttpServletRequest request) {
73 return _instance._getCompanyId(request);
74 }
75
76 public static long[] getCompanyIds() {
77 return _instance._getCompanyIds();
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
181 if (companyId <= 0) {
182 companyId = GetterUtil.getLong(
183 CookieKeys.getCookie(request, CookieKeys.COMPANY_ID));
184
185 if (_log.isDebugEnabled()) {
186 _log.debug("Company id from cookie " + companyId);
187 }
188 }
189
190 if (companyId <= 0) {
191 companyId = _getDefaultCompanyId();
192
193 if (_log.isDebugEnabled()) {
194 _log.debug("Default company id " + companyId);
195 }
196 }
197
198 if (_log.isDebugEnabled()) {
199 _log.debug("Set company id " + companyId);
200 }
201
202 request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
203
204 CompanyThreadLocal.setCompanyId(companyId);
205
206 return companyId;
207 }
208
209 private long _getCompanyIdByVirtualHosts(String host) {
210 if (Validator.isNull(host)) {
211 return 0;
212 }
213
214 try {
215 Company company = CompanyLocalServiceUtil.getCompanyByVirtualHost(
216 host);
217
218 return company.getCompanyId();
219 }
220 catch (NoSuchCompanyException nsce) {
221 }
222 catch (Exception e) {
223 _log.error(e, e);
224 }
225
226 return 0;
227 }
228
229 private long[] _getCompanyIds() {
230 return _companyIds;
231 }
232
233 private long _getDefaultCompanyId() {
234 return _companyIds[0];
235 }
236
237 private LayoutSet _getLayoutSetByVirtualHosts(String host) {
238 if (Validator.isNull(host)) {
239 return null;
240 }
241
242 if (_isVirtualHostsIgnoreHost(host)) {
243 return null;
244 }
245
246 try {
247 return LayoutSetLocalServiceUtil.getLayoutSet(host);
248 }
249 catch (Exception e) {
250 return null;
251 }
252 }
253
254 private String[] _getWebIds() {
255 if (_webIds != null) {
256 return _webIds;
257 }
258
259 if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
260 throw new RuntimeException("Default web id must not be null");
261 }
262
263 try {
264 List<Company> companies = CompanyLocalServiceUtil.getCompanies(
265 false);
266
267 List<String> webIdsList = new ArrayList<String>(companies.size());
268
269 for (Company company : companies) {
270 webIdsList.add(company.getWebId());
271 }
272
273 _webIds = webIdsList.toArray(new String[webIdsList.size()]);
274 }
275 catch (Exception e) {
276 _log.error(e, e);
277 }
278
279 if ((_webIds == null) || (_webIds.length == 0)) {
280 _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID};
281 }
282
283 return _webIds;
284 }
285
286 private long _initCompany(ServletContext servletContext, String webId) {
287
288
290 if (_log.isDebugEnabled()) {
291 _log.debug("Begin initializing company with web id " + webId);
292 }
293
294 long companyId = 0;
295
296 try {
297 Company company = CompanyLocalServiceUtil.checkCompany(webId);
298
299 companyId = company.getCompanyId();
300 }
301 catch (Exception e) {
302 _log.error(e, e);
303 }
304
305 CompanyThreadLocal.setCompanyId(companyId);
306
307
309 if (_log.isDebugEnabled()) {
310 _log.debug("Initialize display");
311 }
312
313 try {
314 String xml = HttpUtil.URLtoString(servletContext.getResource(
315 "/WEB-INF/liferay-display.xml"));
316
317 PortletCategory portletCategory =
318 (PortletCategory)WebAppPool.get(
319 String.valueOf(companyId), WebKeys.PORTLET_CATEGORY);
320
321 if (portletCategory == null) {
322 portletCategory = new PortletCategory();
323 }
324
325 PortletCategory newPortletCategory =
326 PortletLocalServiceUtil.getEARDisplay(xml);
327
328 portletCategory.merge(newPortletCategory);
329
330 WebAppPool.put(
331 String.valueOf(companyId), WebKeys.PORTLET_CATEGORY,
332 portletCategory);
333 }
334 catch (Exception e) {
335 _log.error(e, e);
336 }
337
338
340 if (_log.isDebugEnabled()) {
341 _log.debug("Check journal content search");
342 }
343
344 if (GetterUtil.getBoolean(PropsUtil.get(
345 PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) {
346
347 try {
348 JournalContentSearchLocalServiceUtil.checkContentSearches(
349 companyId);
350 }
351 catch (Exception e) {
352 _log.error(e, e);
353 }
354 }
355
356
358 try {
359 if (PortalLDAPUtil.isImportOnStartup(companyId)) {
360 PortalLDAPUtil.importFromLDAP(companyId);
361 }
362 }
363 catch (Exception e) {
364 _log.error(e, e);
365 }
366
367
369 if (_log.isDebugEnabled()) {
370 _log.debug("Message resources");
371 }
372
373 MultiMessageResources messageResources =
374 (MultiMessageResources)servletContext.getAttribute(
375 Globals.MESSAGES_KEY);
376
377 messageResources.setServletContext(servletContext);
378
379 WebAppPool.put(
380 String.valueOf(companyId), Globals.MESSAGES_KEY, messageResources);
381
382 WebAppPool.put(
383 String.valueOf(companyId), WebKeys.LANGUAGE_RESOURCES,
384 new LanguageResources(messageResources));
385
386
388 if (_log.isDebugEnabled()) {
389 _log.debug("Process application startup events");
390 }
391
392 try {
393 EventsProcessorUtil.process(
394 PropsKeys.APPLICATION_STARTUP_EVENTS,
395 PropsValues.APPLICATION_STARTUP_EVENTS,
396 new String[] {String.valueOf(companyId)});
397 }
398 catch (Exception e) {
399 _log.error(e, e);
400 }
401
402
404 if (_log.isDebugEnabled()) {
405 _log.debug(
406 "End initializing company with web id " + webId +
407 " and company id " + companyId);
408 }
409
410 addCompanyId(companyId);
411
412 return companyId;
413 }
414
415 private boolean _isAutoLoginIgnoreHost(String host) {
416 return _autoLoginIgnoreHosts.contains(host);
417 }
418
419 private boolean _isAutoLoginIgnorePath(String path) {
420 return _autoLoginIgnorePaths.contains(path);
421 }
422
423 private boolean _isVirtualHostsIgnoreHost(String host) {
424 return _virtualHostsIgnoreHosts.contains(host);
425 }
426
427 private boolean _isVirtualHostsIgnorePath(String path) {
428 return _virtualHostsIgnorePaths.contains(path);
429 }
430
431 private static Log _log = LogFactoryUtil.getLog(PortalInstances.class);
432
433 private static PortalInstances _instance = new PortalInstances();
434
435 private long[] _companyIds;
436 private String[] _webIds;
437 private Set<String> _autoLoginIgnoreHosts;
438 private Set<String> _autoLoginIgnorePaths;
439 private Set<String> _virtualHostsIgnoreHosts;
440 private Set<String> _virtualHostsIgnorePaths;
441
442 }