001    /**
002     * Copyright (c) 2000-2010 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.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    }