001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.kernel.servlet.ServletVersionDetector;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.ConcurrentHashSet;
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            public void attributeAdded(HttpSessionBindingEvent event) {
051                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
052                            return;
053                    }
054    
055                    HttpSession session = event.getSession();
056    
057                    if (!_sessionIds.contains(session.getId())) {
058                            return;
059                    }
060    
061                    SharedSessionAttributeCache sharedSessionAttributeCache =
062                            SharedSessionAttributeCache.getInstance(session);
063    
064                    String name = event.getName();
065    
066                    if (ArrayUtil.contains(
067                                    PropsValues.SHARED_SESSION_ATTRIBUTES_EXCLUDES, name)) {
068    
069                            return;
070                    }
071    
072                    for (String sharedName : PropsValues.SHARED_SESSION_ATTRIBUTES) {
073                            if (!name.startsWith(sharedName)) {
074                                    continue;
075                            }
076    
077                            sharedSessionAttributeCache.setAttribute(name, event.getValue());
078    
079                            return;
080                    }
081            }
082    
083            public void attributeRemoved(HttpSessionBindingEvent event) {
084                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
085                            return;
086                    }
087    
088                    HttpSession session = event.getSession();
089    
090                    if (!_sessionIds.contains(session.getId())) {
091                            return;
092                    }
093    
094                    SharedSessionAttributeCache sharedSessionAttributeCache =
095                            SharedSessionAttributeCache.getInstance(session);
096    
097                    sharedSessionAttributeCache.removeAttribute(event.getName());
098            }
099    
100            public void attributeReplaced(HttpSessionBindingEvent event) {
101                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
102                            return;
103                    }
104    
105                    HttpSession session = event.getSession();
106    
107                    if (!_sessionIds.contains(session.getId())) {
108                            return;
109                    }
110    
111                    SharedSessionAttributeCache sharedSessionAttributeCache =
112                            SharedSessionAttributeCache.getInstance(session);
113    
114                    if (sharedSessionAttributeCache.contains(event.getName())) {
115                            Object value = session.getAttribute(event.getName());
116    
117                            sharedSessionAttributeCache.setAttribute(event.getName(), value);
118                    }
119            }
120    
121            public void sessionCreated(HttpSessionEvent event) {
122                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
123                            return;
124                    }
125    
126                    HttpSession session = event.getSession();
127    
128                    SharedSessionAttributeCache.getInstance(session);
129    
130                    _sessionIds.add(session.getId());
131            }
132    
133            public void sessionDestroyed(HttpSessionEvent event) {
134                    if (PropsValues.SESSION_DISABLED || ServletVersionDetector.is2_5()) {
135                            return;
136                    }
137    
138                    HttpSession session = event.getSession();
139    
140                    _sessionIds.remove(session.getId());
141            }
142    
143            private Set<String> _sessionIds;
144    
145    }