1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }