001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.util;
022:
023: import com.liferay.portal.events.EventsProcessor;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.model.Company;
027: import com.liferay.portal.model.LayoutSet;
028: import com.liferay.portal.model.PortletCategory;
029: import com.liferay.portal.model.impl.CompanyImpl;
030: import com.liferay.portal.security.auth.CompanyThreadLocal;
031: import com.liferay.portal.security.ldap.PortalLDAPUtil;
032: import com.liferay.portal.service.CompanyLocalServiceUtil;
033: import com.liferay.portal.service.LayoutSetLocalServiceUtil;
034: import com.liferay.portal.service.PortletLocalServiceUtil;
035: import com.liferay.portal.struts.MultiMessageResources;
036: import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
037: import com.liferay.util.Http;
038: import com.liferay.util.SetUtil;
039:
040: import java.util.ArrayList;
041: import java.util.Iterator;
042: import java.util.List;
043: import java.util.Set;
044:
045: import javax.servlet.ServletContext;
046: import javax.servlet.http.HttpServletRequest;
047:
048: import org.apache.commons.logging.Log;
049: import org.apache.commons.logging.LogFactory;
050: import org.apache.struts.Globals;
051:
052: /**
053: * <a href="PortalInstances.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Brian Wing Shun Chan
056: * @author Jose Oliver
057: * @author Atul Patel
058: * @author Mika Koivisto
059: *
060: */
061: public class PortalInstances {
062:
063: public static final String DEFAULT_VIRTUAL_HOST = "localhost";
064:
065: public static void addCompanyId(long companyId) {
066: _instance._addCompanyId(companyId);
067: }
068:
069: public static long getCompanyId(HttpServletRequest req) {
070: return _instance._getCompanyId(req);
071: }
072:
073: public static long[] getCompanyIds() {
074: return _instance._getCompanyIds();
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(ServletContext ctx, String webId) {
086: return _instance._initCompany(ctx, webId);
087: }
088:
089: public static boolean isIgnoreHost(String host) {
090: return _instance._isIgnoreHost(host);
091: }
092:
093: public static boolean isIgnorePath(String path) {
094: return _instance._isIgnorePath(path);
095: }
096:
097: private PortalInstances() {
098: _companyIds = new long[0];
099: _ignoreHosts = SetUtil.fromArray(PropsUtil
100: .getArray(PropsUtil.VIRTUAL_HOSTS_IGNORE_HOSTS));
101: _ignorePaths = SetUtil.fromArray(PropsUtil
102: .getArray(PropsUtil.VIRTUAL_HOSTS_IGNORE_PATHS));
103: }
104:
105: private void _addCompanyId(long companyId) {
106: long[] companyIds = new long[_companyIds.length + 1];
107:
108: System.arraycopy(_companyIds, 0, companyIds, 0,
109: _companyIds.length);
110:
111: companyIds[_companyIds.length] = companyId;
112:
113: _companyIds = companyIds;
114: }
115:
116: private long _getCompanyId(HttpServletRequest req) {
117: if (_log.isDebugEnabled()) {
118: _log.debug("Get company id");
119: }
120:
121: Long companyIdObj = (Long) req.getAttribute(WebKeys.COMPANY_ID);
122:
123: if (_log.isDebugEnabled()) {
124: _log.debug("Company id from request " + companyIdObj);
125: }
126:
127: if (companyIdObj != null) {
128: return companyIdObj.longValue();
129: }
130:
131: String host = PortalUtil.getHost(req);
132:
133: if (_log.isDebugEnabled()) {
134: _log.debug("Host " + host);
135: }
136:
137: long companyId = _getCompanyIdByVirtualHost(host);
138:
139: if (_log.isDebugEnabled()) {
140: _log.debug("Company id from host " + companyId);
141: }
142:
143: if (companyId <= 0) {
144: LayoutSet layoutSet = _getLayoutSetByVirtualHost(host);
145:
146: if (layoutSet != null) {
147: companyId = layoutSet.getCompanyId();
148:
149: if (_log.isDebugEnabled()) {
150: _log.debug("Company id " + companyId
151: + " is associated with " + "layout set "
152: + layoutSet.getLayoutSetId());
153: }
154:
155: req.setAttribute(WebKeys.VIRTUAL_HOST_LAYOUT_SET,
156: layoutSet);
157: }
158: }
159:
160: if (companyId <= 0) {
161: companyId = GetterUtil.getLong(CookieKeys.getCookie(req,
162: CookieKeys.COMPANY_ID));
163:
164: if (_log.isDebugEnabled()) {
165: _log.debug("Company id from cookie " + companyId);
166: }
167: }
168:
169: if (companyId <= 0) {
170: companyId = _getDefaultCompanyId();
171:
172: if (_log.isDebugEnabled()) {
173: _log.debug("Default company id " + companyId);
174: }
175: }
176:
177: if (_log.isDebugEnabled()) {
178: _log.debug("Set company id " + companyId);
179: }
180:
181: req.setAttribute(WebKeys.COMPANY_ID, new Long(companyId));
182:
183: CompanyThreadLocal.setCompanyId(companyId);
184:
185: return companyId;
186: }
187:
188: private long _getCompanyIdByVirtualHost(String host) {
189: if (Validator.isNull(host)) {
190: return 0;
191: }
192:
193: try {
194: Iterator itr = CompanyLocalServiceUtil.getCompanies()
195: .iterator();
196:
197: while (itr.hasNext()) {
198: Company company = (Company) itr.next();
199:
200: if (company.getVirtualHost().equals(host)) {
201: return company.getCompanyId();
202: }
203: }
204: } catch (Exception e) {
205: _log.error(e, e);
206: }
207:
208: return 0;
209: }
210:
211: private long[] _getCompanyIds() {
212: return _companyIds;
213: }
214:
215: private long _getDefaultCompanyId() {
216: return _companyIds[0];
217: }
218:
219: private LayoutSet _getLayoutSetByVirtualHost(String host) {
220: if (Validator.isNull(host)) {
221: return null;
222: }
223:
224: if (_isIgnoreHost(host)) {
225: return null;
226: }
227:
228: try {
229: return LayoutSetLocalServiceUtil.getLayoutSet(host);
230: } catch (Exception e) {
231: return null;
232: }
233: }
234:
235: private String[] _getWebIds() {
236: if (_webIds != null) {
237: return _webIds;
238: }
239:
240: if (Validator.isNull(CompanyImpl.DEFAULT_WEB_ID)) {
241: throw new RuntimeException(
242: "Default web id must not be null");
243: }
244:
245: try {
246: List companies = CompanyLocalServiceUtil.getCompanies();
247:
248: List webIdsList = new ArrayList(companies.size());
249:
250: Iterator itr = companies.iterator();
251:
252: while (itr.hasNext()) {
253: Company company = (Company) itr.next();
254:
255: webIdsList.add(company.getWebId());
256: }
257:
258: _webIds = (String[]) webIdsList.toArray(new String[0]);
259: } catch (Exception e) {
260: _log.error(e, e);
261: }
262:
263: if ((_webIds == null) || (_webIds.length == 0)) {
264: _webIds = new String[] { CompanyImpl.DEFAULT_WEB_ID };
265: }
266:
267: return _webIds;
268: }
269:
270: private long _initCompany(ServletContext ctx, String webId) {
271:
272: // Begin initializing company
273:
274: if (_log.isDebugEnabled()) {
275: _log.debug("Begin initializing company with web id "
276: + webId);
277: }
278:
279: long companyId = 0;
280:
281: try {
282: Company company = CompanyLocalServiceUtil
283: .checkCompany(webId);
284:
285: companyId = company.getCompanyId();
286: } catch (Exception e) {
287: _log.error(e, e);
288: }
289:
290: CompanyThreadLocal.setCompanyId(companyId);
291:
292: // Initialize display
293:
294: if (_log.isDebugEnabled()) {
295: _log.debug("Initialize display");
296: }
297:
298: try {
299: String xml = Http.URLtoString(ctx
300: .getResource("/WEB-INF/liferay-display.xml"));
301:
302: PortletCategory portletCategory = (PortletCategory) WebAppPool
303: .get(String.valueOf(companyId),
304: WebKeys.PORTLET_CATEGORY);
305:
306: if (portletCategory == null) {
307: portletCategory = new PortletCategory();
308: }
309:
310: PortletCategory newPortletCategory = PortletLocalServiceUtil
311: .getEARDisplay(xml);
312:
313: portletCategory.merge(newPortletCategory);
314:
315: WebAppPool.put(String.valueOf(companyId),
316: WebKeys.PORTLET_CATEGORY, portletCategory);
317: } catch (Exception e) {
318: _log.error(e, e);
319: }
320:
321: // Check journal content search
322:
323: if (_log.isDebugEnabled()) {
324: _log.debug("Check journal content search");
325: }
326:
327: if (GetterUtil.getBoolean(PropsUtil.get(CompanyImpl.SYSTEM,
328: PropsUtil.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))
329: || GetterUtil
330: .getBoolean(PropsUtil
331: .get(PropsUtil.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) {
332:
333: try {
334: JournalContentSearchLocalServiceUtil
335: .checkContentSearches(companyId);
336: } catch (Exception e) {
337: _log.error(e, e);
338: }
339: }
340:
341: // LDAP Import
342:
343: try {
344: if (PortalLDAPUtil.isImportOnStartup(companyId)) {
345: PortalLDAPUtil.importFromLDAP(companyId);
346: }
347: } catch (Exception e) {
348: _log.error(e, e);
349: }
350:
351: // Message resources
352:
353: if (_log.isDebugEnabled()) {
354: _log.debug("Message resources");
355: }
356:
357: MultiMessageResources messageResources = (MultiMessageResources) ctx
358: .getAttribute(Globals.MESSAGES_KEY);
359:
360: messageResources.setServletContext(ctx);
361:
362: WebAppPool.put(String.valueOf(companyId), Globals.MESSAGES_KEY,
363: messageResources);
364:
365: // Process application startup events
366:
367: if (_log.isDebugEnabled()) {
368: _log.debug("Process application startup events");
369: }
370:
371: try {
372: EventsProcessor.process(PropsUtil
373: .getArray(PropsUtil.APPLICATION_STARTUP_EVENTS),
374: new String[] { String.valueOf(companyId) });
375: } catch (Exception e) {
376: _log.error(e, e);
377: }
378:
379: // End initializing company
380:
381: if (_log.isDebugEnabled()) {
382: _log.debug("End initializing company with web id " + webId
383: + " and company id " + companyId);
384: }
385:
386: addCompanyId(companyId);
387:
388: return companyId;
389: }
390:
391: private boolean _isIgnoreHost(String host) {
392: return _ignoreHosts.contains(host);
393: }
394:
395: private boolean _isIgnorePath(String path) {
396: return _ignorePaths.contains(path);
397: }
398:
399: private static Log _log = LogFactory.getLog(PortalInstances.class);
400:
401: private static PortalInstances _instance = new PortalInstances();
402:
403: private long[] _companyIds;
404: private String[] _webIds;
405: private Set _ignoreHosts;
406: private Set _ignorePaths;
407:
408: }
|