1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.conversation;
20
21 import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
22
23 import java.util.HashSet;
24 import java.util.Set;
25
26 /***
27 * Manager to deal with page scoped beans.
28 * <p>
29 * Instances of this type are expected to be request-scoped, ie a new instance is used for
30 * each request. The FlashScopeManagerConfiguration object that it references can be
31 * of application scope.
32 */
33 public final class FlashScopeManager
34 {
35 private FlashScopeManagerConfiguration flashScopeManagerConfiguration;
36
37 private Set accessedConversations = new HashSet();
38 private boolean ignoreRequest;
39
40 public static FlashScopeManager getInstance()
41 {
42
43
44
45
46
47
48
49 FlashScopeManager manager = (FlashScopeManager) FrameworkAdapter.getCurrentInstance().getBean(FlashScopeManager.class.getName());
50 if (manager == null)
51 {
52
53
54 throw new IllegalArgumentException("no FlashScopeManager found. Propably you forgot to add <import resource=\"classpath*:/META-INF/spring-orchestra-init.xml\" /> to your spring configuration.");
55 }
56
57 return manager;
58 }
59
60 public FlashScopeManagerConfiguration getFlashScopeManagerConfiguration()
61 {
62 return flashScopeManagerConfiguration;
63 }
64
65 public void setFlashScopeManagerConfiguration(FlashScopeManagerConfiguration flashScopeManagerConfiguration)
66 {
67 this.flashScopeManagerConfiguration = flashScopeManagerConfiguration;
68 }
69
70 /***
71 * Add a conversation to the list of accessed conversations.
72 * <p>
73 * Notice: this method is final for performance reasons.
74 * <p>
75 * This method is expected to be called via AOP proxies wrapped around each conversation-scoped
76 * bean; any invocation of a method on such a bean causes the conversation associated with that
77 * bean to be added to the accessed list here.
78 */
79 public final void addConversationAccess(String conversationName)
80 {
81
82
83 if (!ignoreRequest && !accessedConversations.contains(conversationName))
84 {
85 accessedConversations.add(conversationName);
86 }
87 }
88
89 public boolean isIgnoreRequest()
90 {
91 return ignoreRequest;
92 }
93
94 /***
95 * Suppress flash scope for the current request, ie do not terminate conversations that are
96 * not accessed by this request.
97 * <p>
98 * This can come in useful occasionally, particularly when handling AJAX requests which
99 * only access some of the beans associated with the current view.
100 */
101 public void setIgnoreRequest()
102 {
103 this.ignoreRequest = true;
104 }
105
106 public boolean isConversationAccessed(String name)
107 {
108 if (ignoreRequest)
109 {
110 throw new IllegalStateException();
111 }
112
113 return accessedConversations.contains(name);
114 }
115 }