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.construct.FlowConstruct; |
18 | |
import org.mule.api.security.SecurityContext; |
19 | |
import org.mule.api.transport.SessionHandler; |
20 | |
import org.mule.config.i18n.CoreMessages; |
21 | |
import org.mule.util.CaseInsensitiveHashMap; |
22 | |
import org.mule.util.UUID; |
23 | |
import org.mule.util.store.DeserializationPostInitialisable; |
24 | |
|
25 | |
import java.io.IOException; |
26 | |
import java.io.ObjectInputStream; |
27 | |
import java.io.ObjectOutputStream; |
28 | |
import java.io.OptionalDataException; |
29 | |
import java.util.Collections; |
30 | |
import java.util.HashMap; |
31 | |
import java.util.Iterator; |
32 | |
import java.util.Map; |
33 | |
import java.util.Set; |
34 | |
|
35 | |
import org.apache.commons.collections.map.CaseInsensitiveMap; |
36 | |
import org.apache.commons.logging.Log; |
37 | |
import org.apache.commons.logging.LogFactory; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public final class DefaultMuleSession implements MuleSession, DeserializationPostInitialisable |
45 | |
{ |
46 | |
|
47 | |
|
48 | |
|
49 | |
private static final long serialVersionUID = 3380926585676521866L; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | private static Log logger = LogFactory.getLog(DefaultMuleSession.class); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | private transient FlowConstruct flowConstruct = null; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | private boolean valid = true; |
67 | |
|
68 | |
private String id; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
private SecurityContext securityContext; |
75 | |
|
76 | 0 | private Map<String, Object> properties = null; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
private transient MuleContext muleContext; |
84 | |
|
85 | 0 | private transient Map<String, Object> serializedData = null; |
86 | |
|
87 | |
public DefaultMuleSession(MuleContext muleContext) |
88 | |
{ |
89 | 0 | this((FlowConstruct) null, muleContext); |
90 | 0 | } |
91 | |
|
92 | |
public DefaultMuleSession(FlowConstruct flowConstruct, MuleContext muleContext) |
93 | 0 | { |
94 | 0 | this.muleContext = muleContext; |
95 | 0 | properties = new CaseInsensitiveHashMap(); |
96 | 0 | id = UUID.getUUID(); |
97 | 0 | this.flowConstruct = flowConstruct; |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
@Deprecated |
104 | |
public DefaultMuleSession(MuleMessage message, |
105 | |
SessionHandler requestSessionHandler, |
106 | |
FlowConstruct flowConstruct, |
107 | |
MuleContext muleContext) throws MuleException |
108 | |
{ |
109 | 0 | this(message, requestSessionHandler, muleContext); |
110 | 0 | if (flowConstruct == null) |
111 | |
{ |
112 | 0 | throw new IllegalArgumentException(CoreMessages.propertiesNotSet("flowConstruct").toString()); |
113 | |
} |
114 | 0 | this.flowConstruct = flowConstruct; |
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
@Deprecated |
121 | |
public DefaultMuleSession(MuleMessage message, SessionHandler requestSessionHandler, MuleContext muleContext) throws MuleException |
122 | |
{ |
123 | 0 | this(muleContext); |
124 | |
|
125 | 0 | if (requestSessionHandler == null) |
126 | |
{ |
127 | 0 | throw new IllegalArgumentException( |
128 | |
CoreMessages.propertiesNotSet("requestSessionHandler").toString()); |
129 | |
} |
130 | |
|
131 | 0 | if (message == null) |
132 | |
{ |
133 | 0 | throw new IllegalArgumentException( |
134 | |
CoreMessages.propertiesNotSet("message").toString()); |
135 | |
} |
136 | |
|
137 | 0 | properties = new CaseInsensitiveMap(); |
138 | 0 | requestSessionHandler.retrieveSessionInfoFromMessage(message, this); |
139 | 0 | id = getProperty(requestSessionHandler.getSessionIDKey()); |
140 | 0 | if (id == null) |
141 | |
{ |
142 | 0 | id = UUID.getUUID(); |
143 | 0 | if (logger.isDebugEnabled()) |
144 | |
{ |
145 | 0 | logger.debug("There is no session id on the request using key: " |
146 | |
+ requestSessionHandler.getSessionIDKey() + ". Generating new session id: " + id); |
147 | |
} |
148 | |
} |
149 | 0 | else if (logger.isDebugEnabled()) |
150 | |
{ |
151 | 0 | logger.debug("Got session with id: " + id); |
152 | |
} |
153 | 0 | } |
154 | |
|
155 | |
public DefaultMuleSession(MuleSession session, MuleContext muleContext) |
156 | 0 | { |
157 | 0 | this.muleContext = muleContext; |
158 | 0 | this.id = session.getId(); |
159 | 0 | this.securityContext = session.getSecurityContext(); |
160 | 0 | this.flowConstruct = session.getFlowConstruct(); |
161 | 0 | this.valid = session.isValid(); |
162 | |
|
163 | 0 | this.properties = new HashMap<String, Object>(); |
164 | 0 | for (String key : session.getPropertyNamesAsSet()) |
165 | |
{ |
166 | 0 | this.properties.put(key, session.getProperty(key)); |
167 | |
} |
168 | 0 | } |
169 | |
|
170 | |
public String getId() |
171 | |
{ |
172 | 0 | return id; |
173 | |
} |
174 | |
|
175 | |
public boolean isValid() |
176 | |
{ |
177 | 0 | return valid; |
178 | |
} |
179 | |
|
180 | |
public void setValid(boolean value) |
181 | |
{ |
182 | 0 | valid = value; |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public FlowConstruct getFlowConstruct() |
189 | |
{ |
190 | 0 | return flowConstruct; |
191 | |
} |
192 | |
|
193 | |
public void setFlowConstruct(FlowConstruct flowConstruct) |
194 | |
{ |
195 | 0 | this.flowConstruct = flowConstruct; |
196 | 0 | } |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public void setSecurityContext(SecurityContext context) |
206 | |
{ |
207 | 0 | securityContext = context; |
208 | 0 | } |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
public SecurityContext getSecurityContext() |
217 | |
{ |
218 | 0 | return securityContext; |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public void setProperty(String key, Object value) |
229 | |
{ |
230 | 0 | properties.put(key, value); |
231 | 0 | } |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
@SuppressWarnings("unchecked") |
240 | |
public <T> T getProperty(Object key) |
241 | |
{ |
242 | 0 | return (T) properties.get(key); |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public Object removeProperty(Object key) |
252 | |
{ |
253 | 0 | return properties.remove(key); |
254 | |
} |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
@Deprecated |
265 | |
public Iterator<String> getPropertyNames() |
266 | |
{ |
267 | 0 | return properties.keySet().iterator(); |
268 | |
} |
269 | |
|
270 | |
public Set<String> getPropertyNamesAsSet() |
271 | |
{ |
272 | 0 | return Collections.unmodifiableSet(properties.keySet()); |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
private void writeObject(ObjectOutputStream out) throws IOException |
280 | |
{ |
281 | 0 | out.defaultWriteObject(); |
282 | |
|
283 | 0 | if (getFlowConstruct() != null) |
284 | |
{ |
285 | 0 | out.writeObject(getFlowConstruct() != null ? getFlowConstruct().getName() : "null"); |
286 | |
} |
287 | 0 | } |
288 | |
|
289 | |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException |
290 | |
{ |
291 | 0 | in.defaultReadObject(); |
292 | 0 | serializedData = new HashMap<String, Object>(); |
293 | |
|
294 | |
try |
295 | |
{ |
296 | |
|
297 | 0 | serializedData.put("serviceName", in.readObject()); |
298 | |
} |
299 | 0 | catch (OptionalDataException e) |
300 | |
{ |
301 | |
|
302 | 0 | } |
303 | 0 | } |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
public void initAfterDeserialisation(MuleContext muleContext) throws MuleException |
317 | |
{ |
318 | 0 | String serviceName = (String) serializedData.get("serviceName"); |
319 | |
|
320 | 0 | if (serviceName != null) |
321 | |
{ |
322 | 0 | flowConstruct = muleContext.getRegistry().lookupService(serviceName); |
323 | |
} |
324 | 0 | serializedData = null; |
325 | 0 | } |
326 | |
|
327 | |
} |