1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.flow.cdi; |
20 | |
|
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
import java.util.logging.Level; |
25 | |
import java.util.logging.Logger; |
26 | |
import javax.enterprise.inject.spi.BeanManager; |
27 | |
import javax.faces.context.FacesContext; |
28 | |
import javax.faces.flow.Flow; |
29 | |
import org.apache.myfaces.cdi.util.CDIUtils; |
30 | |
import org.apache.myfaces.flow.util.FlowUtils; |
31 | |
import org.apache.myfaces.spi.FacesFlowProvider; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public class DefaultCDIFacesFlowProvider extends FacesFlowProvider |
38 | |
{ |
39 | |
private BeanManager _beanManager; |
40 | |
private boolean _initialized; |
41 | |
|
42 | |
private final static String CURRENT_FLOW_SCOPE_MAP = "oam.flow.SCOPE_MAP"; |
43 | |
|
44 | |
static final char SEPARATOR_CHAR = '.'; |
45 | |
|
46 | |
@Override |
47 | |
public Iterator<Flow> getAnnotatedFlows(FacesContext facesContext) |
48 | |
{ |
49 | 0 | BeanManager beanManager = getBeanManager(facesContext); |
50 | 0 | if (beanManager != null) |
51 | |
{ |
52 | 0 | FlowBuilderFactoryBean bean = CDIUtils.lookup( |
53 | |
beanManager, FlowBuilderFactoryBean.class); |
54 | |
|
55 | 0 | List<Flow> flows = bean.getFlowDefinitions(); |
56 | |
|
57 | 0 | return flows.iterator(); |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 0 | Logger.getLogger(DefaultCDIFacesFlowProvider.class.getName()).log(Level.INFO, |
62 | |
"CDI BeanManager not found"); |
63 | |
} |
64 | 0 | return null; |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
public void doAfterEnterFlow(FacesContext context, Flow flow) |
69 | |
{ |
70 | 0 | BeanManager beanManager = getBeanManager(context); |
71 | 0 | if (beanManager != null) |
72 | |
{ |
73 | 0 | FlowScopeBeanHolder beanHolder = CDIUtils.lookup(beanManager, FlowScopeBeanHolder.class); |
74 | 0 | beanHolder.createCurrentFlowScope(context); |
75 | |
} |
76 | 0 | String mapKey = CURRENT_FLOW_SCOPE_MAP+SEPARATOR_CHAR+ |
77 | |
flow.getDefiningDocumentId()+SEPARATOR_CHAR+flow.getId(); |
78 | 0 | context.getAttributes().remove(mapKey); |
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public void doBeforeExitFlow(FacesContext context, Flow flow) |
83 | |
{ |
84 | 0 | BeanManager beanManager = getBeanManager(context); |
85 | 0 | if (beanManager != null) |
86 | |
{ |
87 | 0 | FlowScopeBeanHolder beanHolder = CDIUtils.lookup(beanManager, FlowScopeBeanHolder.class); |
88 | 0 | beanHolder.destroyCurrentFlowScope(context); |
89 | |
} |
90 | 0 | String mapKey = CURRENT_FLOW_SCOPE_MAP+SEPARATOR_CHAR+ |
91 | |
flow.getDefiningDocumentId()+SEPARATOR_CHAR+flow.getId(); |
92 | 0 | context.getAttributes().remove(mapKey); |
93 | 0 | } |
94 | |
|
95 | |
public Map<Object, Object> getCurrentFlowScope(FacesContext facesContext) |
96 | |
{ |
97 | 0 | Flow flow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext); |
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<Object, Object> map = (Map<Object, Object>) facesContext.getAttributes().get( |
103 | |
mapKey); |
104 | 0 | if (map == null) |
105 | |
{ |
106 | 0 | map = new FlowScopeMap(getBeanManager(), FlowUtils.getFlowMapKey(facesContext, flow)); |
107 | |
|
108 | 0 | facesContext.getAttributes().put(mapKey, map); |
109 | |
} |
110 | 0 | return map; |
111 | |
} |
112 | 0 | return null; |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public void refreshClientWindow(FacesContext facesContext) |
117 | |
{ |
118 | 0 | if (!facesContext.getApplication().getStateManager().isSavingStateInClient(facesContext)) |
119 | |
{ |
120 | 0 | Flow flow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext); |
121 | 0 | if (flow != null) |
122 | |
{ |
123 | 0 | BeanManager beanManager = getBeanManager(facesContext); |
124 | 0 | if (beanManager != null) |
125 | |
{ |
126 | 0 | FlowScopeBeanHolder beanHolder = CDIUtils.lookup(beanManager, FlowScopeBeanHolder.class); |
127 | |
|
128 | |
|
129 | 0 | beanHolder.refreshClientWindow(facesContext); |
130 | |
} |
131 | |
} |
132 | |
} |
133 | 0 | } |
134 | |
|
135 | |
public BeanManager getBeanManager() |
136 | |
{ |
137 | 0 | if (_beanManager == null && !_initialized) |
138 | |
{ |
139 | 0 | _beanManager = CDIUtils.getBeanManager( |
140 | |
FacesContext.getCurrentInstance().getExternalContext()); |
141 | 0 | _initialized = true; |
142 | |
} |
143 | 0 | return _beanManager; |
144 | |
} |
145 | |
|
146 | |
public BeanManager getBeanManager(FacesContext facesContext) |
147 | |
{ |
148 | 0 | if (_beanManager == null && !_initialized) |
149 | |
{ |
150 | 0 | _beanManager = CDIUtils.getBeanManager( |
151 | |
facesContext.getExternalContext()); |
152 | 0 | _initialized = true; |
153 | |
} |
154 | 0 | return _beanManager; |
155 | |
} |
156 | |
|
157 | |
} |