/
AbstractConversationContext.java
519 lines (466 loc) · 18.2 KB
/
AbstractConversationContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
*
* Use is subject to license terms.
*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.context;
import static org.jboss.weld.context.conversation.ConversationIdGenerator.CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME;
import static org.jboss.weld.util.reflection.Reflections.cast;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Instance;
import org.jboss.weld.Container;
import org.jboss.weld.context.beanstore.BoundBeanStore;
import org.jboss.weld.context.beanstore.ConversationNamingScheme;
import org.jboss.weld.context.beanstore.NamingScheme;
import org.jboss.weld.context.conversation.ConversationIdGenerator;
import org.jboss.weld.context.conversation.ConversationImpl;
import org.jboss.weld.logging.messages.ConversationMessage;
/**
* The base of the conversation context, which can use a variety of storage
* forms
*
* @author Pete Muir
*/
public abstract class AbstractConversationContext<R, S> extends AbstractBoundContext<R> implements ConversationContext
{
private static final String IDENTIFIER = AbstractConversationContext.class.getName();
private static final String CURRENT_CONVERSATION_ATTRIBUTE_NAME = ConversationContext.class.getName() + ".currentConversation";
public static final String CONVERSATIONS_ATTRIBUTE_NAME = ConversationContext.class.getName() + ".conversations";
private static final long DEFAULT_TIMEOUT = 10 * 60 * 1000L;
private static final long CONCURRENT_ACCESS_TIMEOUT = 1000L;
private static final String PARAMETER_NAME = "cid";
private final AtomicReference<String> parameterName;
private final AtomicLong defaultTimeout;
private final AtomicLong concurrentAccessTimeout;
private final ThreadLocal<R> associated;
private final Instance<ConversationContext> conversationContexts;
public AbstractConversationContext()
{
super(true);
this.parameterName = new AtomicReference<String>(PARAMETER_NAME);
this.defaultTimeout = new AtomicLong(DEFAULT_TIMEOUT);
this.concurrentAccessTimeout = new AtomicLong(CONCURRENT_ACCESS_TIMEOUT);
this.associated = new ThreadLocal<R>();
this.conversationContexts = Container.instance().deploymentManager().instance().select(ConversationContext.class);
}
public String getParameterName()
{
return parameterName.get();
}
public void setParameterName(String cid)
{
this.parameterName.set(cid);
}
public void setConcurrentAccessTimeout(long timeout)
{
this.concurrentAccessTimeout.set(timeout);
}
public long getConcurrentAccessTimeout()
{
return concurrentAccessTimeout.get();
}
public void setDefaultTimeout(long timeout)
{
this.defaultTimeout.set(timeout);
}
public long getDefaultTimeout()
{
return defaultTimeout.get();
}
public boolean associate(R request)
{
if (getRequestAttribute(request, IDENTIFIER) == null)
{
this.associated.set(request);
/*
* We need to delay attaching the bean store until activate() is called
* so that we can attach the correct conversation id
*/
// Don't reassociate
setRequestAttribute(request, IDENTIFIER, IDENTIFIER);
/*
* We may need access to the conversation id generator and
* conversations. If the session already exists, we can load it from
* there, otherwise we can create a new conversation id generator and
* conversations collection. If the the session exists when the request
* is dissociated, then we store them in the session then.
*
* We always store the generator and conversation map in the request
* for temporary usage.
*/
if (getSessionAttribute(request, CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME, false) == null)
{
ConversationIdGenerator generator = new ConversationIdGenerator();
setRequestAttribute(request, CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME, generator);
setSessionAttribute(request, CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME, generator, false);
}
else
{
setRequestAttribute(request, CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME, getSessionAttribute(request, CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME, true));
}
if (getSessionAttribute(request, CONVERSATIONS_ATTRIBUTE_NAME, false) == null)
{
Map<String, ManagedConversation> conversations = new HashMap<String, ManagedConversation>();
setRequestAttribute(request, CONVERSATIONS_ATTRIBUTE_NAME, conversations);
setSessionAttribute(request, CONVERSATIONS_ATTRIBUTE_NAME, conversations, false);
}
else
{
setRequestAttribute(request, CONVERSATIONS_ATTRIBUTE_NAME, getSessionAttribute(request, CONVERSATIONS_ATTRIBUTE_NAME, true));
}
return true;
}
else
{
return false;
}
}
public boolean dissociate(R request)
{
if (isAssociated() && getRequestAttribute(request, IDENTIFIER) != null)
{
try
{
/*
* If the session is available, store the conversation id generator and
* conversations if necessary.
*/
if (getSessionAttribute(request, CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME, false) == null)
{
setSessionAttribute(request, CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME, getRequestAttribute(request, CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME), false);
}
if (getSessionAttribute(request, CONVERSATIONS_ATTRIBUTE_NAME, false) == null)
{
setSessionAttribute(request, CONVERSATIONS_ATTRIBUTE_NAME, getRequestAttribute(request, CONVERSATIONS_ATTRIBUTE_NAME), false);
}
this.associated.set(null);
removeRequestAttribute(request, IDENTIFIER);
return true;
}
finally
{
cleanup();
}
}
else
{
return false;
}
}
@Override
public void activate()
{
this.activate(null);
}
@Override
public void deactivate()
{
// Disassociate from the current conversation
if (getBeanStore() != null)
{
if (!isAssociated())
{
throw new IllegalStateException("Must call associate() before calling deactivate()");
}
if (getCurrentConversation().isTransient())
{
destroy();
}
else
{
try
{
// Update the conversation timestamp
getCurrentConversation().touch();
if (!getBeanStore().isAttached())
{
/*
* This was a transient conversation at the beginning of the
* request, so we need to update the CID it uses, and attach
* it. We also add it to the conversations the session knows
* about.
*/
if (!(getRequestAttribute(getRequest(), ConversationNamingScheme.PARAMETER_NAME) instanceof ConversationNamingScheme))
{
throw new IllegalStateException("Unable to find ConversationNamingScheme in the request, this conversation wasn't transient at the start of the request");
}
((ConversationNamingScheme) getRequestAttribute(getRequest(), ConversationNamingScheme.PARAMETER_NAME)).setCid(getCurrentConversation().getId());
getBeanStore().attach();
getConversationMap().put(getCurrentConversation().getId(), getCurrentConversation());
}
}
finally
{
getCurrentConversation().unlock();
}
}
setBeanStore(null);
// Clean up any expired conversations
Iterator<Entry<String, ManagedConversation>> entryIterator = getConversationMap().entrySet().iterator();
while (entryIterator.hasNext())
{
Entry<String, ManagedConversation> entry = entryIterator.next();
if (entry.getValue().isTransient())
{
destroyConversation(getSessionFromRequest(getRequest(), false), entry.getKey());
entryIterator.remove();
}
}
// deactivate the context
super.setActive(false);
}
else
{
throw new IllegalStateException("Context is not active");
}
}
public void activate(String cid)
{
if (getBeanStore() == null)
{
if (!isAssociated())
{
throw new IllegalStateException("Must call associate() before calling activate()");
}
// Activate the context
super.setActive(true);
// Attach the conversation
if (cid == null || getConversation(cid) == null)
{
ManagedConversation conversation = new ConversationImpl(conversationContexts);
setRequestAttribute(getRequest(), CURRENT_CONVERSATION_ATTRIBUTE_NAME, conversation);
// Set a temporary bean store, this will be attached at the end of
// the request if needed
NamingScheme namingScheme = new ConversationNamingScheme(ConversationContext.class.getName(), "transient");
setBeanStore(createRequestBeanStore(namingScheme, getRequest()));
setRequestAttribute(getRequest(), ConversationNamingScheme.PARAMETER_NAME, namingScheme);
}
else
{
setRequestAttribute(getRequest(), CURRENT_CONVERSATION_ATTRIBUTE_NAME, getConversation(cid));
if (getCurrentConversation().lock(getConcurrentAccessTimeout()))
{
NamingScheme namingScheme = new ConversationNamingScheme(ConversationContext.class.getName(), cid);
setBeanStore(createRequestBeanStore(namingScheme, getRequest()));
getBeanStore().attach();
}
else
{
throw new BusyConversationException(ConversationMessage.CONVERSATION_LOCK_TIMEDOUT, cid);
}
}
}
else
{
throw new IllegalStateException("Context is already active");
}
}
@Override
public void invalidate()
{
for (ManagedConversation conversation : getConversations())
{
if (isExpired(conversation))
{
if (!conversation.isTransient())
{
conversation.end();
}
}
}
}
public boolean destroy(S session)
{
try
{
// We are outside of request, destroy now
if (getSessionAttributeFromSession(session, CONVERSATIONS_ATTRIBUTE_NAME) instanceof Map<?, ?>)
{
// There are no conversations to destroy
Map<String, ManagedConversation> conversations = cast(getSessionAttributeFromSession(session, CONVERSATIONS_ATTRIBUTE_NAME));
// Set the context active
setActive(true);
for (ManagedConversation conversation : conversations.values())
{
String id = conversation.getId();
conversation.end();
destroyConversation(session, id);
}
setActive(false);
}
return true;
}
finally
{
cleanup();
}
}
protected void destroyConversation(S session, String id)
{
if (session != null)
{
// session can be null as we may have nothing in the session
setBeanStore(createSessionBeanStore(new ConversationNamingScheme(ConversationContext.class.getName(), id), session));
getBeanStore().attach();
destroy();
getBeanStore().detach();
setBeanStore(null);
}
}
public String generateConversationId()
{
if (!isAssociated())
{
throw new IllegalStateException("A request must be associated with the context in order to generate a conversation id");
}
if (!(getRequestAttribute(getRequest(), CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME) instanceof ConversationIdGenerator))
{
throw new IllegalStateException("Unable to locate ConversationIdGenerator");
}
ConversationIdGenerator generator = (ConversationIdGenerator) getRequestAttribute(getRequest(), CONVERSATION_ID_GENERATOR_ATTRIBUTE_NAME);
return generator.call();
}
private static boolean isExpired(ManagedConversation conversation)
{
return System.currentTimeMillis() > (conversation.getLastUsed() + conversation.getTimeout());
}
public ManagedConversation getConversation(String id)
{
return getConversationMap().get(id);
}
public Collection<ManagedConversation> getConversations()
{
return getConversationMap().values();
}
private Map<String, ManagedConversation> getConversationMap()
{
if (!isAssociated())
{
throw new IllegalStateException("A request must be associated with the context in order to load the known conversations");
}
if (!(getRequestAttribute(getRequest(), CONVERSATIONS_ATTRIBUTE_NAME) instanceof Map<?, ?>))
{
throw new IllegalStateException("Unable to load current conversations from the associated request, something went badly wrong when associate() was called");
}
return cast(getRequestAttribute(getRequest(), CONVERSATIONS_ATTRIBUTE_NAME));
}
public ManagedConversation getCurrentConversation()
{
if (!isAssociated())
{
throw new IllegalStateException("A request must be associated with the context in order to load the known conversations");
}
if (!(getRequestAttribute(getRequest(), CURRENT_CONVERSATION_ATTRIBUTE_NAME) instanceof ManagedConversation))
{
throw new IllegalStateException("Unable to load current conversations from the associated request, something went badly wrong when associate() was called");
}
return (ManagedConversation) getRequestAttribute(getRequest(), CURRENT_CONVERSATION_ATTRIBUTE_NAME);
}
public Class<? extends Annotation> getScope()
{
return ConversationScoped.class;
}
/**
* Set an attribute in the session.
* @param request the request to set the session attribute in
* @param name the name of the attribute
* @param value the value of the attribute
* @param create if false, the attribute will only be set if the session
* already exists, other wise it will always be set
*
* @throws IllegalStateException if create is true, and the session can't be
* created
*/
protected abstract void setSessionAttribute(R request, String name, Object value, boolean create);
/**
* Get an attribute value from the session.
*
* @param request the request to get the session attribute from
* @param name the name of the attribute
* @param create if false, the attribute will only be retrieved if the
* session already exists, other wise it will always be retrieved
*
* @return attribute
* @throws IllegalStateException if create is true, and the session can't be
* created
*/
protected abstract Object getSessionAttribute(R request, String name, boolean create);
/**
* Get an attribute value from the session.
* @param session the session to get the session attribute from
* @param name the name of the attribute
*
*
* @return attribute
* @throws IllegalStateException if create is true, and the session can't be
* created
*/
protected abstract Object getSessionAttributeFromSession(S session, String name);
/**
* Remove an attribute from the request.
* @param request the request to remove the attribute from
* @param name the name of the attribute
*/
protected abstract void removeRequestAttribute(R request, String name);
/**
* Set an attribute in the request.
* @param request the request to set the attribute from
* @param name the name of the attribute
* @param value the value of the attribute
*/
protected abstract void setRequestAttribute(R request, String name, Object value);
/**
* Retrieve an attribute value from the request
* @param request the request to get the attribute from
* @param name the name of the attribute to get
*
* @return the value of the attribute
*/
protected abstract Object getRequestAttribute(R request, String name);
protected abstract BoundBeanStore createRequestBeanStore(NamingScheme namingScheme, R request);
protected abstract BoundBeanStore createSessionBeanStore(NamingScheme namingScheme, S session);
protected abstract S getSessionFromRequest(R request, boolean create);
/**
* Check if the context is currently associated
*
* @return true if the context is associated
*/
private boolean isAssociated()
{
return associated.get() != null;
}
/**
* Get the associated store
*
* @return the request
*/
private R getRequest()
{
return associated.get();
}
}