1
2
3
4
5
6
7
8
9
10
11 package org.mule.session;
12
13 import org.mule.api.MuleContext;
14 import org.mule.api.MuleException;
15 import org.mule.api.MuleMessage;
16 import org.mule.api.MuleSession;
17 import org.mule.api.config.MuleProperties;
18 import org.mule.api.transport.SessionHandler;
19 import org.mule.util.SerializationUtils;
20
21 import java.io.Serializable;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26
27
28
29
30
31
32 public class SerializeOnlySessionHandler implements SessionHandler
33 {
34 protected transient Log logger = LogFactory.getLog(getClass());
35
36 public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException
37 {
38 MuleSession session = null;
39 byte[] serializedSession = message.getInboundProperty(MuleProperties.MULE_SESSION_PROPERTY);
40
41 if (serializedSession != null)
42 {
43 session = (MuleSession) SerializationUtils.deserialize(serializedSession, message.getMuleContext());
44 }
45 return session;
46 }
47
48
49
50
51 public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException
52 {
53 session = retrieveSessionInfoFromMessage(message);
54 }
55
56 public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException
57 {
58 byte[] serializedSession = SerializationUtils.serialize(removeNonSerializableProperties(session,message.getMuleContext()));
59
60 if (logger.isDebugEnabled())
61 {
62 logger.debug("Adding serialized Session header to message: " + serializedSession);
63 }
64 message.setOutboundProperty(MuleProperties.MULE_SESSION_PROPERTY, serializedSession);
65 }
66
67 protected MuleSession removeNonSerializableProperties(final MuleSession session, final MuleContext muleContext)
68 {
69 MuleSession copy = new DefaultMuleSession(session, muleContext);
70 for (String key : copy.getPropertyNamesAsSet())
71 {
72 final Object value = copy.getProperty(key);
73 if (!(value instanceof Serializable))
74 {
75 logger.warn(String.format("Property %s is not serializable, it will not be preserved " +
76 "as part of the MuleSession", key));
77 copy.removeProperty(key);
78 }
79 }
80 return copy;
81 }
82
83
84
85
86 public String getSessionIDKey()
87 {
88 return "ID";
89 }
90 }