001    /**
002     * Copyright (c) 2000-present 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.kernel.util.TransientValue;
018    
019    import java.io.Serializable;
020    
021    import javax.servlet.http.HttpSession;
022    import javax.servlet.http.HttpSessionActivationListener;
023    import javax.servlet.http.HttpSessionEvent;
024    
025    /**
026     * @author Alexander Chow
027     */
028    public class PortalSessionActivationListener
029            implements HttpSessionActivationListener, Serializable {
030    
031            public static PortalSessionActivationListener getInstance() {
032                    return _instance;
033            }
034    
035            public static PortalSessionActivationListener getInstance(
036                    HttpSession session) {
037    
038                    TransientValue<PortalSessionActivationListener> transientValue =
039                            (TransientValue<PortalSessionActivationListener>)
040                                    session.getAttribute(
041                                            PortalSessionActivationListener.class.getName());
042    
043                    PortalSessionActivationListener portalSessionActivationListener = null;
044    
045                    if (transientValue != null) {
046                            portalSessionActivationListener = transientValue.getValue();
047                    }
048    
049                    return portalSessionActivationListener;
050            }
051    
052            public static void setInstance(HttpSession session) {
053                    TransientValue<PortalSessionActivationListener> transientValue =
054                            new TransientValue<>(PortalSessionActivationListener.getInstance());
055    
056                    session.setAttribute(
057                            PortalSessionActivationListener.class.getName(), transientValue);
058            }
059    
060            @Override
061            public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
062                    new PortalSessionCreator(httpSessionEvent);
063            }
064    
065            @Override
066            public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
067            }
068    
069            private static final PortalSessionActivationListener _instance =
070                    new PortalSessionActivationListener();
071    
072    }