1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.servlet;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  
21  import java.io.Serializable;
22  
23  import javax.servlet.ServletContext;
24  import javax.servlet.http.HttpSession;
25  import javax.servlet.http.HttpSessionAttributeListener;
26  import javax.servlet.http.HttpSessionBindingEvent;
27  
28  /**
29   * <a href="SerializableSessionAttributeListener.java.html"><b><i>View Source
30   * </i></b></a>
31   *
32   * @author Bruno Farache
33   */
34  public class SerializableSessionAttributeListener
35      implements HttpSessionAttributeListener {
36  
37      public void attributeAdded(HttpSessionBindingEvent event) {
38          String name = event.getName();
39          Object value = event.getValue();
40  
41          if (!(value instanceof Serializable)) {
42              _log.error(
43                  value.getClass().getName() +
44                      " is not serializable and will prevent this session from " +
45                          "being replicated");
46  
47              if (_requiresSerializable == null) {
48                  HttpSession session = event.getSession();
49  
50                  ServletContext servletContext = session.getServletContext();
51  
52                  _requiresSerializable = Boolean.valueOf(
53                      GetterUtil.getBoolean(
54                          servletContext.getInitParameter(
55                              "session-attributes-requires-serializable")));
56              }
57  
58              if (_requiresSerializable) {
59                  HttpSession session = event.getSession();
60  
61                  session.removeAttribute(name);
62              }
63          }
64      }
65  
66      public void attributeRemoved(HttpSessionBindingEvent event) {
67      }
68  
69      public void attributeReplaced(HttpSessionBindingEvent event) {
70          attributeAdded(event);
71      }
72  
73      private static Log _log = LogFactoryUtil.getLog(
74          SerializableSessionAttributeListener.class);
75  
76      private Boolean _requiresSerializable;
77  
78  }