1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.flow.impl; |
20 | |
|
21 | |
import java.util.Iterator; |
22 | |
import java.util.Map; |
23 | |
import java.util.concurrent.ConcurrentHashMap; |
24 | |
import javax.faces.context.ExternalContext; |
25 | |
import javax.faces.context.FacesContext; |
26 | |
import javax.faces.flow.Flow; |
27 | |
import org.apache.myfaces.flow.util.FlowUtils; |
28 | |
import org.apache.myfaces.spi.FacesFlowProvider; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class DefaultFacesFlowProvider extends FacesFlowProvider |
35 | |
{ |
36 | |
private static final String FLOW_PREFIX = "oam.flow"; |
37 | |
|
38 | |
static final String FLOW_SESSION_MAP_SUBKEY_PREFIX = FLOW_PREFIX + ".SCOPE"; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
static final char SEPARATOR_CHAR = '.'; |
44 | |
|
45 | |
private final static String CURRENT_FLOW_SCOPE_MAP = "oam.flow.SCOPE_MAP"; |
46 | |
|
47 | |
@Override |
48 | |
public Iterator<Flow> getAnnotatedFlows(FacesContext facesContext) |
49 | |
{ |
50 | |
|
51 | 0 | return null; |
52 | |
} |
53 | |
|
54 | |
@Override |
55 | |
public void doAfterEnterFlow(FacesContext facesContext, Flow flow) |
56 | |
{ |
57 | |
|
58 | 0 | String mapKey = CURRENT_FLOW_SCOPE_MAP+SEPARATOR_CHAR+ |
59 | |
flow.getDefiningDocumentId()+SEPARATOR_CHAR+flow.getId(); |
60 | 0 | facesContext.getAttributes().remove(mapKey); |
61 | 0 | } |
62 | |
|
63 | |
@Override |
64 | |
public void doBeforeExitFlow(FacesContext facesContext, Flow flow) |
65 | |
{ |
66 | 0 | String flowMapKey = FlowUtils.getFlowMapKey(facesContext, flow); |
67 | 0 | String fullToken = FLOW_SESSION_MAP_SUBKEY_PREFIX + SEPARATOR_CHAR + flowMapKey; |
68 | |
|
69 | 0 | String mapKey = CURRENT_FLOW_SCOPE_MAP+SEPARATOR_CHAR+ |
70 | |
flow.getDefiningDocumentId()+SEPARATOR_CHAR+flow.getId(); |
71 | 0 | Map<Object, Object> map = (Map<Object, Object>) facesContext.getAttributes().get( |
72 | |
mapKey); |
73 | 0 | if (map != null) |
74 | |
{ |
75 | 0 | map.clear(); |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 0 | map = (Map<Object, Object>) facesContext.getExternalContext(). |
80 | |
getSessionMap().get(fullToken); |
81 | 0 | if (map != null) |
82 | |
{ |
83 | 0 | map.clear(); |
84 | |
} |
85 | |
} |
86 | |
|
87 | 0 | facesContext.getExternalContext().getSessionMap().remove(fullToken); |
88 | |
|
89 | |
|
90 | 0 | facesContext.getAttributes().remove(mapKey); |
91 | 0 | } |
92 | |
|
93 | |
@Override |
94 | |
public Map<Object, Object> getCurrentFlowScope(FacesContext facesContext) |
95 | |
{ |
96 | 0 | Flow flow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext); |
97 | 0 | Map<Object, Object> map = null; |
98 | 0 | if (flow != null) |
99 | |
{ |
100 | 0 | String mapKey = CURRENT_FLOW_SCOPE_MAP+SEPARATOR_CHAR+ |
101 | |
flow.getDefiningDocumentId()+SEPARATOR_CHAR+flow.getId(); |
102 | 0 | map = (Map<Object, Object>) facesContext.getAttributes().get(mapKey); |
103 | 0 | if (map == null) |
104 | |
{ |
105 | 0 | String flowMapKey = FlowUtils.getFlowMapKey(facesContext, flow); |
106 | |
|
107 | |
|
108 | |
|
109 | 0 | map = new FlowScopeMap(this, flowMapKey); |
110 | |
|
111 | 0 | facesContext.getAttributes().put(mapKey, map); |
112 | |
} |
113 | |
} |
114 | 0 | return map; |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
Map<Object, Object> createOrRestoreMap(FacesContext context, String prefix, |
123 | |
boolean create) |
124 | |
{ |
125 | 0 | ExternalContext external = context.getExternalContext(); |
126 | 0 | Map<String, Object> sessionMap = external.getSessionMap(); |
127 | |
|
128 | 0 | Map<Object, Object> map = (Map<Object, Object>) sessionMap.get(prefix); |
129 | 0 | if (map == null && create) |
130 | |
{ |
131 | 0 | map = new ConcurrentHashMap<Object, Object>(); |
132 | 0 | sessionMap.put(prefix, map); |
133 | |
} |
134 | 0 | return map; |
135 | |
} |
136 | |
} |