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.Map;
22 import java.util.concurrent.ConcurrentHashMap;
23 import javax.enterprise.event.Observes;
24 import javax.enterprise.inject.spi.AfterBeanDiscovery;
25 import javax.enterprise.inject.spi.AfterDeploymentValidation;
26 import javax.enterprise.inject.spi.AnnotatedType;
27 import javax.enterprise.inject.spi.BeanManager;
28 import javax.enterprise.inject.spi.BeforeBeanDiscovery;
29 import javax.enterprise.inject.spi.Extension;
30 import javax.enterprise.inject.spi.ProcessBean;
31 import javax.faces.flow.FlowScoped;
32 import org.apache.myfaces.flow.FlowReference;
33
34
35
36
37
38 public class FlowScopeCDIExtension implements Extension
39 {
40 private FlowScopedContextImpl flowScopedContext;
41
42 private Map<Class, FlowReference> flowBeanReferences;
43
44 public FlowScopeCDIExtension()
45 {
46 flowBeanReferences = new ConcurrentHashMap<Class, FlowReference>();
47 }
48
49 void beforeBeanDiscovery(
50 @Observes final BeforeBeanDiscovery event, BeanManager beanManager)
51 {
52 event.addScope(FlowScoped.class, true, true);
53
54
55 AnnotatedType bean = beanManager.createAnnotatedType(FlowScopeBeanHolder.class);
56 event.addAnnotatedType(bean);
57 }
58
59 void onProcessBean(@Observes ProcessBean event, BeanManager manager)
60 {
61
62
63
64 if (event.getAnnotated().isAnnotationPresent(FlowScoped.class))
65 {
66 FlowScoped flowScoped = event.getAnnotated().getAnnotation(FlowScoped.class);
67 String flowId = flowScoped.value();
68 if (flowId != null)
69 {
70 flowBeanReferences.put(event.getBean().getBeanClass(), new FlowReference(
71 flowScoped.definingDocumentId(), flowId));
72 }
73 }
74 }
75
76 void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager manager)
77 {
78 flowScopedContext = new FlowScopedContextImpl(manager, flowBeanReferences);
79 event.addContext(flowScopedContext);
80 }
81
82 void initializeFlowContexts(@Observes AfterDeploymentValidation adv, BeanManager beanManager)
83 {
84
85
86
87
88 }
89 }