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