You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2023/03/28 12:56:59 UTC

[myfaces] branch main updated: MYFACES-4469

This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/main by this push:
     new fb5d71e9a MYFACES-4469
fb5d71e9a is described below

commit fb5d71e9a563b3be7da8ff8c8bf272f425c610bf
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Tue Mar 28 14:56:49 2023 +0200

    MYFACES-4469
---
 .../application/ApplicationFactoryImpl.java        |  6 +-
 .../apache/myfaces/config/FacesConfigurator.java   | 97 +++++++---------------
 .../org/apache/myfaces/config/RuntimeConfig.java   |  4 +-
 .../context/servlet/FacesContextImplBase.java      | 13 ++-
 .../myfaces/lifecycle/LifecycleFactoryImpl.java    |  6 +-
 .../myfaces/renderkit/RenderKitFactoryImpl.java    |  6 +-
 .../java/org/apache/myfaces/util/Purgeable.java    | 24 ++++++
 7 files changed, 73 insertions(+), 83 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/application/ApplicationFactoryImpl.java b/impl/src/main/java/org/apache/myfaces/application/ApplicationFactoryImpl.java
index b0836aec0..e2a1c81ec 100755
--- a/impl/src/main/java/org/apache/myfaces/application/ApplicationFactoryImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/application/ApplicationFactoryImpl.java
@@ -26,13 +26,14 @@ import jakarta.faces.application.ApplicationFactory;
 import jakarta.faces.context.ExternalContext;
 import jakarta.faces.context.FacesContext;
 import org.apache.myfaces.core.api.shared.lang.Assert;
+import org.apache.myfaces.util.Purgeable;
 
 /**
  * @author Manfred Geiler (latest modification by $Author$)
  * @author Thomas Spiegl
  * @version $Revision$ $Date$
  */
-public class ApplicationFactoryImpl extends ApplicationFactory
+public class ApplicationFactoryImpl extends ApplicationFactory implements Purgeable
 {
     private static final Logger log = Logger.getLogger(ApplicationFactoryImpl.class.getName());
 
@@ -60,7 +61,8 @@ public class ApplicationFactoryImpl extends ApplicationFactory
         }
     }
 
-    public void purgeApplication()
+    @Override
+    public void purge()
     {
         createAndLogNewApplication();
     }
diff --git a/impl/src/main/java/org/apache/myfaces/config/FacesConfigurator.java b/impl/src/main/java/org/apache/myfaces/config/FacesConfigurator.java
index 097023c16..0e391e624 100755
--- a/impl/src/main/java/org/apache/myfaces/config/FacesConfigurator.java
+++ b/impl/src/main/java/org/apache/myfaces/config/FacesConfigurator.java
@@ -19,8 +19,6 @@
 package org.apache.myfaces.config;
 
 import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -147,6 +145,7 @@ import org.apache.myfaces.spi.ResourceLibraryContractsProvider;
 import org.apache.myfaces.spi.ResourceLibraryContractsProviderFactory;
 import org.apache.myfaces.util.ExternalSpecifications;
 import org.apache.myfaces.util.NavigationUtils;
+import org.apache.myfaces.util.Purgeable;
 import org.apache.myfaces.view.ViewDeclarationLanguageFactoryImpl;
 import org.apache.myfaces.view.facelets.el.ELText;
 import org.apache.myfaces.view.facelets.impl.FaceletCacheFactoryImpl;
@@ -388,30 +387,12 @@ public class FacesConfigurator
             long ttl = lastUpdate + refreshPeriod;
             if ((System.currentTimeMillis() > ttl) && (getLastModifiedTime() > ttl))
             {
-                boolean purged = false;
                 try
                 {
-                    purged = purgeConfiguration();
-                }
-                catch (NoSuchMethodException e)
-                {
-                    log.severe("Configuration objects do not support clean-up. Update aborted");
-
-                    // We still want to update the timestamp to avoid running purge on every subsequent
-                    // request after this one.
-                    //
-                    lastUpdate = System.currentTimeMillis();
-
-                    return;
-                }
-                catch (IllegalAccessException | InvocationTargetException e)
-                {
-                    log.severe("Error during configuration clean-up" + e.getMessage());
-                }
-                if (purged)
-                {
+                    purgeConfiguration();
+  
                     configure();
-                    
+
                     // Faces 2.0 Publish PostConstructApplicationEvent after all configuration resources
                     // has been parsed and processed
                     FacesContext facesContext = getFacesContext();
@@ -420,79 +401,57 @@ public class FacesConfigurator
                     application.publishEvent(facesContext, PostConstructApplicationEvent.class,
                             Application.class, application);
                 }
+                catch (Exception e)
+                {
+                    log.severe("Error during configuration clean-up" + e.getMessage());
+
+                    // We still want to update the timestamp to avoid running purge on every subsequent
+                    // request after this one.
+                    //
+                    lastUpdate = System.currentTimeMillis();
+                }
             }
         }
     }
 
