1
22
23 package com.liferay.portal.util;
24
25 import com.liferay.portal.events.EventsProcessor;
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.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 import com.liferay.util.SetUtil;
41
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Set;
45
46 import javax.servlet.ServletContext;
47 import javax.servlet.http.HttpServletRequest;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51 import org.apache.struts.Globals;
52
53
62 public class PortalInstances {
63
64 public static final String DEFAULT_VIRTUAL_HOST = "localhost";
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 List<Company> companies = CompanyLocalServiceUtil.getCompanies();
214
215 for (Company company : companies) {
216 if (company.getVirtualHost().equals(host)) {
217 return company.getCompanyId();
218 }
219 }
220 }
221 catch (Exception e) {
222 _log.error(e, e);
223 }
224
225 return 0;
226 }
227
228 private long[] _getCompanyIds() {
229 return _companyIds;
230 }
231
232 private long _getDefaultCompanyId() {
233 return _companyIds[0];
234 }
235
236 private LayoutSet _getLayoutSetByVirtualHosts(String host) {
237 if (Validator.isNull(host)) {
238 return null;
239 }
240
241 if (_isVirtualHostsIgnoreHost(host)) {
242 return null;
243 }
244
245 try {
246 return LayoutSetLocalServiceUtil.getLayoutSet(host);
247 }
248 catch (Exception e) {
249 return null;
250 }
251 }
252
253 private String[] _getWebIds() {
254 if (_webIds != null) {
255 return _webIds;
256 }
257
258 if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
259 throw new RuntimeException("Default web id must not be null");
260 }
261
262 try {
263 List<Company> companies = CompanyLocalServiceUtil.getCompanies();
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
382 if (_log.isDebugEnabled()) {
383 _log.debug("Process application startup events");
384 }
385
386 try {
387 EventsProcessor.process(
388 PropsKeys.APPLICATION_STARTUP_EVENTS,
389 PropsValues.APPLICATION_STARTUP_EVENTS,
390 new String[] {String.valueOf(companyId)});
391 }
392 catch (Exception e) {
393 _log.error(e, e);
394 }
395
396
398 if (_log.isDebugEnabled()) {
399 _log.debug(
400 "End initializing company with web id " + webId +
401 " and company id " + companyId);
402 }
403
404 addCompanyId(companyId);
405
406 return companyId;
407 }
408
409 private boolean _isAutoLoginIgnoreHost(String host) {
410 return _autoLoginIgnoreHosts.contains(host);
411 }
412
413 private boolean _isAutoLoginIgnorePath(String path) {
414 return _autoLoginIgnorePaths.contains(path);
415 }
416
417 private boolean _isVirtualHostsIgnoreHost(String host) {
418 return _virtualHostsIgnoreHosts.contains(host);
419 }
420
421 private boolean _isVirtualHostsIgnorePath(String path) {
422 return _virtualHostsIgnorePaths.contains(path);
423 }
424
425 private static Log _log = LogFactory.getLog(PortalInstances.class);
426
427 private static PortalInstances _instance = new PortalInstances();
428
429 private long[] _companyIds;
430 private String[] _webIds;
431 private Set<String> _autoLoginIgnoreHosts;
432 private Set<String> _autoLoginIgnorePaths;
433 private Set<String> _virtualHostsIgnoreHosts;
434 private Set<String> _virtualHostsIgnorePaths;
435
436 }