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.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   * @author Leonardo Uribe
36   */
37  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          BeanManager beanManager = getBeanManager(facesContext);
50          if (beanManager != null)
51          {
52              FlowBuilderFactoryBean bean = CDIUtils.lookup(
53                  beanManager, FlowBuilderFactoryBean.class);
54  
55              List<Flow> flows = bean.getFlowDefinitions();
56              
57              return flows.iterator();
58          }
59          else
60          {
61              Logger.getLogger(DefaultCDIFacesFlowProvider.class.getName()).log(Level.INFO,
62                  "CDI BeanManager not found");
63          }
64          return null;
65      }
66      
67      @Override
68      public void doAfterEnterFlow(FacesContext context, Flow flow)
69      {
70          BeanManager beanManager = getBeanManager(context);
71          if (beanManager != null)
72          {
73              FlowScopeBeanHolder beanHolder = CDIUtils.lookup(beanManager, FlowScopeBeanHolder.class);
74              beanHolder.createCurrentFlowScope(context);
75          }
76          String mapKey = CURRENT_FLOW_SCOPE_MAP+SEPARATOR_CHAR+
77                  flow.getDefiningDocumentId()+SEPARATOR_CHAR+flow.getId();
78          context.getAttributes().remove(mapKey);
79      }
80      
81      @Override
82      public void doBeforeExitFlow(FacesContext context, Flow flow)
83      {
84          BeanManager beanManager = getBeanManager(context);
85          if (beanManager != null)
86          {
87              FlowScopeBeanHolder beanHolder = CDIUtils.lookup(beanManager, FlowScopeBeanHolder.class);
88              beanHolder.destroyCurrentFlowScope(context);
89          }
90          String mapKey = CURRENT_FLOW_SCOPE_MAP+SEPARATOR_CHAR+
91                  flow.getDefiningDocumentId()+SEPARATOR_CHAR+flow.getId();
92          context.getAttributes().remove(mapKey);
93      }
94      
95      public Map<Object, Object> getCurrentFlowScope(FacesContext facesContext)
96      {
97          Flow flow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
98          if (flow != null)
99          {
100             String mapKey = CURRENT_FLOW_SCOPE_MAP+SEPARATOR_CHAR+
101                 flow.getDefiningDocumentId()+SEPARATOR_CHAR+flow.getId();
102             Map<Object, Object> map = (Map<Object, Object>) facesContext.getAttributes().get(
103                 mapKey);
104             if (map == null)
105             {
106                 map = new FlowScopeMap(getBeanManager(), FlowUtils.getFlowMapKey(facesContext, flow));
107                 
108                 facesContext.getAttributes().put(mapKey, map);
109             }
110             return map;
111         }
112         return null;
113     }
114 
115     @Override
116     public void refreshClientWindow(FacesContext facesContext)
117     {
118         if (!facesContext.getApplication().getStateManager().isSavingStateInClient(facesContext))
119         {
120             Flow flow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
121             if (flow != null)
122             {
123                 BeanManager beanManager = getBeanManager(facesContext);
124                 if (beanManager != null)
125                 {
126                     FlowScopeBeanHolder beanHolder = CDIUtils.lookup(beanManager, FlowScopeBeanHolder.class);
127 
128                     //Refresh client window for flow scope
129                     beanHolder.refreshClientWindow(facesContext);
130                 }
131             }
132         }
133     }
134     
135     public BeanManager getBeanManager()
136     {
137         if (_beanManager == null && !_initialized)
138         {
139             _beanManager = CDIUtils.getBeanManager(
140                 FacesContext.getCurrentInstance().getExternalContext());
141             _initialized = true;
142         }
143         return _beanManager;
144     }
145     
146     public BeanManager getBeanManager(FacesContext facesContext)
147     {
148         if (_beanManager == null && !_initialized)
149         {
150             _beanManager = CDIUtils.getBeanManager(
151                 facesContext.getExternalContext());
152             _initialized = true;
153         }
154         return _beanManager;
155     }
156 
157 }