1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.events.EventsProcessor;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ArrayUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.HttpUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Company;
33  import com.liferay.portal.model.LayoutSet;
34  import com.liferay.portal.model.PortletCategory;
35  import com.liferay.portal.security.auth.CompanyThreadLocal;
36  import com.liferay.portal.security.ldap.PortalLDAPUtil;
37  import com.liferay.portal.service.CompanyLocalServiceUtil;
38  import com.liferay.portal.service.LayoutSetLocalServiceUtil;
39  import com.liferay.portal.service.PortletLocalServiceUtil;
40  import com.liferay.portal.struts.MultiMessageResources;
41  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
42  import com.liferay.util.SetUtil;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  import java.util.Set;
47  
48  import javax.servlet.ServletContext;
49  import javax.servlet.http.HttpServletRequest;
50  
51  import org.apache.struts.Globals;
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   */
62  public class PortalInstances {
63  
64      public static void addCompanyId(long companyId) {
65          _instance._addCompanyId(companyId);
66      }
67  
68      public static long getCompanyId(HttpServletRequest request) {
69          return _instance._getCompanyId(request);
70      }
71  
72      public static long[] getCompanyIds() {
73          return _instance._getCompanyIds();
74      }
75  
76      public static long getDefaultCompanyId() {
77          return _instance._getDefaultCompanyId();
78      }
79  
80      public static String[] getWebIds() {
81          return _instance._getWebIds();
82      }
83  
84      public static long initCompany(
85          ServletContext servletContext, String webId) {
86  
87          return _instance._initCompany(servletContext, webId);
88      }
89  
90      public static boolean isAutoLoginIgnoreHost(String host) {
91          return _instance._isAutoLoginIgnoreHost(host);
92      }
93  
94      public static boolean isAutoLoginIgnorePath(String path) {
95          return _instance._isAutoLoginIgnorePath(path);
96      }
97  
98      public static boolean isVirtualHostsIgnoreHost(String host) {
99          return _instance._isVirtualHostsIgnoreHost(host);
100     }
101 
102     public static boolean isVirtualHostsIgnorePath(String path) {
103         return _instance._isVirtualHostsIgnorePath(path);
104     }
105 
106     private PortalInstances() {
107         _companyIds = new long[0];
108         _autoLoginIgnoreHosts = SetUtil.fromArray(PropsUtil.getArray(
109             PropsKeys.AUTO_LOGIN_IGNORE_HOSTS));
110         _autoLoginIgnorePaths = SetUtil.fromArray(PropsUtil.getArray(
111             PropsKeys.AUTO_LOGIN_IGNORE_PATHS));
112         _virtualHostsIgnoreHosts = SetUtil.fromArray(PropsUtil.getArray(
113             PropsKeys.VIRTUAL_HOSTS_IGNORE_HOSTS));
114         _virtualHostsIgnorePaths = SetUtil.fromArray(PropsUtil.getArray(
115             PropsKeys.VIRTUAL_HOSTS_IGNORE_PATHS));
116     }
117 
118     private void _addCompanyId(long companyId) {
119         if (ArrayUtil.contains(_companyIds, companyId)) {
120             return;
121         }
122 
123         long[] companyIds = new long[_companyIds.length + 1];
124 
125         System.arraycopy(
126             _companyIds, 0, companyIds, 0, _companyIds.length);
127 
128         companyIds[_companyIds.length] = companyId;
129 
130         _companyIds = companyIds;
131     }
132 
133     private long _getCompanyId(HttpServletRequest request) {
134         if (_log.isDebugEnabled()) {
135             _log.debug("Get company id");
136         }
137 
138         Long companyIdObj = (Long)request.getAttribute(WebKeys.COMPANY_ID);
139 
140         if (_log.isDebugEnabled()) {
141             _log.debug("Company id from request " + companyIdObj);
142         }
143 
144         if (companyIdObj != null) {
145             return companyIdObj.longValue();
146         }
147 
148         String host = PortalUtil.getHost(request);
149 
150         if (_log.isDebugEnabled()) {
151             _log.debug("Host " + host);
152         }
153 
154         long companyId = _getCompanyIdByVirtualHosts(host);
155 
156         if (_log.isDebugEnabled()) {
157             _log.debug("Company id from host " + companyId);
158         }
159 
160         if (companyId <= 0) {
161             LayoutSet layoutSet = _getLayoutSetByVirtualHosts(host);
162 
163             if (layoutSet != null) {
164                 companyId = layoutSet.getCompanyId();
165 
166                 if (_log.isDebugEnabled()) {
167                     _log.debug(
168                         "Company id " + companyId + " is associated with " +
169                             "layout set " + layoutSet.getLayoutSetId());
170                 }
171 
172                 request.setAttribute(
173                     WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
174             }
175         }
176 
177         if (companyId <= 0) {
178             companyId = GetterUtil.getLong(
179                 CookieKeys.getCookie(request, CookieKeys.COMPANY_ID));
180 
181             if (_log.isDebugEnabled()) {
182                 _log.debug("Company id from cookie " + companyId);
183             }
184         }
185 
186         if (companyId <= 0) {
187             companyId = _getDefaultCompanyId();
188 
189             if (_log.isDebugEnabled()) {
190                 _log.debug("Default company id " + companyId);
191             }
192         }
193 
194         if (_log.isDebugEnabled()) {
195             _log.debug("Set company id " + companyId);
196         }
197 
198         request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
199 
200         CompanyThreadLocal.setCompanyId(companyId);
201 
202         return companyId;
203     }
204 
205     private long _getCompanyIdByVirtualHosts(String host) {
206         if (Validator.isNull(host)) {
207             return 0;
208         }
209 
210         try {
211             List<Company> companies = CompanyLocalServiceUtil.getCompanies();
212 
213             for (Company company : companies) {
214                 if (company.getVirtualHost().equals(host)) {
215                     return company.getCompanyId();
216                 }
217             }
218         }
219         catch (Exception e) {
220             _log.error(e, e);
221         }
222 
223         return 0;
224     }
225 
226     private long[] _getCompanyIds() {
227         return _companyIds;
228     }
229 
230     private long _getDefaultCompanyId() {
231         return _companyIds[0];
232     }
233 
234     private LayoutSet _getLayoutSetByVirtualHosts(String host) {
235         if (Validator.isNull(host)) {
236             return null;
237         }
238 
239         if (_isVirtualHostsIgnoreHost(host)) {
240             return null;
241         }
242 
243         try {
244             return LayoutSetLocalServiceUtil.getLayoutSet(host);
245         }
246         catch (Exception e) {
247             return null;
248         }
249     }
250 
251     private String[] _getWebIds() {
252         if (_webIds != null) {
253             return _webIds;
254         }
255 
256         if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
257             throw new RuntimeException("Default web id must not be null");
258         }
259 
260         try {
261             List<Company> companies = CompanyLocalServiceUtil.getCompanies();
262 
263             List<String> webIdsList = new ArrayList<String>(companies.size());
264 
265             for (Company company : companies) {
266                 webIdsList.add(company.getWebId());
267             }
268 
269             _webIds = webIdsList.toArray(new String[webIdsList.size()]);
270         }
271         catch (Exception e) {
272             _log.error(e, e);
273         }
274 
275         if ((_webIds == null) || (_webIds.length == 0)) {
276             _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID};
277         }
278 
279         return _webIds;
280     }
281 
282     private long _initCompany(ServletContext servletContext, String webId) {
283 
284         // Begin initializing company
285 
286         if (_log.isDebugEnabled()) {
287             _log.debug("Begin initializing company with web id " + webId);
288         }
289 
290         long companyId = 0;
291 
292         try {
293             Company company = CompanyLocalServiceUtil.checkCompany(webId);
294 
295             companyId = company.getCompanyId();
296         }
297         catch (Exception e) {
298             _log.error(e, e);
299         }
300 
301         CompanyThreadLocal.setCompanyId(companyId);
302 
303         // Initialize display
304 
305         if (_log.isDebugEnabled()) {
306             _log.debug("Initialize display");
307         }
308 
309         try {
310             String xml = HttpUtil.URLtoString(servletContext.getResource(
311                 "/WEB-INF/liferay-display.xml"));
312 
313             PortletCategory portletCategory =
314                 (PortletCategory)WebAppPool.get(
315                     String.valueOf(companyId), WebKeys.PORTLET_CATEGORY);
316 
317             if (portletCategory == null) {
318                 portletCategory = new PortletCategory();
319             }
320 
321             PortletCategory newPortletCategory =
322                 PortletLocalServiceUtil.getEARDisplay(xml);
323 
324             portletCategory.merge(newPortletCategory);
325 
326             WebAppPool.put(
327                 String.valueOf(companyId), WebKeys.PORTLET_CATEGORY,
328                 portletCategory);
329         }
330         catch (Exception e) {
331             _log.error(e, e);
332         }
333 
334         // Check journal content search
335 
336         if (_log.isDebugEnabled()) {
337             _log.debug("Check journal content search");
338         }
339 
340         if (GetterUtil.getBoolean(PropsUtil.get(
341                 PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) {
342 
343             try {
344                 JournalContentSearchLocalServiceUtil.checkContentSearches(
345                     companyId);
346             }
347             catch (Exception e) {
348                 _log.error(e, e);
349             }
350         }
351 
352         // LDAP Import
353 
354         try {
355             if (PortalLDAPUtil.isImportOnStartup(companyId)) {
356                 PortalLDAPUtil.importFromLDAP(companyId);
357             }
358         }
359         catch (Exception e) {
360             _log.error(e, e);
361         }
362 
363         // Message resources
364 
365         if (_log.isDebugEnabled()) {
366             _log.debug("Message resources");
367         }
368 
369         MultiMessageResources messageResources =
370             (MultiMessageResources)servletContext.getAttribute(
371                 Globals.MESSAGES_KEY);
372 
373         messageResources.setServletContext(servletContext);
374 
375         WebAppPool.put(
376             String.valueOf(companyId), Globals.MESSAGES_KEY, messageResources);
377 
378         // Process application startup events
379 
380         if (_log.isDebugEnabled()) {
381             _log.debug("Process application startup events");
382         }
383 
384         try {
385             EventsProcessor.process(
386                 PropsKeys.APPLICATION_STARTUP_EVENTS,
387                 PropsValues.APPLICATION_STARTUP_EVENTS,
388                 new String[] {String.valueOf(companyId)});
389         }
390         catch (Exception e) {
391             _log.error(e, e);
392         }
393 
394         // End initializing company
395 
396         if (_log.isDebugEnabled()) {
397             _log.debug(
398                 "End initializing company with web id " + webId +
399                     " and company id " + companyId);
400         }
401 
402         addCompanyId(companyId);
403 
404         return companyId;
405     }
406 
407     private boolean _isAutoLoginIgnoreHost(String host) {
408         return _autoLoginIgnoreHosts.contains(host);
409     }
410 
411     private boolean _isAutoLoginIgnorePath(String path) {
412         return _autoLoginIgnorePaths.contains(path);
413     }
414 
415     private boolean _isVirtualHostsIgnoreHost(String host) {
416         return _virtualHostsIgnoreHosts.contains(host);
417     }
418 
419     private boolean _isVirtualHostsIgnorePath(String path) {
420         return _virtualHostsIgnorePaths.contains(path);
421     }
422 
423     private static Log _log = LogFactoryUtil.getLog(PortalInstances.class);
424 
425     private static PortalInstances _instance = new PortalInstances();
426 
427     private long[] _companyIds;
428     private String[] _webIds;
429     private Set<String> _autoLoginIgnoreHosts;
430     private Set<String> _autoLoginIgnorePaths;
431     private Set<String> _virtualHostsIgnoreHosts;
432     private Set<String> _virtualHostsIgnorePaths;
433 
434 }