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.servlet;
016    
017    import com.liferay.portal.events.EventsProcessorUtil;
018    import com.liferay.portal.kernel.events.ActionException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.BasePortalLifecycle;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.util.PropsValues;
024    
025    import javax.servlet.http.HttpSession;
026    import javax.servlet.http.HttpSessionEvent;
027    
028    /**
029     * @author Michael Young
030     */
031    public class PortalSessionCreator extends BasePortalLifecycle {
032    
033            public PortalSessionCreator(HttpSessionEvent httpSessionEvent) {
034                    _httpSessionEvent = httpSessionEvent;
035    
036                    registerPortalLifecycle();
037            }
038    
039            protected void doPortalDestroy() {
040            }
041    
042            protected void doPortalInit() {
043                    if (PropsValues.SESSION_DISABLED) {
044                            return;
045                    }
046    
047                    HttpSession session = _httpSessionEvent.getSession();
048    
049                    try {
050                            PortalSessionContext.put(session.getId(), session);
051                    }
052                    catch (IllegalStateException ise) {
053                            if (_log.isWarnEnabled()) {
054                                    _log.warn(ise, ise);
055                            }
056                    }
057    
058                    // Process session created events
059    
060                    try {
061                            EventsProcessorUtil.process(
062                                    PropsKeys.SERVLET_SESSION_CREATE_EVENTS,
063                                    PropsValues.SERVLET_SESSION_CREATE_EVENTS, session);
064                    }
065                    catch (ActionException ae) {
066                            _log.error(ae, ae);
067                    }
068            }
069    
070            private static Log _log = LogFactoryUtil.getLog(
071                    PortalSessionCreator.class);
072    
073            private HttpSessionEvent _httpSessionEvent;
074    
075    }