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 | 0 | public class SerializeOnlySessionHandler implements SessionHandler |
31 | |
{ |
32 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
33 | |
|
34 | |
public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException |
35 | |
{ |
36 | 0 | MuleSession session = null; |
37 | 0 | byte[] serializedSession = message.getInboundProperty(MuleProperties.MULE_SESSION_PROPERTY); |
38 | |
|
39 | 0 | if (serializedSession != null) |
40 | |
{ |
41 | 0 | session = (MuleSession) SerializationUtils.deserialize(serializedSession, message.getMuleContext()); |
42 | |
} |
43 | 0 | return session; |
44 | |
} |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException |
50 | |
{ |
51 | 0 | session = retrieveSessionInfoFromMessage(message); |
52 | 0 | } |
53 | |
|
54 | |
public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException |
55 | |
{ |
56 | 0 | byte[] serializedSession = SerializationUtils.serialize(removeNonSerializableProperties(session,message.getMuleContext())); |
57 | |
|
58 | 0 | if (logger.isDebugEnabled()) |
59 | |
{ |
60 | 0 | logger.debug("Adding serialized Session header to message: " + serializedSession); |
61 | |
} |
62 | 0 | message.setOutboundProperty(MuleProperties.MULE_SESSION_PROPERTY, serializedSession); |
63 | 0 | } |
64 | |
|
65 | |
protected MuleSession removeNonSerializableProperties(final MuleSession session, |
66 | |
final MuleContext muleContext) |
67 | |
{ |
68 | 0 | DefaultMuleSession copy = new DefaultMuleSession(session, muleContext); |
69 | 0 | Iterator<Entry<String, Object>> propertyIterator = copy.getProperties().entrySet().iterator(); |
70 | 0 | while (propertyIterator.hasNext()) |
71 | |
{ |
72 | 0 | final Entry<String, Object> entry = propertyIterator.next(); |
73 | 0 | if (!(entry.getValue() instanceof Serializable)) |
74 | |
{ |
75 | 0 | logger.warn(String.format("Property %s is not serializable, it will not be preserved " |
76 | |
+ "as part of the MuleSession", entry.getKey())); |
77 | 0 | propertyIterator.remove(); |
78 | |
} |
79 | 0 | } |
80 | 0 | return copy; |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public String getSessionIDKey() |
87 | |
{ |
88 | 0 | return "ID"; |
89 | |
} |
90 | |
} |