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.concurrent.ConcurrentHashSet;
018    import com.liferay.portal.kernel.servlet.ServletVersionDetector;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.util.PropsValues;
021    
022    import java.util.Set;
023    
024    import javax.servlet.http.HttpSession;
025    import javax.servlet.http.HttpSessionAttributeListener;
026    import javax.servlet.http.HttpSessionBindingEvent;
027    import javax.servlet.http.HttpSessionEvent;
028    import javax.servlet.http.HttpSessionListener;
029    
030    /**
031     * <p>
032     * Listener used to help manage shared session attributes into a cache. This
033     * cache is more thread safe than the HttpSession and leads to fewer problems
034     * with shared session attributes being modified out of sequence.
035     * </p>
036     *
037     * @author Michael C. Han
038     */
039    public class SharedSessionAttributeListener
040            implements HttpSessionAttributeListener, HttpSessionListener {
041    
042            public SharedSessionAttributeListener() {
043                    if (ServletVersionDetector.is2_5()) {
044                            return;
045                    }
046    
047                    _sessionIds = new ConcurrentHashSet<String>();
048            }
049    
050            @Override
051            public void attributeAdded(HttpSessionBindingEvent event) {
052                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
053                            return;
054                    }
055    
056                    HttpSession session = event.getSession();
057    
058                    if (!_sessionIds.contains(session.getId())) {
059                            return;
060                    }
061    
062                    SharedSessionAttributeCache sharedSessionAttributeCache =
063                            SharedSessionAttributeCache.getInstance(session);
064    
065                    String name = event.getName();
066    
067                    if (ArrayUtil.contains(
068                                    PropsValues.SESSION_SHARED_ATTRIBUTES_EXCLUDES, name)) {
069    
070                            return;
071                    }
072    
073                    for (String sharedName : PropsValues.SESSION_SHARED_ATTRIBUTES) {
074                            if (!name.startsWith(sharedName)) {
075                                    continue;
076                            }
077    
078                            sharedSessionAttributeCache.setAttribute(name, event.getValue());
079    
080                            return;
081                    }
082            }
083    
084            @Override
085            public void attributeRemoved(HttpSessionBindingEvent event) {
086                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
087                            return;
088                    }
089    
090                    HttpSession session = event.getSession();
091    
092                    if (!_sessionIds.contains(session.getId())) {
093                            return;
094                    }
095    
096                    SharedSessionAttributeCache sharedSessionAttributeCache =
097                            SharedSessionAttributeCache.getInstance(session);
098    
099                    sharedSessionAttributeCache.removeAttribute(event.getName());
100            }
101    
102            @Override
103            public void attributeReplaced(HttpSessionBindingEvent event) {
104                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
105                            return;
106                    }
107    
108                    HttpSession session = event.getSession();
109    
110                    if (!_sessionIds.contains(session.getId())) {
111                            return;
112                    }
113    
114                    SharedSessionAttributeCache sharedSessionAttributeCache =
115                            SharedSessionAttributeCache.getInstance(session);
116    
117                    if (sharedSessionAttributeCache.contains(event.getName())) {
118                            Object value = session.getAttribute(event.getName());
119    
120                            sharedSessionAttributeCache.setAttribute(event.getName(), value);
121                    }
122            }
123    
124            @Override
125            public void sessionCreated(HttpSessionEvent event) {
126                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
127                            return;
128                    }
129    
130                    HttpSession session = event.getSession();
131    
132                    SharedSessionAttributeCache.getInstance(session);
133    
134                    _sessionIds.add(session.getId());
135            }
136    
137            @Override
138            public void sessionDestroyed(HttpSessionEvent event) {
139                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
140                            return;
141                    }
142    
143                    HttpSession session = event.getSession();
144    
145                    _sessionIds.remove(session.getId());
146            }
147    
148            private Set<String> _sessionIds;
149    
150    }