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.kernel.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    
021    import java.io.Serializable;
022    
023    import javax.servlet.ServletContext;
024    import javax.servlet.http.HttpSession;
025    import javax.servlet.http.HttpSessionAttributeListener;
026    import javax.servlet.http.HttpSessionBindingEvent;
027    
028    /**
029     * @author Bruno Farache
030     */
031    public class SerializableSessionAttributeListener
032            implements HttpSessionAttributeListener {
033    
034            public void attributeAdded(HttpSessionBindingEvent event) {
035                    String name = event.getName();
036                    Object value = event.getValue();
037    
038                    if (!(value instanceof Serializable)) {
039                            _log.error(
040                                    value.getClass().getName() +
041                                            " is not serializable and will prevent this session from " +
042                                                    "being replicated");
043    
044                            if (_requiresSerializable == null) {
045                                    HttpSession session = event.getSession();
046    
047                                    ServletContext servletContext = session.getServletContext();
048    
049                                    _requiresSerializable = Boolean.valueOf(
050                                            GetterUtil.getBoolean(
051                                                    servletContext.getInitParameter(
052                                                            "session-attributes-requires-serializable")));
053                            }
054    
055                            if (_requiresSerializable) {
056                                    HttpSession session = event.getSession();
057    
058                                    session.removeAttribute(name);
059                            }
060                    }
061            }
062    
063            public void attributeRemoved(HttpSessionBindingEvent event) {
064            }
065    
066            public void attributeReplaced(HttpSessionBindingEvent event) {
067                    attributeAdded(event);
068            }
069    
070            private static Log _log = LogFactoryUtil.getLog(
071                    SerializableSessionAttributeListener.class);
072    
073            private Boolean _requiresSerializable;
074    
075    }