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