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.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     * @deprecated As of 7.0.0, with no direct replacement
039     */
040    public class SharedSessionAttributeListener
041            implements HttpSessionAttributeListener, HttpSessionListener {
042    
043            public SharedSessionAttributeListener() {
044                    if (ServletVersionDetector.is2_5()) {
045                            _sessionIds = null;
046                    }
047                    else {
048                            _sessionIds = new ConcurrentHashSet<String>();
049                    }
050            }
051    
052            @Override
053            public void attributeAdded(HttpSessionBindingEvent event) {
054                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
055                            return;
056                    }
057    
058                    HttpSession session = event.getSession();
059    
060                    if (!_sessionIds.contains(session.getId())) {
061                            return;
062                    }
063    
064                    SharedSessionAttributeCache sharedSessionAttributeCache =
065                            SharedSessionAttributeCache.getInstance(session);
066    
067                    String name = event.getName();
068    
069                    if (ArrayUtil.contains(
070                                    PropsValues.SESSION_SHARED_ATTRIBUTES_EXCLUDES, name)) {
071    
072                            return;
073                    }
074    
075                    for (String sharedName : PropsValues.SESSION_SHARED_ATTRIBUTES) {
076                            if (!name.startsWith(sharedName)) {
077                                    continue;
078                            }
079    
080                            sharedSessionAttributeCache.setAttribute(name, event.getValue());
081    
082                            return;
083                    }
084            }
085    
086            @Override
087            public void attributeRemoved(HttpSessionBindingEvent event) {
088                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
089                            return;
090                    }
091    
092                    HttpSession session = event.getSession();
093    
094                    if (!_sessionIds.contains(session.getId())) {
095                            return;
096                    }
097    
098                    SharedSessionAttributeCache sharedSessionAttributeCache =
099                            SharedSessionAttributeCache.getInstance(session);
100    
101                    sharedSessionAttributeCache.removeAttribute(event.getName());
102            }
103    
104            @Override
105            public void attributeReplaced(HttpSessionBindingEvent event) {
106                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
107                            return;
108                    }
109    
110                    HttpSession session = event.getSession();
111    
112                    if (!_sessionIds.contains(session.getId())) {
113                            return;
114                    }
115    
116                    SharedSessionAttributeCache sharedSessionAttributeCache =
117                            SharedSessionAttributeCache.getInstance(session);
118    
119                    if (sharedSessionAttributeCache.contains(event.getName())) {
120                            Object value = session.getAttribute(event.getName());
121    
122                            sharedSessionAttributeCache.setAttribute(event.getName(), value);
123                    }
124            }
125    
126            @Override
127            public void sessionCreated(HttpSessionEvent event) {
128                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
129                            return;
130                    }
131    
132                    HttpSession session = event.getSession();
133    
134                    SharedSessionAttributeCache.getInstance(session);
135    
136                    _sessionIds.add(session.getId());
137            }
138    
139            @Override
140            public void sessionDestroyed(HttpSessionEvent event) {
141                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
142                            return;
143                    }
144    
145                    HttpSession session = event.getSession();
146    
147                    _sessionIds.remove(session.getId());
148            }
149    
150            private final Set<String> _sessionIds;
151    
152    }