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