001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.NoSuchCompanyException;
018    import com.liferay.portal.events.EventsProcessorUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.HttpUtil;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.SetUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Company;
029    import com.liferay.portal.model.Group;
030    import com.liferay.portal.model.LayoutSet;
031    import com.liferay.portal.model.PortletCategory;
032    import com.liferay.portal.security.auth.CompanyThreadLocal;
033    import com.liferay.portal.security.ldap.LDAPSettingsUtil;
034    import com.liferay.portal.security.ldap.PortalLDAPImporterUtil;
035    import com.liferay.portal.service.CompanyLocalServiceUtil;
036    import com.liferay.portal.service.GroupLocalServiceUtil;
037    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
038    import com.liferay.portal.service.PortletLocalServiceUtil;
039    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
040    
041    import java.sql.Connection;
042    import java.sql.PreparedStatement;
043    import java.sql.ResultSet;
044    import java.sql.SQLException;
045    
046    import java.util.ArrayList;
047    import java.util.List;
048    import java.util.Set;
049    
050    import javax.servlet.ServletContext;
051    import javax.servlet.http.HttpServletRequest;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     * @author Jose Oliver
056     * @author Atul Patel
057     * @author Mika Koivisto
058     */
059    public class PortalInstances {
060    
061            public static void addCompanyId(long companyId) {
062                    _instance._addCompanyId(companyId);
063            }
064    
065            public static long getCompanyId(HttpServletRequest request) {
066                    return _instance._getCompanyId(request);
067            }
068    
069            public static long[] getCompanyIds() {
070                    return _instance._getCompanyIds();
071            }
072    
073            public static long[] getCompanyIdsBySQL() throws SQLException {
074                    return _instance._getCompanyIdsBySQL();
075            }
076    
077            public static long getDefaultCompanyId() {
078                    return _instance._getDefaultCompanyId();
079            }
080    
081            public static String[] getWebIds() {
082                    return _instance._getWebIds();
083            }
084    
085            public static long initCompany(
086                    ServletContext servletContext, String webId) {
087    
088                    return _instance._initCompany(servletContext, webId);
089            }
090    
091            public static boolean isAutoLoginIgnoreHost(String host) {
092                    return _instance._isAutoLoginIgnoreHost(host);
093            }
094    
095            public static boolean isAutoLoginIgnorePath(String path) {
096                    return _instance._isAutoLoginIgnorePath(path);
097            }
098    
099            public static boolean isVirtualHostsIgnoreHost(String host) {
100                    return _instance._isVirtualHostsIgnoreHost(host);
101            }
102    
103            public static boolean isVirtualHostsIgnorePath(String path) {
104                    return _instance._isVirtualHostsIgnorePath(path);
105            }
106    
107            private PortalInstances() {
108                    _companyIds = new long[0];
109                    _autoLoginIgnoreHosts = SetUtil.fromArray(PropsUtil.getArray(
110                            PropsKeys.AUTO_LOGIN_IGNORE_HOSTS));
111                    _autoLoginIgnorePaths = SetUtil.fromArray(PropsUtil.getArray(
112                            PropsKeys.AUTO_LOGIN_IGNORE_PATHS));
113                    _virtualHostsIgnoreHosts = SetUtil.fromArray(PropsUtil.getArray(
114                            PropsKeys.VIRTUAL_HOSTS_IGNORE_HOSTS));
115                    _virtualHostsIgnorePaths = SetUtil.fromArray(PropsUtil.getArray(
116                            PropsKeys.VIRTUAL_HOSTS_IGNORE_PATHS));
117            }
118    
119            private void _addCompanyId(long companyId) {
120                    if (ArrayUtil.contains(_companyIds, companyId)) {
121                            return;
122                    }
123    
124                    long[] companyIds = new long[_companyIds.length + 1];
125    
126                    System.arraycopy(
127                            _companyIds, 0, companyIds, 0, _companyIds.length);
128    
129                    companyIds[_companyIds.length] = companyId;
130    
131                    _companyIds = companyIds;
132            }
133    
134            private long _getCompanyId(HttpServletRequest request) {
135                    if (_log.isDebugEnabled()) {
136                            _log.debug("Get company id");
137                    }
138    
139                    Long companyIdObj = (Long)request.getAttribute(WebKeys.COMPANY_ID);
140    
141                    if (_log.isDebugEnabled()) {
142                            _log.debug("Company id from request " + companyIdObj);
143                    }
144    
145                    if (companyIdObj != null) {
146                            return companyIdObj.longValue();
147                    }
148    
149                    String host = PortalUtil.getHost(request);
150    
151                    if (_log.isDebugEnabled()) {
152                            _log.debug("Host " + host);
153                    }
154    
155                    long companyId = _getCompanyIdByVirtualHosts(host);
156    
157                    if (_log.isDebugEnabled()) {
158                            _log.debug("Company id from host " + companyId);
159                    }
160    
161                    if (companyId <= 0) {
162                            LayoutSet layoutSet = _getLayoutSetByVirtualHosts(host);
163    
164                            if (layoutSet != null) {
165                                    companyId = layoutSet.getCompanyId();
166    
167                                    if (_log.isDebugEnabled()) {
168                                            _log.debug(
169                                                    "Company id " + companyId + " is associated with " +
170                                                            "layout set " + layoutSet.getLayoutSetId());
171                                    }
172    
173                                    request.setAttribute(
174                                            WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
175                            }
176                    }
177                    else if (Validator.isNotNull(
178                                            PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME)) {
179    
180                            try {
181                                    Group group = GroupLocalServiceUtil.getGroup(
182                                            companyId,
183                                            PropsValues.VIRTUAL_HOSTS_DEFAULT_COMMUNITY_NAME);
184    
185                                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
186                                            group.getGroupId(), false);
187    
188                                    if (Validator.isNull(layoutSet.getVirtualHost())) {
189                                            request.setAttribute(
190                                                    WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet);
191                                    }
192                            }
193                            catch (Exception e) {
194                                    _log.error(e, e);
195                            }
196                    }
197    
198                    if (companyId <= 0) {
199                            companyId = GetterUtil.getLong(
200                                    CookieKeys.getCookie(request, CookieKeys.COMPANY_ID));
201    
202                            if (_log.isDebugEnabled()) {
203                                    _log.debug("Company id from cookie " + companyId);
204                            }
205                    }
206    
207                    if (companyId <= 0) {
208                            companyId = _getDefaultCompanyId();
209    
210                            if (_log.isDebugEnabled()) {
211                                    _log.debug("Default company id " + companyId);
212                            }
213                    }
214    
215                    if (_log.isDebugEnabled()) {
216                            _log.debug("Set company id " + companyId);
217                    }
218    
219                    request.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
220    
221                    CompanyThreadLocal.setCompanyId(companyId);
222    
223                    return companyId;
224            }
225    
226            private long _getCompanyIdByVirtualHosts(String host) {
227                    if (Validator.isNull(host)) {
228                            return 0;
229                    }
230    
231                    try {
232                            Company company = CompanyLocalServiceUtil.getCompanyByVirtualHost(
233                                    host);
234    
235                            return company.getCompanyId();
236                    }
237                    catch (NoSuchCompanyException nsce) {
238                    }
239                    catch (Exception e) {
240                            _log.error(e, e);
241                    }
242    
243                    return 0;
244            }
245    
246            private long[] _getCompanyIds() {
247                    return _companyIds;
248            }
249    
250            private long[] _getCompanyIdsBySQL() throws SQLException {
251                    List<Long> companyIds = new ArrayList<Long>();
252    
253                    Connection con = null;
254                    PreparedStatement ps = null;
255                    ResultSet rs = null;
256    
257                    try {
258                            con = DataAccess.getConnection();
259    
260                            ps = con.prepareStatement(_GET_COMPANY_IDS);
261    
262                            rs = ps.executeQuery();
263    
264                            while (rs.next()) {
265                                    long companyId = rs.getLong("companyId");
266    
267                                    companyIds.add(companyId);
268                            }
269                    }
270                    finally {
271                            DataAccess.cleanUp(con, ps, rs);
272                    }
273    
274                    return ArrayUtil.toArray(
275                            companyIds.toArray(new Long[companyIds.size()]));
276            }
277    
278            private long _getDefaultCompanyId() {
279                    return _companyIds[0];
280            }
281    
282            private LayoutSet _getLayoutSetByVirtualHosts(String host) {
283                    if (Validator.isNull(host)) {
284                            return null;
285                    }
286    
287                    if (_isVirtualHostsIgnoreHost(host)) {
288                            return null;
289                    }
290    
291                    try {
292                            return LayoutSetLocalServiceUtil.getLayoutSet(host);
293                    }
294                    catch (Exception e) {
295                            return null;
296                    }
297            }
298    
299            private String[] _getWebIds() {
300                    if (_webIds != null) {
301                            return _webIds;
302                    }
303    
304                    if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
305                            throw new RuntimeException("Default web id must not be null");
306                    }
307    
308                    try {
309                            List<Company> companies = CompanyLocalServiceUtil.getCompanies(
310                                    false);
311    
312                            List<String> webIdsList = new ArrayList<String>(companies.size());
313    
314                            for (Company company : companies) {
315                                    String webId = company.getWebId();
316    
317                                    if (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
318                                            webIdsList.add(0, webId);
319                                    }
320                                    else {
321                                            webIdsList.add(webId);
322                                    }
323                            }
324    
325                            _webIds = webIdsList.toArray(new String[webIdsList.size()]);
326                    }
327                    catch (Exception e) {
328                            _log.error(e, e);
329                    }
330    
331                    if ((_webIds == null) || (_webIds.length == 0)) {
332                            _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID};
333                    }
334    
335                    return _webIds;
336            }
337    
338            private long _initCompany(ServletContext servletContext, String webId) {
339    
340                    // Begin initializing company
341    
342                    if (_log.isDebugEnabled()) {
343                            _log.debug("Begin initializing company with web id " + webId);
344                    }
345    
346                    long companyId = 0;
347    
348                    try {
349                            Company company = CompanyLocalServiceUtil.checkCompany(webId);
350    
351                            companyId = company.getCompanyId();
352                    }
353                    catch (Exception e) {
354                            _log.error(e, e);
355                    }
356    
357                    CompanyThreadLocal.setCompanyId(companyId);
358    
359                    // Initialize display
360    
361                    if (_log.isDebugEnabled()) {
362                            _log.debug("Initialize display");
363                    }
364    
365                    try {
366                            String xml = HttpUtil.URLtoString(servletContext.getResource(
367                                    "/WEB-INF/liferay-display.xml"));
368    
369                            PortletCategory portletCategory =
370                                    (PortletCategory)WebAppPool.get(
371                                            String.valueOf(companyId), WebKeys.PORTLET_CATEGORY);
372    
373                            if (portletCategory == null) {
374                                    portletCategory = new PortletCategory();
375                            }
376    
377                            PortletCategory newPortletCategory =
378                                    PortletLocalServiceUtil.getEARDisplay(xml);
379    
380                            portletCategory.merge(newPortletCategory);
381    
382                            WebAppPool.put(
383                                    String.valueOf(companyId), WebKeys.PORTLET_CATEGORY,
384                                    portletCategory);
385                    }
386                    catch (Exception e) {
387                            _log.error(e, e);
388                    }
389    
390                    // Check journal content search
391    
392                    if (_log.isDebugEnabled()) {
393                            _log.debug("Check journal content search");
394                    }
395    
396                    if (GetterUtil.getBoolean(PropsUtil.get(
397                                    PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) {
398    
399                            try {
400                                    JournalContentSearchLocalServiceUtil.checkContentSearches(
401                                            companyId);
402                            }
403                            catch (Exception e) {
404                                    _log.error(e, e);
405                            }
406                    }
407    
408                    // LDAP Import
409    
410                    try {
411                            if (LDAPSettingsUtil.isImportOnStartup(companyId)) {
412                                    PortalLDAPImporterUtil.importFromLDAP(companyId);
413                            }
414                    }
415                    catch (Exception e) {
416                            _log.error(e, e);
417                    }
418    
419                    // Process application startup events
420    
421                    if (_log.isDebugEnabled()) {
422                            _log.debug("Process application startup events");
423                    }
424    
425                    try {
426                            EventsProcessorUtil.process(
427                                    PropsKeys.APPLICATION_STARTUP_EVENTS,
428                                    PropsValues.APPLICATION_STARTUP_EVENTS,
429                                    new String[] {String.valueOf(companyId)});
430                    }
431                    catch (Exception e) {
432                            _log.error(e, e);
433                    }
434    
435                    // End initializing company
436    
437                    if (_log.isDebugEnabled()) {
438                            _log.debug(
439                                    "End initializing company with web id " + webId +
440                                            " and company id " + companyId);
441                    }
442    
443                    addCompanyId(companyId);
444    
445                    return companyId;
446            }
447    
448            private boolean _isAutoLoginIgnoreHost(String host) {
449                    return _autoLoginIgnoreHosts.contains(host);
450            }
451    
452            private boolean _isAutoLoginIgnorePath(String path) {
453                    return _autoLoginIgnorePaths.contains(path);
454            }
455    
456            private boolean _isVirtualHostsIgnoreHost(String host) {
457                    return _virtualHostsIgnoreHosts.contains(host);
458            }
459    
460            private boolean _isVirtualHostsIgnorePath(String path) {
461                    return _virtualHostsIgnorePaths.contains(path);
462            }
463    
464            private static final String _GET_COMPANY_IDS =
465                    "select companyId from Company";
466    
467            private static Log _log = LogFactoryUtil.getLog(PortalInstances.class);
468    
469            private static PortalInstances _instance = new PortalInstances();
470    
471            private long[] _companyIds;
472            private String[] _webIds;
473            private Set<String> _autoLoginIgnoreHosts;
474            private Set<String> _autoLoginIgnorePaths;
475            private Set<String> _virtualHostsIgnoreHosts;
476            private Set<String> _virtualHostsIgnorePaths;
477    
478    }