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