-    private boolean purgeConfiguration() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
+    private void purgeConfiguration()
     {
-
-        Method appFactoryPurgeMethod;
-        Method renderKitPurgeMethod;
-        Method lifecyclePurgeMethod;
-        Method facesContextPurgeMethod;
-
         // Check that we have access to all of the necessary purge methods before purging anything
-        //
+
         ApplicationFactory applicationFactory
                 = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
-        appFactoryPurgeMethod = getPurgeMethod(applicationFactory, "purgeApplication", NO_PARAMETER_TYPES);
+        purge(applicationFactory);
 
         RenderKitFactory renderKitFactory
                 = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
-        renderKitPurgeMethod = getPurgeMethod(renderKitFactory, "purgeRenderKit", NO_PARAMETER_TYPES);
+        purge(renderKitFactory);
+
+        RuntimeConfig.getCurrentInstance(_externalContext).purge();
 
         LifecycleFactory lifecycleFactory
                 = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
-        lifecyclePurgeMethod = getPurgeMethod(lifecycleFactory, "purgeLifecycle", NO_PARAMETER_TYPES);
+        purge(lifecycleFactory);
 
         FacesContext facesContext = getFacesContext();
-        facesContextPurgeMethod = getPurgeMethod(facesContext, "purgeFacesContext", NO_PARAMETER_TYPES);
-        
-        // If there was no exception so far, now we can purge
-        //
-        if (appFactoryPurgeMethod != null && renderKitPurgeMethod != null && lifecyclePurgeMethod != null && 
-            facesContextPurgeMethod != null)
-        {
-            appFactoryPurgeMethod.invoke(applicationFactory, NO_PARAMETERS);
-            renderKitPurgeMethod.invoke(renderKitFactory, NO_PARAMETERS);
-            RuntimeConfig.getCurrentInstance(_externalContext).purge();
-            lifecyclePurgeMethod.invoke(lifecycleFactory, NO_PARAMETERS);
-            facesContextPurgeMethod.invoke(facesContext, NO_PARAMETERS);
+        purge(facesContext);
 
-            // factories and serial factory need not be purged...
-
-            // Remove first request processed so we can initialize it again
-            _externalContext.getApplicationMap().remove(LifecycleImpl.FIRST_REQUEST_PROCESSED_PARAM);
-            return true;
-        }
-        return false;
+        _externalContext.getApplicationMap().remove(LifecycleImpl.FIRST_REQUEST_PROCESSED_PARAM);
     }
     
-    private Method getPurgeMethod(Object instance, String methodName, Class<?>[] parameters)
-    {
+    <T> void purge(T instance)
+    {        
         while (instance != null)
         {
-            Method purgeMethod = null;
-            try
+            if (instance instanceof Purgeable)
             {
-                purgeMethod = instance.getClass().getMethod(methodName, parameters);
-            }
-            catch (NoSuchMethodException e)
-            {
-                // No op, it is expected to found this case, so in that case
-                // look for the parent to do the purge
-            }
-            if (purgeMethod != null)
-            {
-                return purgeMethod;
+                ((Purgeable) instance).purge();
             }
+
             if (instance instanceof FacesWrapper)
             {
-                instance = ((FacesWrapper)instance).getWrapped();
+                instance = ((FacesWrapper<T>) instance).getWrapped();
             }
         }
-        return null;
     }
 
     public void configure() throws FacesException
diff --git a/impl/src/main/java/org/apache/myfaces/config/RuntimeConfig.java b/impl/src/main/java/org/apache/myfaces/config/RuntimeConfig.java
index 20d2a3068..a7b2466eb 100755
--- a/impl/src/main/java/org/apache/myfaces/config/RuntimeConfig.java
+++ b/impl/src/main/java/org/apache/myfaces/config/RuntimeConfig.java
@@ -46,6 +46,7 @@ import org.apache.myfaces.config.element.ResourceBundle;
 import org.apache.myfaces.config.element.ViewPoolMapping;
 import org.apache.myfaces.config.element.facelets.FaceletTagLibrary;
 import org.apache.myfaces.core.api.shared.lang.Assert;
+import org.apache.myfaces.util.Purgeable;
 
 /**
  * Holds all configuration information (from the faces-config xml files) that is needed later during runtime. The config
@@ -56,7 +57,7 @@ import org.apache.myfaces.core.api.shared.lang.Assert;
  * @version $Revision$ $Date$
  */
 @SuppressWarnings("deprecation")
-public class RuntimeConfig
+public class RuntimeConfig implements Purgeable
 {
     private static final Logger log = Logger.getLogger(RuntimeConfig.class.getName());
 
@@ -127,6 +128,7 @@ public class RuntimeConfig
                 APPLICATION_MAP_PARAM_NAME, k -> new RuntimeConfig());
     }
 
