1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class SerializeOnlySessionHandler implements SessionHandler |
32 | |
{ |
33 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
34 | |
|
35 | |
public MuleSession retrieveSessionInfoFromMessage(MuleMessage message) throws MuleException |
36 | |
{ |
37 | 0 | MuleSession session = null; |
38 | 0 | byte[] serializedSession = message.getInboundProperty(MuleProperties.MULE_SESSION_PROPERTY); |
39 | |
|
40 | 0 | if (serializedSession != null) |
41 | |
{ |
42 | 0 | session = (MuleSession) SerializationUtils.deserialize(serializedSession); |
43 | |
} |
44 | 0 | return session; |
45 | |
} |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public void retrieveSessionInfoFromMessage(MuleMessage message, MuleSession session) throws MuleException |
51 | |
{ |
52 | 0 | session = retrieveSessionInfoFromMessage(message); |
53 | 0 | } |
54 | |
|
55 | |
public void storeSessionInfoToMessage(MuleSession session, MuleMessage message) throws MuleException |
56 | |
{ |
57 | 0 | byte[] serializedSession = SerializationUtils.serialize(removeNonSerializableProperties(session)); |
58 | |
|
59 | 0 | if (logger.isDebugEnabled()) |
60 | |
{ |
61 | 0 | logger.debug("Adding serialized Session header to message: " + serializedSession); |
62 | |
} |
63 | 0 | message.setOutboundProperty(MuleProperties.MULE_SESSION_PROPERTY, serializedSession); |
64 | 0 | } |
65 | |
|
66 | |
protected MuleSession removeNonSerializableProperties(MuleSession session) |
67 | |
{ |
68 | 0 | for (String key : session.getPropertyNamesAsSet()) |
69 | |
{ |
70 | 0 | final Object value = session.getProperty(key); |
71 | 0 | if (!(value instanceof Serializable)) |
72 | |
{ |
73 | 0 | logger.warn(String.format("Property %s is not serializable, it will not be preserved " + |
74 | |
"as part of the MuleSession", key)); |
75 | 0 | session.removeProperty(key); |
76 | |
} |
77 | 0 | } |
78 | 0 | return session; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public String getSessionIDKey() |
85 | |
{ |
86 | 0 | return "ID"; |
87 | |
} |
88 | |
} |