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