myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject svn commit: r987218 - in /myfaces/extensions/cdi/trunk: core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/ jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/ jee-modules/jsf...
Date Thu, 19 Aug 2010 16:18:30 GMT
Author: gpetracek
Date: Thu Aug 19 16:18:30 2010
New Revision: 987218

URL: http://svn.apache.org/viewvc?rev=987218&view=rev
Log:
EXTCDI-46 initial implementation + cleanup

Added:
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PageBeanDefinitionEntry.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntry.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntryHelper.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackPhaseListener.java
Modified:
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/AfterPhase.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/BeforePhase.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ExceptionUtils.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PreRenderViewBeanLoader.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/ViewDefinitionEntry.java

Modified: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.java?rev=987218&r1=987217&r2=987218&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.java
(original)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/utils/CodiUtils.java
Thu Aug 19 16:18:30 2010
@@ -57,7 +57,7 @@ public class CodiUtils
 
         if(foundBeans.size() != 1)
         {
-            throw new IllegalStateException(foundBeans.size() + " beans found for type: "
+ targetClass.getName());
+            throw new IllegalStateException(foundBeans.size() + " beans found with name:
" + beanName);
         }
 
         //noinspection unchecked

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/AfterPhase.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/AfterPhase.java?rev=987218&r1=987217&r2=987218&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/AfterPhase.java
(original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/AfterPhase.java
Thu Aug 19 16:18:30 2010
@@ -22,6 +22,7 @@ import javax.inject.Qualifier;
 import java.lang.annotation.Documented;
 import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
 import java.lang.annotation.Retention;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import java.lang.annotation.Target;
@@ -30,11 +31,16 @@ import java.lang.annotation.Target;
  * @author Gerhard Petracek
  */
 
-@Target({PARAMETER, FIELD})
+@Target({PARAMETER, FIELD, METHOD})
 @Retention(RUNTIME)
 @Documented
 
 @Qualifier
+
+/**
+ * - for phase-observer-methods
+ * - for lifecycle callbacks in view-definitions
+ */
 public @interface AfterPhase
 {
     PhaseId value();

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/BeforePhase.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/BeforePhase.java?rev=987218&r1=987217&r2=987218&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/BeforePhase.java
(original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/api/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/api/listener/phase/BeforePhase.java
Thu Aug 19 16:18:30 2010
@@ -22,6 +22,7 @@ import javax.inject.Qualifier;
 import java.lang.annotation.Documented;
 import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
 import java.lang.annotation.Retention;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import java.lang.annotation.Target;
@@ -30,11 +31,16 @@ import java.lang.annotation.Target;
  * @author Gerhard Petracek
  */
 
-@Target({PARAMETER, FIELD})
+@Target({PARAMETER, FIELD, METHOD})
 @Retention(RUNTIME)
 @Documented
 
 @Qualifier
+
+/**
+ * - for phase-observer-methods
+ * - for lifecycle callbacks in view-definitions
+ */
 public @interface BeforePhase
 {
     PhaseId value();

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java?rev=987218&r1=987217&r2=987218&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java
(original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java
Thu Aug 19 16:18:30 2010
@@ -49,6 +49,7 @@ public class PhaseListenerExtension impl
     {
         if (processAnnotatedType.getAnnotatedType().isAnnotationPresent(JsfPhaseListener.class))
         {
+            //TODO check config if phase-listener is deactivated
             addPhaseListener(processAnnotatedType);
 
             processAnnotatedType.veto();

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ExceptionUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ExceptionUtils.java?rev=987218&r1=987217&r2=987218&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ExceptionUtils.java
(original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/util/ExceptionUtils.java
Thu Aug 19 16:18:30 2010
@@ -24,10 +24,16 @@ import org.apache.myfaces.extensions.cdi
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.scope.conversation.spi.EditableWindowContext;
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.scope.conversation.spi.EditableWindowContextManager;
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.scope.conversation.spi.EditableConversation;
+import org.apache.myfaces.extensions.cdi.javaee.jsf.api.listener.phase.BeforePhase;
+import org.apache.myfaces.extensions.cdi.javaee.jsf.api.listener.phase.AfterPhase;
+
+import javax.faces.event.PhaseId;
+import java.lang.reflect.Method;
 
 /**
  * @author Gerhard Petracek
  */
+//TODO create CODI exceptions
 public class ExceptionUtils
 {
     public static RuntimeException tooManyOpenWindowException(int windowContextTimeoutInMinutes)
@@ -59,4 +65,22 @@ public class ExceptionUtils
     {
         return new RuntimeException("View-ID: " + viewId + " doesn't exist.");
     }
+
+    public static IllegalArgumentException invalidPhasesCallbackMethod(Class targetClass,
Method method)
+    {
+        return new IllegalArgumentException(targetClass.getName() + "#" + method.getName()
+ " is annotated with " +
+                BeforePhase.class.getName() + " or " + AfterPhase.class.getName() +
+                " and the method signature isn't supported. " +
+                "Supported arguments: no-args or one parameter of type: " +
+                PhaseId.class.getName());
+    }
+
+    public static IllegalStateException unsupportedPhasesLifecycleCallback()
+    {
+        return new IllegalStateException("The usage of @ + " + BeforePhase.class.getName()
+
+                "(PhaseId.RESTORE_VIEW) as well as @" + BeforePhase.class.getName() + "(PhaseId.ANY_PHASE)
"+
+                "is not supported as request-lifecycle-callback. " +
+                "If you really need it, use an phases-observer-method e.g.: " +
+                "protected void preRestoreView(@Observes @BeforePhase(PhaseId.RESTORE_VIEW)
PhaseEvent event) ");
+    }
 }

Added: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PageBeanDefinitionEntry.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PageBeanDefinitionEntry.java?rev=987218&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PageBeanDefinitionEntry.java
(added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PageBeanDefinitionEntry.java
Thu Aug 19 16:18:30 2010
@@ -0,0 +1,138 @@
+/*
+ * 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.extensions.cdi.javaee.jsf.impl.view;
+
+import org.apache.myfaces.extensions.cdi.javaee.jsf.api.listener.phase.BeforePhase;
+import org.apache.myfaces.extensions.cdi.javaee.jsf.api.listener.phase.AfterPhase;
+import static org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ExceptionUtils.unsupportedPhasesLifecycleCallback;
+
+import javax.faces.event.PhaseId;
+import static javax.faces.event.PhaseId.*;
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author Gerhard Petracek
+ */
+class PageBeanDefinitionEntry
+{
+    private final String beanName;
+
+    private final Class beanClass;
+
+    private final Map<PhaseId, PhasesLifecycleCallbackEntry> phasesLifecycleCallbacks;
+
+    PageBeanDefinitionEntry(String beanName, Class beanClass)
+    {
+        this.beanName = beanName;
+        this.beanClass = beanClass;
+        this.phasesLifecycleCallbacks = Collections.unmodifiableMap(findCallbackDefinitions(beanClass));
+    }
+
+    String getBeanName()
+    {
+        return beanName;
+    }
+
+    Class getBeanClass()
+    {
+        return beanClass;
+    }
+
+    PhasesLifecycleCallbackEntry getPhasesLifecycleCallback(PhaseId phaseId)
+    {
+        return phasesLifecycleCallbacks.get(phaseId);
+    }
+
+    private Map<PhaseId, PhasesLifecycleCallbackEntry> findCallbackDefinitions(Class
beanClass)
+    {
+        Class currentClass = beanClass;
+
+        PhasesLifecycleCallbackEntryHelper beforeCallbackEntryHelper = new PhasesLifecycleCallbackEntryHelper();
+        PhasesLifecycleCallbackEntryHelper afterCallbackEntryHelper = new PhasesLifecycleCallbackEntryHelper();
+
+        while(!(currentClass.getName().equals(Object.class.getName())))
+        {
+            for(Method currentMethod : currentClass.getDeclaredMethods())
+            {
+                if(currentMethod.isAnnotationPresent(BeforePhase.class))
+                {
+                    beforeCallbackEntryHelper.add(
+                            currentMethod.getAnnotation(BeforePhase.class).value(), currentMethod);
+                }
+                else if(currentMethod.isAnnotationPresent(AfterPhase.class))
+                {
+                    afterCallbackEntryHelper.add(
+                            currentMethod.getAnnotation(AfterPhase.class).value(), currentMethod);
+                }
+            }
+
+            currentClass = currentClass.getSuperclass();
+        }
+
+        return createPhasesLifecycleCallbackMap(beforeCallbackEntryHelper, afterCallbackEntryHelper);
+    }
+
+    private Map<PhaseId, PhasesLifecycleCallbackEntry> createPhasesLifecycleCallbackMap(
+            PhasesLifecycleCallbackEntryHelper beforeCallbackEntryHelper,
+            PhasesLifecycleCallbackEntryHelper afterCallbackEntryHelper)
+    {
+        Map<PhaseId, PhasesLifecycleCallbackEntry> result = new HashMap<PhaseId,
PhasesLifecycleCallbackEntry>(6);
+
+        result.put(RESTORE_VIEW,
+                createCallbackEntry(RESTORE_VIEW, beforeCallbackEntryHelper, afterCallbackEntryHelper));
+
+        result.put(APPLY_REQUEST_VALUES,
+                createCallbackEntry(APPLY_REQUEST_VALUES, beforeCallbackEntryHelper, afterCallbackEntryHelper));
+
+        result.put(PROCESS_VALIDATIONS,
+                createCallbackEntry(PROCESS_VALIDATIONS, beforeCallbackEntryHelper, afterCallbackEntryHelper));
+
+        result.put(UPDATE_MODEL_VALUES,
+                createCallbackEntry(UPDATE_MODEL_VALUES, beforeCallbackEntryHelper, afterCallbackEntryHelper));
+
+        result.put(INVOKE_APPLICATION,
+                createCallbackEntry(INVOKE_APPLICATION, beforeCallbackEntryHelper, afterCallbackEntryHelper));
+
+        result.put(RENDER_RESPONSE,
+                createCallbackEntry(RENDER_RESPONSE, beforeCallbackEntryHelper, afterCallbackEntryHelper));
+
+        return result;
+    }
+
+    private PhasesLifecycleCallbackEntry createCallbackEntry(
+            PhaseId phaseId,
+            PhasesLifecycleCallbackEntryHelper beforeCallbackEntryHelper,
+            PhasesLifecycleCallbackEntryHelper afterCallbackEntryHelper)
+    {
+        List<Method> beforePhaseCallbacks = beforeCallbackEntryHelper.getMethodsFor(phaseId);
+        List<Method> afterPhaseCallbacks = afterCallbackEntryHelper.getMethodsFor(phaseId);
+
+        if(ANY_PHASE.equals(phaseId) ||
+                (RESTORE_VIEW.equals(phaseId) && beforePhaseCallbacks != null &&
!beforePhaseCallbacks.isEmpty()))
+        {
+            throw unsupportedPhasesLifecycleCallback();
+        }
+
+        return new PhasesLifecycleCallbackEntry(beforePhaseCallbacks, afterPhaseCallbacks);
+    }
+}

Added: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntry.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntry.java?rev=987218&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntry.java
(added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntry.java
Thu Aug 19 16:18:30 2010
@@ -0,0 +1,64 @@
+/*
+ * 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.extensions.cdi.javaee.jsf.impl.view;
+
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Collections;
+
+/**
+ * @author Gerhard Petracek
+ */
+class PhasesLifecycleCallbackEntry
+{
+    private final List<Method> beforePhaseCallbacks;
+
+    private final List<Method> afterPhaseCallbacks;
+
+    PhasesLifecycleCallbackEntry(List<Method> beforePhaseCallbacks, List<Method>
afterPhaseCallbacks)
+    {
+        if(beforePhaseCallbacks != null)
+        {
+            this.beforePhaseCallbacks = Collections.unmodifiableList(beforePhaseCallbacks);
+        }
+        else
+        {
+            this.beforePhaseCallbacks = Collections.emptyList();
+        }
+
+        if(afterPhaseCallbacks != null)
+        {
+            this.afterPhaseCallbacks = Collections.unmodifiableList(afterPhaseCallbacks);
+        }
+        else
+        {
+            this.afterPhaseCallbacks = Collections.emptyList();
+        }
+    }
+
+    List<Method> getBeforePhaseCallbacks()
+    {
+        return beforePhaseCallbacks;
+    }
+
+    List<Method> getAfterPhaseCallbacks()
+    {
+        return afterPhaseCallbacks;
+    }
+}

Added: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntryHelper.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntryHelper.java?rev=987218&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntryHelper.java
(added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackEntryHelper.java
Thu Aug 19 16:18:30 2010
@@ -0,0 +1,60 @@
+/*
+ * 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.extensions.cdi.javaee.jsf.impl.view;
+
+import org.apache.myfaces.extensions.cdi.javaee.jsf.api.listener.phase.PhaseId;
+import static org.apache.myfaces.extensions.cdi.javaee.jsf.api.listener.phase.PhaseId.convertToFacesClass;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * @author Gerhard Petracek
+ */
+class PhasesLifecycleCallbackEntryHelper
+{
+    private Map<javax.faces.event.PhaseId, List<Method>> methods
+            = new HashMap<javax.faces.event.PhaseId, List<Method>>();
+
+    void add(PhaseId phaseId, Method method)
+    {
+        javax.faces.event.PhaseId id = convertToFacesClass(phaseId);
+        List<Method> methodList = this.methods.get(id);
+
+        if(methodList == null)
+        {
+            methodList = new ArrayList<Method>();
+            methods.put(id, methodList);
+        }
+        methodList.add(method);
+    }
+
+    boolean isEmpty()
+    {
+        return methods.isEmpty();
+    }
+
+    List<Method> getMethodsFor(javax.faces.event.PhaseId phaseId)
+    {
+        return methods.get(phaseId);
+    }
+}

Added: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackPhaseListener.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackPhaseListener.java?rev=987218&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackPhaseListener.java
(added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PhasesLifecycleCallbackPhaseListener.java
Thu Aug 19 16:18:30 2010
@@ -0,0 +1,154 @@
+/*
+ * 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.extensions.cdi.javaee.jsf.impl.view;
+
+import org.apache.myfaces.extensions.cdi.core.impl.utils.CodiUtils;
+import static org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.ExceptionUtils.invalidPhasesCallbackMethod;
+import org.apache.myfaces.extensions.cdi.javaee.jsf.api.listener.phase.JsfPhaseListener;
+
+import javax.faces.event.PhaseEvent;
+import javax.faces.event.PhaseListener;
+import javax.faces.component.UIViewRoot;
+import java.util.List;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * @author Gerhard Petracek
+ */
+@JsfPhaseListener
+public final class PhasesLifecycleCallbackPhaseListener implements PhaseListener
+{
+    private static final long serialVersionUID = 6893021853444122202L;
+
+    public void afterPhase(PhaseEvent event)
+    {
+        try
+        {
+            processPhaseCallbacks(event, false);
+        }
+        catch (Exception e)
+        {
+            if(!(e instanceof RuntimeException))
+            {
+                throw new IllegalStateException(e);
+            }
+            throw (RuntimeException)e;
+        }
+    }
+
+    public void beforePhase(PhaseEvent event)
+    {
+        try
+        {
+            processPhaseCallbacks(event, true);
+        }
+        catch (Exception e)
+        {
+            if(!(e instanceof RuntimeException))
+            {
+                throw new IllegalStateException(e);
+            }
+            throw (RuntimeException)e;
+        }
+    }
+
+    public javax.faces.event.PhaseId getPhaseId()
+    {
+        return javax.faces.event.PhaseId.ANY_PHASE;
+    }
+
+    private void processPhaseCallbacks(PhaseEvent phaseEvent, boolean beforePhase) throws
Exception
+    {
+        UIViewRoot viewRoot = phaseEvent.getFacesContext().getViewRoot();
+
+        if(viewRoot == null)
+        {
+            return;
+        }
+
+        String viewId = viewRoot.getViewId();
+
+        ViewDefinitionEntry viewDefinitionEntry = ViewDefinitionCache.getViewDefinition(viewId);
+
+        if(viewDefinitionEntry == null)
+        {
+            return;
+        }
+
+        List<PageBeanDefinitionEntry> beanEntries = viewDefinitionEntry.getBeanDefinitions();
+
+        Object bean;
+        PhasesLifecycleCallbackEntry phasesLifecycleCallbackEntry;
+        List<Method> lifecycleCallbacks;
+
+        for(PageBeanDefinitionEntry beanEntry : beanEntries)
+        {
+            phasesLifecycleCallbackEntry = beanEntry.getPhasesLifecycleCallback(phaseEvent.getPhaseId());
+
+            if(phasesLifecycleCallbackEntry == null)
+            {
+                continue;
+            }
+
+
+            if(beforePhase)
+            {
+                lifecycleCallbacks = phasesLifecycleCallbackEntry.getBeforePhaseCallbacks();
+            }
+            else
+            {
+                lifecycleCallbacks = phasesLifecycleCallbackEntry.getAfterPhaseCallbacks();
+            }
+
+            if(lifecycleCallbacks.isEmpty())
+            {
+                continue;
+            }
+
+            //TODO provide a detailed error message in case of a missing bean
+            bean = CodiUtils.getOrCreateScopedInstanceOfBeanByName(beanEntry.getBeanName(),
Object.class);
+            invokePhasesLifecycleCallbacks(bean, lifecycleCallbacks, phaseEvent);
+        }
+    }
+
+    private void invokePhasesLifecycleCallbacks(Object bean, List<Method> lifecycleCallbacks,
PhaseEvent phaseEvent)
+            throws InvocationTargetException, IllegalAccessException
+    {
+        Class<?>[] parameterTypes;
+        for(Method currentMethod : lifecycleCallbacks)
+        {
+            currentMethod.setAccessible(true);
+
+            parameterTypes = currentMethod.getParameterTypes();
+            if(parameterTypes.length == 0)
+            {
+                currentMethod.invoke(bean);
+            }
+            else if(parameterTypes.length == 1 && PhaseEvent.class.isAssignableFrom(parameterTypes[0]))
+            {
+                currentMethod.invoke(bean, phaseEvent);
+            }
+            else
+            {
+                throw invalidPhasesCallbackMethod(bean.getClass(), currentMethod);
+            }
+        }
+    }
+}

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PreRenderViewBeanLoader.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PreRenderViewBeanLoader.java?rev=987218&r1=987217&r2=987218&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PreRenderViewBeanLoader.java
(original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/PreRenderViewBeanLoader.java
Thu Aug 19 16:18:30 2010
@@ -42,12 +42,12 @@ final class PreRenderViewBeanLoader
             return;
         }
 
-        List<String> beanNames = viewDefinitionEntry.getBeanNames();
+        List<PageBeanDefinitionEntry> beanEntries = viewDefinitionEntry.getBeanDefinitions();
 
-        for(String beanName : beanNames)
+        for(PageBeanDefinitionEntry beanEntry : beanEntries)
         {
             //resolve bean to trigger @PostConstruct if it isn't scoped
-            CodiUtils.getOrCreateScopedInstanceOfBeanByName(beanName, Object.class);
+            CodiUtils.getOrCreateScopedInstanceOfBeanByName(beanEntry.getBeanName(), Object.class);
         }
     }
 }

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/ViewDefinitionEntry.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/ViewDefinitionEntry.java?rev=987218&r1=987217&r2=987218&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/ViewDefinitionEntry.java
(original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/view/ViewDefinitionEntry.java
Thu Aug 19 16:18:30 2010
@@ -36,7 +36,7 @@ public class ViewDefinitionEntry
     private final String viewId;
     private final Class<? extends ViewDefinition> viewDefinitionClass;
     private final NavigationMode navigationMode;
-    private final List<String> beanNames;
+    private final List<PageBeanDefinitionEntry> beanDefinition;
 
     public ViewDefinitionEntry(String viewId,
                                Class<? extends ViewDefinition> viewDefinitionClass,
@@ -46,7 +46,7 @@ public class ViewDefinitionEntry
         this.viewDefinitionClass = viewDefinitionClass;
         this.navigationMode = navigationMode;
 
-        beanNames = Collections.unmodifiableList(findBeanNames(viewDefinitionClass));
+        beanDefinition = Collections.unmodifiableList(findPageBeanDefinitions(viewDefinitionClass));
         //TODO validate view-id
     }
 
@@ -65,12 +65,12 @@ public class ViewDefinitionEntry
         return navigationMode;
     }
 
-    public List<String> getBeanNames()
+    List<PageBeanDefinitionEntry> getBeanDefinitions()
     {
-        return beanNames;
+        return beanDefinition;
     }
 
-    private List<String> findBeanNames(Class<? extends ViewDefinition> viewDefinitionClass)
+    private List<PageBeanDefinitionEntry> findPageBeanDefinitions(Class<? extends
ViewDefinition> viewDefinitionClass)
     {
         if(!viewDefinitionClass.isAnnotationPresent(PageBean.class) &&
                 !viewDefinitionClass.isAnnotationPresent(PageBean.List.class))
@@ -78,51 +78,58 @@ public class ViewDefinitionEntry
             return Collections.emptyList();
         }
 
-        List<String> result = new ArrayList<String>();
+        List<PageBeanDefinitionEntry> result = new ArrayList<PageBeanDefinitionEntry>();
 
         if(viewDefinitionClass.isAnnotationPresent(PageBean.class))
         {
-            result.add(extractBeanName(viewDefinitionClass.getAnnotation(PageBean.class)));
+            result.add(extractBeanEntry(viewDefinitionClass.getAnnotation(PageBean.class)));
         }
 
         if(viewDefinitionClass.isAnnotationPresent(PageBean.List.class))
         {
-            result.addAll(extractBeanNames(viewDefinitionClass.getAnnotation(PageBean.List.class)));
+            result.addAll(extractBeanEntries(viewDefinitionClass.getAnnotation(PageBean.List.class)));
         }
 
         return result;
     }
 
-    private List<String> extractBeanNames(PageBean.List pageBeanList)
+    private List<PageBeanDefinitionEntry> extractBeanEntries(PageBean.List pageBeanList)
     {
-        List<String> result = new ArrayList<String>();
+        List<PageBeanDefinitionEntry> result = new ArrayList<PageBeanDefinitionEntry>();
         for(PageBean pageBean : pageBeanList.value())
         {
-            result.add(extractBeanName(pageBean));
+            result.add(extractBeanEntry(pageBean));
         }
         return result;
     }
 
-    private String extractBeanName(PageBean pageBean)
+    private PageBeanDefinitionEntry extractBeanEntry(PageBean pageBean)
     {
         if(!"".equals(pageBean.name()))
         {
-            return pageBean.name();
+            return new PageBeanDefinitionEntry(pageBean.name(), pageBean.value());
         }
 
         Class<?> pageBeanClass = pageBean.value();
+        String pageBeanName = null;
 
+        //TODO allow indirect usage of @Named
         if(pageBeanClass.isAnnotationPresent(Named.class))
         {
             String beanName = pageBeanClass.getAnnotation(Named.class).value();
 
             if(!"".equals(beanName))
             {
-                return beanName;
+                pageBeanName = beanName;
             }
         }
 
-        return Introspector.decapitalize(pageBeanClass.getSimpleName());
+        if(pageBeanName == null)
+        {
+            pageBeanName = Introspector.decapitalize(pageBeanClass.getSimpleName());
+        }
+
+        return new PageBeanDefinitionEntry(pageBeanName, pageBeanClass);
     }
 
     @Override



Mime
View raw message