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