1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.conversation;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.context.FacesContext;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29
30
31
32
33 public class Conversation
34 {
35 private final static Log log = LogFactory.getLog(Conversation.class);
36
37 private final String name;
38 private final boolean persistence;
39
40 private PersistenceManager persistenceManager;
41
42
43 private final Map beans = new TreeMap();
44
45 protected Conversation(String name, boolean persistence)
46 {
47 this.name = name;
48 this.persistence = persistence;
49
50 if (log.isDebugEnabled())
51 {
52 log.debug("start conversation:" + name + "(persistence=" + persistence + ")");
53 }
54 }
55
56
57
58
59
60 public void putBean(FacesContext context, String name, Object value)
61 {
62 if (name.indexOf('.') > -1)
63 {
64 throw new IllegalArgumentException("you cant put a property under conversation control. name: " + name);
65 }
66
67 if (beans.containsKey(name))
68 {
69
70 return;
71 }
72 if (log.isDebugEnabled())
73 {
74 log.debug("put bean to conversation:" + name + "(bean=" + name + ")");
75 }
76 beans.put(name, value);
77 }
78
79
80
81
82 public String getName()
83 {
84 return name;
85 }
86
87
88
89
90
91
92
93
94 public void endConversation(boolean regularEnd)
95 {
96 if (log.isDebugEnabled())
97 {
98 log.debug("end conversation:" + name);
99 }
100
101 Iterator iterBeans = beans.values().iterator();
102 while (iterBeans.hasNext())
103 {
104 Object bean = iterBeans.next();
105 if (bean instanceof ConversationListener)
106 {
107 ((ConversationListener) bean).conversationEnded();
108 }
109 }
110 beans.clear();
111
112 if (isPersistence())
113 {
114 if (regularEnd)
115 {
116 getPersistenceManager().commit();
117 }
118 else
119 {
120 getPersistenceManager().rollback();
121 }
122
123 getPersistenceManager().purge();
124 }
125 }
126
127
128
129
130
131
132
133
134
135
136
137 public boolean hasBean(String name)
138 {
139 return beans.containsKey(name);
140 }
141
142 public Object getBean(String name)
143 {
144 return beans.get(name);
145 }
146
147 public Object removeBean(String name)
148 {
149 return beans.remove(name);
150 }
151
152
153
154
155 public boolean isPersistence()
156 {
157 return persistence || persistenceManager != null;
158 }
159
160 public PersistenceManager getPersistenceManager()
161 {
162 if (persistenceManager == null)
163 {
164 persistenceManager = ConversationManager.getInstance().createPersistenceManager();
165 }
166
167 return persistenceManager;
168 }
169
170
171
172
173 public boolean hasBean(Object instance)
174 {
175 return beans.containsValue(instance);
176 }
177 }