You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by wt...@apache.org on 2016/01/12 22:14:48 UTC

svn commit: r1724324 - in /myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi: DefaultCDIFacesFlowProvider.java FlowBuilderCDIExtension.java FlowBuilderFactoryBean.java FlowDefinitionQualifier.java

Author: wtlucy
Date: Tue Jan 12 21:14:48 2016
New Revision: 1724324

URL: http://svn.apache.org/viewvc?rev=1724324&view=rev
Log:
MYFACES-4022 Faces Flows are not discovered when the web application is packaged inside an EAR

Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/DefaultCDIFacesFlowProvider.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderCDIExtension.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderFactoryBean.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowDefinitionQualifier.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/DefaultCDIFacesFlowProvider.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/DefaultCDIFacesFlowProvider.java?rev=1724324&r1=1724323&r2=1724324&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/DefaultCDIFacesFlowProvider.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/DefaultCDIFacesFlowProvider.java Tue Jan 12 21:14:48 2016
@@ -19,10 +19,10 @@
 package org.apache.myfaces.flow.cdi;
 
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
-import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.faces.context.FacesContext;
 import javax.faces.flow.Flow;
@@ -51,9 +51,9 @@ public class DefaultCDIFacesFlowProvider
             FlowBuilderFactoryBean bean = CDIUtils.lookup(
                 beanManager, FlowBuilderFactoryBean.class);
 
-            Instance<Flow> instance = bean.getFlowDefinitions();
+            List<Flow> flows = bean.getFlowDefinitions();
             
-            return instance.iterator();
+            return flows.iterator();
         }
         else
         {

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderCDIExtension.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderCDIExtension.java?rev=1724324&r1=1724323&r2=1724324&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderCDIExtension.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderCDIExtension.java Tue Jan 12 21:14:48 2016
@@ -18,32 +18,56 @@
  */
 package org.apache.myfaces.flow.cdi;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.BeforeBeanDiscovery;
 import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessProducer;
+import javax.enterprise.inject.spi.Producer;
+import javax.faces.flow.Flow;
+import javax.faces.flow.builder.FlowDefinition;
 
 /**
  * This extension is responsible of scan flow definitions through CDI. For example:
- * 
+ *
  * <code>
  * @Produces @FlowDefinition
  * public Flow defineFlow(@FlowBuilderParameter FlowBuilder flowBuilder) {...}
  * </code>
- * 
+ *
  * @author Leonardo Uribe
  */
 public class FlowBuilderCDIExtension implements Extension
 {
-    
+    private List<Producer<Flow>> flowProducers = new ArrayList<Producer<Flow>>();
+
+    public List<Producer<Flow>> getFlowProducers()
+    {
+        return flowProducers;
+    }
+
     void beforeBeanDiscovery(
         @Observes final BeforeBeanDiscovery event, BeanManager beanManager)
     {
         // Register FlowBuilderFactoryBean as a bean with CDI annotations, so the system
         // can take it into account, and use it later when necessary.
-        AnnotatedType flowDiscoveryHelper = beanManager.createAnnotatedType(FlowBuilderFactoryBean.class);
+        AnnotatedType<FlowBuilderFactoryBean> flowDiscoveryHelper =
+                        beanManager.createAnnotatedType(FlowBuilderFactoryBean.class);
         event.addAnnotatedType(flowDiscoveryHelper);
     }
 
+    /**
+     * Stores any producer method that is annotated with @FlowDefinition.
+     */
+    <T> void findFlowDefinition(@Observes ProcessProducer<T, Flow> processProducer)
+    {
+        if (processProducer.getAnnotatedMember().isAnnotationPresent(FlowDefinition.class))
+        {
+            flowProducers.add(processProducer.getProducer());
+        }
+    }
 }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderFactoryBean.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderFactoryBean.java?rev=1724324&r1=1724323&r2=1724324&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderFactoryBean.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowBuilderFactoryBean.java Tue Jan 12 21:14:48 2016
@@ -18,16 +18,22 @@
  */
 package org.apache.myfaces.flow.cdi;
 
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
 import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.inject.Any;
-import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.Producer;
+import javax.faces.context.FacesContext;
 import javax.faces.flow.Flow;
 import javax.faces.flow.builder.FlowBuilder;
 import javax.faces.flow.builder.FlowBuilderParameter;
 import javax.inject.Inject;
 import javax.inject.Named;
 
+import org.apache.myfaces.cdi.util.CDIUtils;
 import org.apache.myfaces.flow.builder.FlowBuilderImpl;
 
 /**
@@ -39,40 +45,46 @@ import org.apache.myfaces.flow.builder.F
 @ApplicationScoped
 public class FlowBuilderFactoryBean
 {
-    public static final String FLOW_BUILDER_FACTORY_BEAN_NAME = 
+    public static final String FLOW_BUILDER_FACTORY_BEAN_NAME =
         "oam_FLOW_BUILDER_FACTORY_BEAN_NAME";
-    
-    /**
-     * In this point two things are important:
-     * 
-     * 1. Initialize flows in a lazy way (triggered by JSF),
-     * 2. Get multiple Flow instances.
-     * 
-     */
-    @Inject 
-    @Any
-    private Instance<Flow> flowDefinitions;
+
+    private List<Flow> flowDefinitions = null;
+
+    @Inject
+    private FlowBuilderCDIExtension flowBuilderExtension;
 
     public FlowBuilderFactoryBean()
     {
     }
-    
+
     @Produces
     @FlowBuilderParameter
     public FlowBuilder createFlowBuilderInstance()
     {
         return new FlowBuilderImpl();
     }
-    
+
     /**
      * @return the flowDefinitions
      */
-    public Instance<Flow> getFlowDefinitions()
+    public List<Flow> getFlowDefinitions()
     {
-        // Pass the @FlowDefinition qualifier, so only all producer methods involving
-        // this annotation will be taken into account.
-        return flowDefinitions.select(
-                new FlowDefinitionQualifier());
+        if (flowDefinitions == null)
+        {
+            flowDefinitions = new ArrayList<Flow>();
+            BeanManager beanManager = CDIUtils.getBeanManager(FacesContext.getCurrentInstance().getExternalContext());
+            Iterator<Producer<Flow>> it = flowBuilderExtension.getFlowProducers().iterator();
+
+            if (it != null)
+            {
+                while (it.hasNext())
+                {
+                    Flow flow = it.next().produce(beanManager.<Flow>createCreationalContext(null));
+                    flowDefinitions.add(flow);
+                }
+            }
+        }
+
+        return flowDefinitions;
     }
-    
 }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowDefinitionQualifier.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowDefinitionQualifier.java?rev=1724324&r1=1724323&r2=1724324&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowDefinitionQualifier.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/flow/cdi/FlowDefinitionQualifier.java Tue Jan 12 21:14:48 2016
@@ -1,38 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.myfaces.flow.cdi;
-
-import javax.enterprise.util.AnnotationLiteral;
-import javax.faces.flow.builder.FlowDefinition;
-
-/**
- * This class follows the hack proposed in section 4.10
- * "Obtaining a contextual instance by programmatic lookup", to specify
- * a qualifier dynamically. 
- * 
- * See:
- * http://docs.jboss.org/weld/reference/latest/en-US/html/injection.html
- *
- * @author Leonardo Uribe
- */
-public class FlowDefinitionQualifier extends AnnotationLiteral<FlowDefinition> 
-    implements FlowDefinition
-{
-    
-}