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