View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Leonardo Uribe
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          // Register FlowBuilderFactoryBean as a bean with CDI annotations, so the system
54          // can take it into account, and use it later when necessary.
55          AnnotatedType bean = beanManager.createAnnotatedType(FlowScopeBeanHolder.class);
56          event.addAnnotatedType(bean);
57      }
58      
59      void onProcessBean(@Observes ProcessBean event, BeanManager manager)
60      {
61          // Register all beans who are annotated with FlowScoped and has a flow reference
62          // restriction, to take it into account later when it is created and store it
63          // in the right context so @PreDestroy is called when the referenced flow is.
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          //FlowScopeBeanHolder flowScopeBeanHolder = BeanProvider.getContextualReference(
85          //    beanManager, FlowScopeBeanHolder.class, false);
86          
87          //flowScopedContext.initFlowContext(flowScopeBeanHolder);
88      }
89  }