View Javadoc

1   /*
2    * $Id: SerializeOnlySessionHandler.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.session;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.MuleSession;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.transport.SessionHandler;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.lang.SerializationUtils;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * A session handler used to store and retrieve session information on an
27   * event. The MuleSession information is stored as a header on the message (does not
28   * support Tcp, Udp, etc. unless the MuleMessage object is serialised across the
29   * wire). The session is stored in the "MULE_SESSION" property as an array of bytes (byte[])
30   */
31  public class SerializeOnlySessionHandler implements SessionHandler
32  {
33      protected transient Log logger = LogFactory.getLog(getClass());
34  
35      public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException
36      {
37          MuleSession session = null;
38          byte[] serializedSession = message.getInboundProperty(MuleProperties.MULE_SESSION_PROPERTY);
39  
40          if (serializedSession != null)
41          {
42              session = (MuleSession) SerializationUtils.deserialize(serializedSession);
43          }
44          return session;
45      }
46  
47      /**
48       * @deprecated Use retrieveSessionInfoFromMessage(MuleMessage message) instead
49       */
50      public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException
51      {
52          session = retrieveSessionInfoFromMessage(message);
53      }
54  
55      public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
56      {
57          byte[] serializedSession = SerializationUtils.serialize(removeNonSerializableProperties(session));
58          
59          if (logger.isDebugEnabled())
60          {
61              logger.debug("Adding serialized Session header to message: " + serializedSession);
62          }
63          message.setOutboundProperty(MuleProperties.MULE_SESSION_PROPERTY, serializedSession);
64      }
65      
66      protected MuleSession removeNonSerializableProperties(MuleSession session)
67      {
68          for (String key : session.getPropertyNamesAsSet())
69          {
70              final Object value = session.getProperty(key);
71              if (!(value instanceof Serializable))
72              {
73                  logger.warn(String.format("Property %s is not serializable, it will not be preserved " +
74                                            "as part of the MuleSession", key));
75                  session.removeProperty(key);
76              }
77          }
78          return session;
79      }
80      
81      /**
82       * @deprecated This method is no longer needed and will be removed in the next major release
83       */
84      public String getSessionIDKey()
85      {
86          return "ID";
87      }
88  }