1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.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.SetUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.language.LanguageResources;
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.portal.struts.MultiMessageResources;
44  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
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  import org.apache.struts.Globals;
54  
55  /**
56   * <a href="PortalInstances.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Jose Oliver
60   * @author Atul Patel
61   * @author Mika Koivisto
62   */
63  public class PortalInstances {
64  
65      public static final String DEFAULT_VIRTUAL_HOST = "localhost";
66  
67      public static void addCompanyId(long companyId) {
68          _instance._addCompanyId(companyId);
69      }
70  
71      public static long getCompanyId(HttpServletRequest request) {
72          return _instance._getCompanyId(request);
73      }
74  
75      public static long[] getCompanyIds() {
76          return _instance._getCompanyIds();
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 
180         if (companyId <= 0) {
181             companyId = GetterUtil.getLong(
182                 CookieKeys.getCookie(request, CookieKeys.COMPANY_ID));
183 
184             if (_log.isDebugEnabled()) {
185                 _log.debug("Company id from cookie " + companyId);
186             }
187         }
188 
189         if (companyId <= 0) {
190             companyId = _getDefaultCompanyId();
191 
192             if (_log.isDebugEnabled()) {
193                 _log.debug("Default company id " + companyId);
194             }
195         }
196 
197         if (_log.isDebugEnabled()) {
198             _log.debug("Set company id " + companyId);
199         }
200 
201         request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
202 
203         CompanyThreadLocal.setCompanyId(companyId);
204 
205         return companyId;
206     }
207 
208     private long _getCompanyIdByVirtualHosts(String host) {
209         if (Validator.isNull(host)) {
210             return 0;
211         }
212 
213         try {
214             Company company = CompanyLocalServiceUtil.getCompanyByVirtualHost(
215                 host);
216 
217             return company.getCompanyId();
218         }
219         catch (NoSuchCompanyException nsce) {
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                 false);
265 
266             List<String> webIdsList = new ArrayList<String>(companies.size());
267 
268             for (Company company : companies) {
269                 webIdsList.add(company.getWebId());
270             }
271 
272             _webIds = webIdsList.toArray(new String[webIdsList.size()]);
273         }
274         catch (Exception e) {
275             _log.error(e, e);
276         }
277 
278         if ((_webIds == null) || (_webIds.length == 0)) {
279             _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID};
280         }
281 
282         return _webIds;
283     }
284 
285     private long _initCompany(ServletContext servletContext, String webId) {
286 
287         // Begin initializing company
288 
289         if (_log.isDebugEnabled()) {
290             _log.debug("Begin initializing company with web id " + webId);
291         }
292 
293         long companyId = 0;
294 
295         try {
296             Company company = CompanyLocalServiceUtil.checkCompany(webId);
297 
298             companyId = company.getCompanyId();
299         }
300         catch (Exception e) {
301             _log.error(e, e);
302         }
303 
304         CompanyThreadLocal.setCompanyId(companyId);
305 
306         // Initialize display
307 
308         if (_log.isDebugEnabled()) {
309             _log.debug("Initialize display");
310         }
311 
312         try {
313             String xml = HttpUtil.URLtoString(servletContext.getResource(
314                 "/WEB-INF/liferay-display.xml"));
315 
316             PortletCategory portletCategory =
317                 (PortletCategory)WebAppPool.get(
318                     String.valueOf(companyId), WebKeys.PORTLET_CATEGORY);
319 
320             if (portletCategory == null) {
321                 portletCategory = new PortletCategory();
322             }
323 
324             PortletCategory newPortletCategory =
325                 PortletLocalServiceUtil.getEARDisplay(xml);
326 
327             portletCategory.merge(newPortletCategory);
328 
329             WebAppPool.put(
330                 String.valueOf(companyId), WebKeys.PORTLET_CATEGORY,
331                 portletCategory);
332         }
333         catch (Exception e) {
334             _log.error(e, e);
335         }
336 
337         // Check journal content search
338 
339         if (_log.isDebugEnabled()) {
340             _log.debug("Check journal content search");
341         }
342 
343         if (GetterUtil.getBoolean(PropsUtil.get(
344                 PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) {
345 
346             try {
347                 JournalContentSearchLocalServiceUtil.checkContentSearches(
348                     companyId);
349             }
350             catch (Exception e) {
351                 _log.error(e, e);
352             }
353         }
354 
355         // LDAP Import
356 
357         try {
358             if (PortalLDAPUtil.isImportOnStartup(companyId)) {
359                 PortalLDAPUtil.importFromLDAP(companyId);
360             }
361         }
362         catch (Exception e) {
363             _log.error(e, e);
364         }
365 
366         // Message resources
367 
368         if (_log.isDebugEnabled()) {
369             _log.debug("Message resources");
370         }
371 
372         MultiMessageResources messageResources =
373             (MultiMessageResources)servletContext.getAttribute(
374                 Globals.MESSAGES_KEY);
375 
376         messageResources.setServletContext(servletContext);
377 
378         WebAppPool.put(
379             String.valueOf(companyId), Globals.MESSAGES_KEY, messageResources);
380 
381         WebAppPool.put(
382             String.valueOf(companyId), WebKeys.LANGUAGE_RESOURCES,
383             new LanguageResources(messageResources));
384 
385         // Process application startup events
386 
387         if (_log.isDebugEnabled()) {
388             _log.debug("Process application startup events");
389         }
390 
391         try {
392             EventsProcessorUtil.process(
393                 PropsKeys.APPLICATION_STARTUP_EVENTS,
394                 PropsValues.APPLICATION_STARTUP_EVENTS,
395                 new String[] {String.valueOf(companyId)});
396         }
397         catch (Exception e) {
398             _log.error(e, e);
399         }
400 
401         // End initializing company
402 
403         if (_log.isDebugEnabled()) {
404             _log.debug(
405                 "End initializing company with web id " + webId +
406                     " and company id " + companyId);
407         }
408 
409         addCompanyId(companyId);
410 
411         return companyId;
412     }
413 
414     private boolean _isAutoLoginIgnoreHost(String host) {
415         return _autoLoginIgnoreHosts.contains(host);
416     }
417 
418     private boolean _isAutoLoginIgnorePath(String path) {
419         return _autoLoginIgnorePaths.contains(path);
420     }
421 
422     private boolean _isVirtualHostsIgnoreHost(String host) {
423         return _virtualHostsIgnoreHosts.contains(host);
424     }
425 
426     private boolean _isVirtualHostsIgnorePath(String path) {
427         return _virtualHostsIgnorePaths.contains(path);
428     }
429 
430     private static Log _log = LogFactoryUtil.getLog(PortalInstances.class);
431 
432     private static PortalInstances _instance = new PortalInstances();
433 
434     private long[] _companyIds;
435     private String[] _webIds;
436     private Set<String> _autoLoginIgnoreHosts;
437     private Set<String> _autoLoginIgnorePaths;
438     private Set<String> _virtualHostsIgnoreHosts;
439     private Set<String> _virtualHostsIgnorePaths;
440 
441 }