1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.NoSuchCompanyException;
18  import com.liferay.portal.events.EventsProcessorUtil;
19  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.ArrayUtil;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.HttpUtil;
25  import com.liferay.portal.kernel.util.PropsKeys;
26  import com.liferay.portal.kernel.util.SetUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Company;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.model.LayoutSet;
31  import com.liferay.portal.model.PortletCategory;
32  import com.liferay.portal.security.auth.CompanyThreadLocal;
33  import com.liferay.portal.security.ldap.LDAPSettingsUtil;
34  import com.liferay.portal.security.ldap.PortalLDAPImporterUtil;
35  import com.liferay.portal.service.CompanyLocalServiceUtil;
36  import com.liferay.portal.service.GroupLocalServiceUtil;
37  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
38  import com.liferay.portal.service.PortletLocalServiceUtil;
39  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
40  
41  import java.sql.Connection;
42  import java.sql.PreparedStatement;
43  import java.sql.ResultSet;
44  import java.sql.SQLException;
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  /**
54   * <a href="PortalInstances.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   * @author Jose Oliver
58   * @author Atul Patel
59   * @author Mika Koivisto
60   */
61  public class PortalInstances {
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[] getCompanyIdsBySQL() throws SQLException {
76          return _instance._getCompanyIdsBySQL();
77      }
78  
79      public static long getDefaultCompanyId() {
80          return _instance._getDefaultCompanyId();
81      }
82  
83      public static String[] getWebIds() {
84          return _instance._getWebIds();
85      }
86  
87      public static long initCompany(
88          ServletContext servletContext, String webId) {
89  
90          return _instance._initCompany(servletContext, webId);
91      }
92  
93      public static boolean isAutoLoginIgnoreHost(String host) {
94          return _instance._isAutoLoginIgnoreHost(host);
95      }
96  
97      public static boolean isAutoLoginIgnorePath(String path) {
98          return _instance._isAutoLoginIgnorePath(path);
99      }
100 
101     public static boolean isVirtualHostsIgnoreHost(String host) {
102         return _instance._isVirtualHostsIgnoreHost(host);
103     }
104 
105     public static boolean isVirtualHostsIgnorePath(String path) {
106         return _instance._isVirtualHostsIgnorePath(path);
107     }
108 
109     private PortalInstances() {
110         _companyIds = new long[0];
111         _autoLoginIgnoreHosts = SetUtil.fromArray(PropsUtil.getArray(
112             PropsKeys.AUTO_LOGIN_IGNORE_HOSTS));
113         _autoLoginIgnorePaths = SetUtil.fromArray(PropsUtil.getArray(
114             PropsKeys.AUTO_LOGIN_IGNORE_PATHS));
115         _virtualHostsIgnoreHosts = SetUtil.fromArray(PropsUtil.getArray(
116             PropsKeys.VIRTUAL_HOSTS_IGNORE_HOSTS));
117         _virtualHostsIgnorePaths = SetUtil.fromArray(PropsUtil.getArray(
118             PropsKeys.VIRTUAL_HOSTS_IGNORE_PATHS));
119     }
120 
121     private void _addCompanyId(long companyId) {
122         if (ArrayUtil.contains(_companyIds, companyId)) {
123             return;
124         }
125 
126         long[] companyIds = new long[_companyIds.length + 1];
127 
128         System.arraycopy(
129             _companyIds, 0, companyIds, 0, _companyIds.length);
130 
131         companyIds[_companyIds.length] = companyId;
132 
133         _companyIds = companyIds;
134     }
135 
136     private long _getCompanyId(HttpServletRequest request) {
137         if (_log.isDebugEnabled()) {
138             _log.debug("Get company id");
139         }
140 
141         Long companyIdObj = (Long)request.getAttribute(WebKeys.COMPANY_ID);
142 
143         if (_log.isDebugEnabled()) {
144             _log.debug("Company id from request " + companyIdObj);
145         }
146 
147         if (companyIdObj != null) {
148             return companyIdObj.longValue();
149         }
150 
151         String host = PortalUtil.getHost(request);
152 
153         if (_log.isDebugEnabled()) {
154             _log.debug("Host " + host);
155         }
156 
157         long companyId = _getCompanyIdByVirtualHosts(host);
158 
159         if (_log.isDebugEnabled()) {
160             _log.debug("Company id from host " + companyId);
161         }
162 
163         if (companyId <= 0) {
164             LayoutSet layoutSet = _getLayoutSetByVirtualHosts(host);
165 
166             if (layoutSet != null) {
167                 companyId = layoutSet.getCompanyId();
168 
169                 if (_log.isDebugEnabled()) {
170                     _log.debug(
171                         "Company id " + companyId + " is associated with " +
172                             "layout set " + layoutSet.getLayoutSetId());
173                 }
174 
175                 request.setAttribute(
176                     WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
177             }
178         }
179         else if (Validator.isNotNull(
180                     PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME)) {
181 
182             try {
183                 Group group = GroupLocalServiceUtil.getGroup(
184                     companyId,
185                     PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME);
186 
187                 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
188                     group.getGroupId(), false);
189 
190                 if (Validator.isNull(layoutSet.getVirtualHost())) {
191                     request.setAttribute(
192                         WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
193                 }
194             }
195             catch (Exception e) {
196                 _log.error(e, e);
197             }
198         }
199 
200         if (companyId <= 0) {
201             companyId = GetterUtil.getLong(
202                 CookieKeys.getCookie(request, CookieKeys.COMPANY_ID));
203 
204             if (_log.isDebugEnabled()) {
205                 _log.debug("Company id from cookie " + companyId);
206             }
207         }
208 
209         if (companyId <= 0) {
210             companyId = _getDefaultCompanyId();
211 
212             if (_log.isDebugEnabled()) {
213                 _log.debug("Default company id " + companyId);
214             }
215         }
216 
217         if (_log.isDebugEnabled()) {
218             _log.debug("Set company id " + companyId);
219         }
220 
221         request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
222 
223         CompanyThreadLocal.setCompanyId(companyId);
224 
225         return companyId;
226     }
227 
228     private long _getCompanyIdByVirtualHosts(String host) {
229         if (Validator.isNull(host)) {
230             return 0;
231         }
232 
233         try {
234             Company company = CompanyLocalServiceUtil.getCompanyByVirtualHost(
235                 host);
236 
237             return company.getCompanyId();
238         }
239         catch (NoSuchCompanyException nsce) {
240         }
241         catch (Exception e) {
242             _log.error(e, e);
243         }
244 
245         return 0;
246     }
247 
248     private long[] _getCompanyIds() {
249         return _companyIds;
250     }
251 
252     private long[] _getCompanyIdsBySQL() throws SQLException {
253         List<Long> companyIds = new ArrayList<Long>();
254 
255         Connection con = null;
256         PreparedStatement ps = null;
257         ResultSet rs = null;
258 
259         try {
260             con = DataAccess.getConnection();
261 
262             ps = con.prepareStatement(_GET_COMPANY_IDS);
263 
264             rs = ps.executeQuery();
265 
266             while (rs.next()) {
267                 long companyId = rs.getLong("companyId");
268 
269                 companyIds.add(companyId);
270             }
271         }
272         finally {
273             DataAccess.cleanUp(con, ps, rs);
274         }
275 
276         return ArrayUtil.toArray(
277             companyIds.toArray(new Long[companyIds.size()]));
278     }
279 
280     private long _getDefaultCompanyId() {
281         return _companyIds[0];
282     }
283 
284     private LayoutSet _getLayoutSetByVirtualHosts(String host) {
285         if (Validator.isNull(host)) {
286             return null;
287         }
288 
289         if (_isVirtualHostsIgnoreHost(host)) {
290             return null;
291         }
292 
293         try {
294             return LayoutSetLocalServiceUtil.getLayoutSet(host);
295         }
296         catch (Exception e) {
297             return null;
298         }
299     }
300 
301     private String[] _getWebIds() {
302         if (_webIds != null) {
303             return _webIds;
304         }
305 
306         if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
307             throw new RuntimeException("Default web id must not be null");
308         }
309 
310         try {
311             List<Company> companies = CompanyLocalServiceUtil.getCompanies(
312                 false);
313 
314             List<String> webIdsList = new ArrayList<String>(companies.size());
315 
316             for (Company company : companies) {
317                 webIdsList.add(company.getWebId());
318             }
319 
320             _webIds = webIdsList.toArray(new String[webIdsList.size()]);
321         }
322         catch (Exception e) {
323             _log.error(e, e);
324         }
325 
326         if ((_webIds == null) || (_webIds.length == 0)) {
327             _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID};
328         }
329 
330         return _webIds;
331     }
332 
333     private long _initCompany(ServletContext servletContext, String webId) {
334 
335         // Begin initializing company
336 
337         if (_log.isDebugEnabled()) {
338             _log.debug("Begin initializing company with web id " + webId);
339         }
340 
341         long companyId = 0;
342 
343         try {
344             Company company = CompanyLocalServiceUtil.checkCompany(webId);
345 
346             companyId = company.getCompanyId();
347         }
348         catch (Exception e) {
349             _log.error(e, e);
350         }
351 
352         CompanyThreadLocal.setCompanyId(companyId);
353 
354         // Initialize display
355 
356         if (_log.isDebugEnabled()) {
357             _log.debug("Initialize display");
358         }
359 
360         try {
361             String xml = HttpUtil.URLtoString(servletContext.getResource(
362                 "/WEB-INF/liferay-display.xml"));
363 
364             PortletCategory portletCategory =
365                 (PortletCategory)WebAppPool.get(
366                     String.valueOf(companyId), WebKeys.PORTLET_CATEGORY);
367 
368             if (portletCategory == null) {
369                 portletCategory = new PortletCategory();
370             }
371 
372             PortletCategory newPortletCategory =
373                 PortletLocalServiceUtil.getEARDisplay(xml);
374 
375             portletCategory.merge(newPortletCategory);
376 
377             WebAppPool.put(
378                 String.valueOf(companyId), WebKeys.PORTLET_CATEGORY,
379                 portletCategory);
380         }
381         catch (Exception e) {
382             _log.error(e, e);
383         }
384 
385         // Check journal content search
386 
387         if (_log.isDebugEnabled()) {
388             _log.debug("Check journal content search");
389         }
390 
391         if (GetterUtil.getBoolean(PropsUtil.get(
392                 PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) {
393 
394             try {
395                 JournalContentSearchLocalServiceUtil.checkContentSearches(
396                     companyId);
397             }
398             catch (Exception e) {
399                 _log.error(e, e);
400             }
401         }
402 
403         // LDAP Import
404 
405         try {
406             if (LDAPSettingsUtil.isImportOnStartup(companyId)) {
407                 PortalLDAPImporterUtil.importFromLDAP(companyId);
408             }
409         }
410         catch (Exception e) {
411             _log.error(e, e);
412         }
413 
414         // Process application startup events
415 
416         if (_log.isDebugEnabled()) {
417             _log.debug("Process application startup events");
418         }
419 
420         try {
421             EventsProcessorUtil.process(
422                 PropsKeys.APPLICATION_STARTUP_EVENTS,
423                 PropsValues.APPLICATION_STARTUP_EVENTS,
424                 new String[] {String.valueOf(companyId)});
425         }
426         catch (Exception e) {
427             _log.error(e, e);
428         }
429 
430         // End initializing company
431 
432         if (_log.isDebugEnabled()) {
433             _log.debug(
434                 "End initializing company with web id " + webId +
435                     " and company id " + companyId);
436         }
437 
438         addCompanyId(companyId);
439 
440         return companyId;
441     }
442 
443     private boolean _isAutoLoginIgnoreHost(String host) {
444         return _autoLoginIgnoreHosts.contains(host);
445     }
446 
447     private boolean _isAutoLoginIgnorePath(String path) {
448         return _autoLoginIgnorePaths.contains(path);
449     }
450 
451     private boolean _isVirtualHostsIgnoreHost(String host) {
452         return _virtualHostsIgnoreHosts.contains(host);
453     }
454 
455     private boolean _isVirtualHostsIgnorePath(String path) {
456         return _virtualHostsIgnorePaths.contains(path);
457     }
458 
459     private static final String _GET_COMPANY_IDS =
460         "select companyId from Company";
461 
462     private static Log _log = LogFactoryUtil.getLog(PortalInstances.class);
463 
464     private static PortalInstances _instance = new PortalInstances();
465 
466     private long[] _companyIds;
467     private String[] _webIds;
468     private Set<String> _autoLoginIgnoreHosts;
469     private Set<String> _autoLoginIgnorePaths;
470     private Set<String> _virtualHostsIgnoreHosts;
471     private Set<String> _virtualHostsIgnorePaths;
472 
473 }