You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gp...@apache.org on 2011/02/19 00:27:16 UTC

svn commit: r1072193 - in /myfaces/extensions/cdi/trunk/jee-modules: jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/phase/ jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/request/ jsf...

Author: gpetracek
Date: Fri Feb 18 23:27:16 2011
New Revision: 1072193

URL: http://svn.apache.org/viewvc?rev=1072193&view=rev
Log:
EXTCDI-127 perform re-injection in case of postbacks

Added:
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/phase/RestoreInjectionPointsObserver.java
Modified:
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/request/InjectionAwareApplicationWrapper.java
    myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/request/InjectionAwareApplicationWrapper.java

Added: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/phase/RestoreInjectionPointsObserver.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/phase/RestoreInjectionPointsObserver.java?rev=1072193&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/phase/RestoreInjectionPointsObserver.java (added)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/phase/RestoreInjectionPointsObserver.java Fri Feb 18 23:27:16 2011
@@ -0,0 +1,133 @@
+/*
+ * 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.jsf.impl.listener.phase;
+
+import org.apache.myfaces.extensions.cdi.jsf.api.listener.phase.BeforePhase;
+import static org.apache.myfaces.extensions.cdi.jsf.api.listener.phase.JsfPhaseId.*;
+import org.apache.myfaces.extensions.cdi.jsf.api.request.RequestTypeResolver;
+import static org.apache.myfaces.extensions.cdi.core.impl.util.CodiUtils.injectFields;
+import org.apache.myfaces.extensions.cdi.core.api.config.CodiCoreConfig;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.context.ApplicationScoped;
+import javax.faces.event.PhaseEvent;
+import javax.faces.component.UIViewRoot;
+import javax.faces.component.UIComponent;
+import javax.faces.component.EditableValueHolder;
+import javax.faces.component.ValueHolder;
+import javax.faces.validator.Validator;
+import javax.faces.context.FacesContext;
+import java.util.List;
+
+/**
+ * see EXTCDI-127
+ *
+ * @author Gerhard Petracek
+ */
+@ApplicationScoped
+public class RestoreInjectionPointsObserver
+{
+    private String injectionMarker = RestoreInjectionPointsObserver.class.getName() + ":injected";
+
+    protected void restoreInjectionPoints(@Observes @BeforePhase(PROCESS_VALIDATIONS) PhaseEvent event,
+                                          CodiCoreConfig codiCoreConfig)
+    {
+        FacesContext facesContext = event.getFacesContext();
+        UIViewRoot uiViewRoot = facesContext.getViewRoot();
+
+        restoreAllInjectionPoints(uiViewRoot, codiCoreConfig);
+
+        facesContext.getExternalContext().getRequestMap().put(this.injectionMarker, uiViewRoot.getViewId());
+    }
+
+    protected void restoreInjectionPointsForSkippedRequests(@Observes @BeforePhase(RENDER_RESPONSE) PhaseEvent event,
+                                                            CodiCoreConfig codiCoreConfig,
+                                                            RequestTypeResolver requestTypeResolver)
+    {
+        //injection is performed by the application wrapper provided by codi in case of initial and get requests
+        if(!requestTypeResolver.isPostRequest())
+        {
+            return;
+        }
+
+        FacesContext facesContext = event.getFacesContext();
+        UIViewRoot uiViewRoot = facesContext.getViewRoot();
+
+        if(isSkippedPostback(facesContext))
+        {
+            //restored view but the life-cycle wasn't executed completely
+            restoreAllInjectionPoints(uiViewRoot, codiCoreConfig);
+        }
+    }
+
+    /**
+     * Checks if the {@link #restoreInjectionPoints} has been invoked
+     *
+     * @param facesContext current faces-context
+     * @return true if the injection points have to be restored, false otherwise
+     */
+    private boolean isSkippedPostback(FacesContext facesContext)
+    {
+        Object storedViewId = facesContext.getExternalContext().getRequestMap().get(this.injectionMarker);
+        return storedViewId == null;
+    }
+
+    private void restoreAllInjectionPoints(UIViewRoot uiViewRoot, CodiCoreConfig codiCoreConfig)
+    {
+        if(uiViewRoot == null)
+        {
+            return;
+        }
+
+        boolean advancedQualifierRequiredForDependencyInjection =
+                codiCoreConfig.isAdvancedQualifierRequiredForDependencyInjection();
+        processComponents(uiViewRoot.getChildren(), advancedQualifierRequiredForDependencyInjection);
+    }
+
+    private void processComponents(List<UIComponent> uiComponents,
+                                   boolean advancedQualifierRequiredForDependencyInjection)
+    {
+        if(uiComponents == null)
+        {
+            return;
+        }
+
+        for(UIComponent uiComponent : uiComponents)
+        {
+            inject(uiComponent, advancedQualifierRequiredForDependencyInjection);
+            processComponents(uiComponent.getChildren(), advancedQualifierRequiredForDependencyInjection);
+        }
+    }
+
+    private void inject(UIComponent uiComponent, boolean advancedQualifierRequiredForDependencyInjection)
+    {
+        if(uiComponent instanceof ValueHolder)
+        {
+            injectFields(((ValueHolder)uiComponent).getConverter());
+
+            if(uiComponent instanceof EditableValueHolder)
+            {
+                for(Validator validator : ((EditableValueHolder)uiComponent).getValidators())
+                {
+                    injectFields(validator, advancedQualifierRequiredForDependencyInjection);
+                }
+            }
+        }
+    }
+}

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/request/InjectionAwareApplicationWrapper.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/request/InjectionAwareApplicationWrapper.java?rev=1072193&r1=1072192&r2=1072193&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/request/InjectionAwareApplicationWrapper.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf/impl/listener/request/InjectionAwareApplicationWrapper.java Fri Feb 18 23:27:16 2011
@@ -49,6 +49,7 @@ import java.util.Collection;
  * TODO move it to a meaningful package
  *
  * @author Gerhard Petracek
+ * @see org.apache.myfaces.extensions.cdi.jsf.impl.listener.phase.RestoreInjectionPointsObserver
  */
 class InjectionAwareApplicationWrapper extends Application
 {

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/request/InjectionAwareApplicationWrapper.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/request/InjectionAwareApplicationWrapper.java?rev=1072193&r1=1072192&r2=1072193&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/request/InjectionAwareApplicationWrapper.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf20-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/jsf2/impl/listener/request/InjectionAwareApplicationWrapper.java Fri Feb 18 23:27:16 2011
@@ -30,6 +30,7 @@ import javax.faces.FacesException;
  * TODO move it to a meaningful package
  *
  * @author Gerhard Petracek
+ * @see org.apache.myfaces.extensions.cdi.jsf.impl.listener.phase.RestoreInjectionPointsObserver
  */
 class InjectionAwareApplicationWrapper extends ApplicationWrapper
 {