+    @Override
     public void purge()
     {
         _navigationRules.clear();
diff --git a/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImplBase.java b/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImplBase.java
index af1ec59be..4c6107e40 100644
--- a/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImplBase.java
+++ b/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImplBase.java
@@ -41,6 +41,7 @@ import org.apache.myfaces.cdi.view.ViewTransientScopeContext;
 
 import org.apache.myfaces.el.FacesELContext;
 import org.apache.myfaces.core.api.shared.lang.Assert;
+import org.apache.myfaces.util.Purgeable;
 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage; 
 
 /**
@@ -50,7 +51,7 @@ import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
  * @author Jakob Korherr (latest modification by $Author$)
  * @version $Revision$ $Date$
  */
-public abstract class FacesContextImplBase extends FacesContext
+public abstract class FacesContextImplBase extends FacesContext implements Purgeable
 {
     private Application _application;
     private ExternalContext _externalContext;
@@ -178,18 +179,16 @@ public abstract class FacesContextImplBase extends FacesContext
         
         return _application;
     }
-    
-    /**
-     * 
-     */
-    public void purgeFacesContext()
+
+    @Override
+    public void purge()
     {
         _application = null;
         _renderKitFactory = null;
         _cachedRenderKit = null;
         _cachedRenderKitId = null;
     }
-    
+
     @Override
     public final ExceptionHandler getExceptionHandler()
     {
diff --git a/impl/src/main/java/org/apache/myfaces/lifecycle/LifecycleFactoryImpl.java b/impl/src/main/java/org/apache/myfaces/lifecycle/LifecycleFactoryImpl.java
index 776fa45e3..616c50530 100755
--- a/impl/src/main/java/org/apache/myfaces/lifecycle/LifecycleFactoryImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/lifecycle/LifecycleFactoryImpl.java
@@ -25,13 +25,14 @@ import java.util.concurrent.ConcurrentHashMap;
 import jakarta.faces.FacesException;
 import jakarta.faces.lifecycle.Lifecycle;
 import jakarta.faces.lifecycle.LifecycleFactory;
+import org.apache.myfaces.util.Purgeable;
 
 /**
  * @author Manfred Geiler (latest modification by $Author$)
  * @author Anton Koinov
  * @version $Revision$ $Date$
  */
-public class LifecycleFactoryImpl extends LifecycleFactory
+public class LifecycleFactoryImpl extends LifecycleFactory implements Purgeable
 {
     /**
      * At start we used synchronized blocks for addLifecycle and getLifecycle. But thinking about it,
@@ -46,7 +47,8 @@ public class LifecycleFactoryImpl extends LifecycleFactory
         addLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE, new LifecycleImpl());
     }
 
-    public void purgeLifecycle()
+    @Override
+    public void purge()
     {
         // Note this is not safe, because if by some coincidence one thread call getLifecycle between
         // the two lines below it will throw IllegalArgumentException, but this method is not supposed 
diff --git a/impl/src/main/java/org/apache/myfaces/renderkit/RenderKitFactoryImpl.java b/impl/src/main/java/org/apache/myfaces/renderkit/RenderKitFactoryImpl.java
index 3356b5351..08ff66d34 100755
--- a/impl/src/main/java/org/apache/myfaces/renderkit/RenderKitFactoryImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/renderkit/RenderKitFactoryImpl.java
@@ -29,6 +29,7 @@ import jakarta.faces.context.FacesContext;
 import jakarta.faces.render.RenderKit;
 import jakarta.faces.render.RenderKitFactory;
 import org.apache.myfaces.core.api.shared.lang.Assert;
+import org.apache.myfaces.util.Purgeable;
 
 /**
  * RenderKitFactory implementation as defined in Spec. Faces.7.3
@@ -36,7 +37,7 @@ import org.apache.myfaces.core.api.shared.lang.Assert;
  * @author Manfred Geiler (latest modification by $Author$)
  * @version $Revision$ $Date$
  */
-public class RenderKitFactoryImpl extends RenderKitFactory
+public class RenderKitFactoryImpl extends RenderKitFactory implements Purgeable
 {
     private static final Logger log = Logger.getLogger(RenderKitFactoryImpl.class.getName());
 
@@ -46,7 +47,8 @@ public class RenderKitFactoryImpl extends RenderKitFactory
     {
     }
 
-    public void purgeRenderKit()
+    @Override
+    public void purge()
     {
         renderkits.clear();
     }
diff --git a/impl/src/main/java/org/apache/myfaces/util/Purgeable.java b/impl/src/main/java/org/apache/myfaces/util/Purgeable.java
new file mode 100644
index 000000000..a7fbb7553
--- /dev/null
+++ b/impl/src/main/java/org/apache/myfaces/util/Purgeable.java
@@ -0,0 +1,24 @@
+/*
+ * 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.util;
+
+public interface Purgeable
+{
+    void purge();
+}