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