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.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>(
055                                    PortalSessionActivationListener.getInstance());
056    
057                    session.setAttribute(
058                            PortalSessionActivationListener.class.getName(), transientValue);
059            }
060    
061            @Override
062            public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
063                    new PortalSessionCreator(httpSessionEvent);
064            }
065    
066            @Override
067            public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {
068            }
069    
070            private static PortalSessionActivationListener _instance =
071                    new PortalSessionActivationListener();
072    
073    }