You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by ya...@apache.org on 2018/11/03 10:43:28 UTC

[struts] branch master updated (9fd8d6f -> b13e785)

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

yasserzamani pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git.


    from 9fd8d6f  WW-4971 Use response encoding for include by default
     new 1a84bf8  monitor not-monitored loaded files on demand
     new b13e785  not miss container provided fileUrl and ...

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../xwork2/util/fs/DefaultFileManager.java         | 23 +++++++++--
 .../apache/struts2/util/fs/JBossFileManager.java   |  2 +-
 .../providers/XmlConfigurationProviderTest.java    | 45 +++++++++++++++++++---
 ...k-test-wildcard-1.xml => xwork-test-reload.xml} |  8 ++--
 4 files changed, 64 insertions(+), 14 deletions(-)
 copy core/src/test/resources/com/opensymphony/xwork2/config/providers/{xwork-test-wildcard-1.xml => xwork-test-reload.xml} (84%)


[struts] 01/02: monitor not-monitored loaded files on demand

Posted by ya...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

yasserzamani pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git

commit 1a84bf8f94d31d03311d61781da84a5f36e740b9
Author: Yasser Zamani <ya...@apache.org>
AuthorDate: Wed Oct 31 19:51:37 2018 +0330

    monitor not-monitored loaded files on demand
    
    See WW-4974
    
    (cherry picked from commit bac0f29)
---
 .../xwork2/util/fs/DefaultFileManager.java         | 26 ++++++++++++++++++----
 .../apache/struts2/util/fs/JBossFileManager.java   |  2 +-
 .../providers/XmlConfigurationProviderTest.java    | 12 +++++-----
 3 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/core/src/main/java/com/opensymphony/xwork2/util/fs/DefaultFileManager.java b/core/src/main/java/com/opensymphony/xwork2/util/fs/DefaultFileManager.java
index 21d6cc0..d39e914 100644
--- a/core/src/main/java/com/opensymphony/xwork2/util/fs/DefaultFileManager.java
+++ b/core/src/main/java/com/opensymphony/xwork2/util/fs/DefaultFileManager.java
@@ -40,7 +40,7 @@ public class DefaultFileManager implements FileManager {
     private static final Pattern JAR_PATTERN = Pattern.compile("^(jar:|wsjar:|zip:|vfsfile:|code-source:)?(file:)?(.*?)(\\!/|\\.jar/)(.*)");
     private static final int JAR_FILE_PATH = 3;
 
-    protected static Map<String, Revision> files = Collections.synchronizedMap(new HashMap<String, Revision>());
+    protected static final Map<String, Revision> files = Collections.synchronizedMap(new HashMap<String, Revision>());
 
     protected boolean reloadingConfigs = false;
 
@@ -48,6 +48,22 @@ public class DefaultFileManager implements FileManager {
     }
 
     public void setReloadingConfigs(boolean reloadingConfigs) {
+        if (reloadingConfigs && !this.reloadingConfigs) {
+            this.reloadingConfigs = true;
+            //starting monitoring not-monitored loaded files on demand
+            synchronized (files) {
+                for (String fileName :
+                        files.keySet()) {
+                    if (null == files.get(fileName)) {
+                        try {
+                            monitorFile(new URL(fileName));
+                        } catch (MalformedURLException e) {
+                            LOG.warn("Error creating URL from [{}]!", fileName, e);
+                        }
+                    }
+                }
+            }
+        }
         this.reloadingConfigs = reloadingConfigs;
     }
 
@@ -70,9 +86,7 @@ public class DefaultFileManager implements FileManager {
             return null;
         }
         InputStream is = openFile(fileUrl);
-        if (reloadingConfigs) {
-            monitorFile(fileUrl);
-        }
+        monitorFile(fileUrl);
         return is;
     }
 
@@ -90,6 +104,10 @@ public class DefaultFileManager implements FileManager {
 
     public void monitorFile(URL fileUrl) {
         String fileName = fileUrl.toString();
+        if (!reloadingConfigs) {
+            files.put(fileName, null);
+            return;
+        }
         Revision revision;
         LOG.debug("Creating revision for URL: {}", fileName);
         if (isJarURL(fileUrl)) {
diff --git a/core/src/main/java/org/apache/struts2/util/fs/JBossFileManager.java b/core/src/main/java/org/apache/struts2/util/fs/JBossFileManager.java
index b3a19a7..7b7de30 100644
--- a/core/src/main/java/org/apache/struts2/util/fs/JBossFileManager.java
+++ b/core/src/main/java/org/apache/struts2/util/fs/JBossFileManager.java
@@ -81,7 +81,7 @@ public class JBossFileManager extends DefaultFileManager {
 
     @Override
     public void monitorFile(URL fileUrl) {
-        if (isJBossUrl(fileUrl)) {
+        if (reloadingConfigs && isJBossUrl(fileUrl)) {
             String fileName = fileUrl.toString();
             LOG.debug("Creating revision for URL: {}", fileName);
             URL normalizedUrl = normalizeToFileProtocol(fileUrl);
diff --git a/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderTest.java b/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderTest.java
index b66c2ff..63f9b53 100644
--- a/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderTest.java
@@ -80,10 +80,10 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
     public void testNeedsReload() throws Exception {
         final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
         ConfigurationProvider provider = new XmlConfigurationProvider(filename, true);
-        container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
         container.inject(provider);
         provider.init(configuration);
         provider.loadPackages();
+        container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
 
         assertFalse(provider.needsReload()); // Revision exists and timestamp didn't change
 
@@ -176,10 +176,10 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
     public void testEmptySpaces() throws Exception {
         final String filename = "com/opensymphony/xwork2/config/providers/xwork- test.xml";
         ConfigurationProvider provider = new XmlConfigurationProvider(filename, true);
-        container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
         container.inject(provider);
         provider.init(configuration);
         provider.loadPackages();
+        container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
 
         assertFalse(provider.needsReload());
 
@@ -215,7 +215,6 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
     }
 
     public void testConfigsInJarFiles() throws Exception {
-        container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
         testProvider("xwork-jar.xml");
         testProvider("xwork-zip.xml");
         testProvider("xwork - jar.xml");
@@ -229,7 +228,8 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
 
     private void testProvider(String configFile) throws Exception {
         ConfigurationProvider provider = buildConfigurationProvider(configFile);
-        assertTrue(!provider.needsReload());
+        container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
+        assertFalse(provider.needsReload());
 
         String fullPath = ClassLoaderUtil.getResource(configFile, ConfigurationProvider.class).toString();
 
@@ -241,9 +241,9 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
         File file = new File(jar);
 
         assertTrue("File [" + file + "] doesn't exist!", file.exists());
-        file.setLastModified(System.currentTimeMillis());
+        changeFileTime(file);
 
-        assertTrue(!provider.needsReload());
+        assertFalse(provider.needsReload());
     }
 
     public void testIncludeWithWildcard() throws Exception {


[struts] 02/02: not miss container provided fileUrl and ...

Posted by ya...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

yasserzamani pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git

commit b13e7857ffcd40446e2a9629ae49949b3c441c94
Author: Yasser Zamani <ya...@apache.org>
AuthorDate: Thu Nov 1 15:17:02 2018 +0330

    not miss container provided fileUrl and ...
    
    lazy monitoring for performance. Fix and test stopping reload configs at runtime.
    
    Fixes WW-4974
    
    (cherry picked from commit ecb7bee)
---
 .../xwork2/util/fs/DefaultFileManager.java         | 21 ++++++--------
 .../providers/XmlConfigurationProviderTest.java    | 33 ++++++++++++++++++++++
 .../xwork2/config/providers/xwork-test-reload.xml  | 31 ++++++++++++++++++++
 3 files changed, 73 insertions(+), 12 deletions(-)

diff --git a/core/src/main/java/com/opensymphony/xwork2/util/fs/DefaultFileManager.java b/core/src/main/java/com/opensymphony/xwork2/util/fs/DefaultFileManager.java
index d39e914..5708bd5 100644
--- a/core/src/main/java/com/opensymphony/xwork2/util/fs/DefaultFileManager.java
+++ b/core/src/main/java/com/opensymphony/xwork2/util/fs/DefaultFileManager.java
@@ -41,6 +41,7 @@ public class DefaultFileManager implements FileManager {
     private static final int JAR_FILE_PATH = 3;
 
     protected static final Map<String, Revision> files = Collections.synchronizedMap(new HashMap<String, Revision>());
+    private static final List<URL> lazyMonitoredFilesCache = Collections.synchronizedList(new ArrayList<URL>());
 
     protected boolean reloadingConfigs = false;
 
@@ -49,19 +50,13 @@ public class DefaultFileManager implements FileManager {
 
     public void setReloadingConfigs(boolean reloadingConfigs) {
         if (reloadingConfigs && !this.reloadingConfigs) {
+            //starting monitoring cached not-monitored files (lazy monitoring on demand because of performance)
             this.reloadingConfigs = true;
-            //starting monitoring not-monitored loaded files on demand
-            synchronized (files) {
-                for (String fileName :
-                        files.keySet()) {
-                    if (null == files.get(fileName)) {
-                        try {
-                            monitorFile(new URL(fileName));
-                        } catch (MalformedURLException e) {
-                            LOG.warn("Error creating URL from [{}]!", fileName, e);
-                        }
-                    }
+            synchronized (lazyMonitoredFilesCache) {
+                for (URL fileUrl : lazyMonitoredFilesCache) {
+                    monitorFile(fileUrl);
                 }
+                lazyMonitoredFilesCache.clear();
             }
         }
         this.reloadingConfigs = reloadingConfigs;
@@ -105,7 +100,9 @@ public class DefaultFileManager implements FileManager {
     public void monitorFile(URL fileUrl) {
         String fileName = fileUrl.toString();
         if (!reloadingConfigs) {
-            files.put(fileName, null);
+            //reserve file for monitoring on demand because of performance
+            files.remove(fileName);
+            lazyMonitoredFilesCache.add(fileUrl);
             return;
         }
         Revision revision;
diff --git a/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderTest.java b/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderTest.java
index 63f9b53..faa6b93 100644
--- a/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderTest.java
+++ b/core/src/test/java/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderTest.java
@@ -31,6 +31,9 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -94,6 +97,36 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
         assertTrue(provider.needsReload());
     }
 
+    public void testReload() throws Exception {
+        final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-reload.xml";
+        ConfigurationProvider provider = new XmlConfigurationProvider(filename, true);
+        loadConfigurationProviders(provider);
+
+        assertFalse(provider.needsReload()); // Revision exists and timestamp didn't change
+
+        File file = new File(getClass().getResource("/" + filename).toURI());
+        assertTrue("not exists: " + file.toString(), file.exists());
+
+        Path configPath = Paths.get(file.getAbsolutePath());
+        String content = new String(Files.readAllBytes(configPath));
+        content = content.replaceAll("<constant name=\"struts.configuration.xml.reload\" value=\"true\" />",
+                "<constant name=\"struts.configuration.xml.reload\" value=\"false\" />");
+        Files.write(configPath, content.getBytes()); // user demand: stop reloading configs
+
+        try {
+            assertTrue(provider.needsReload()); // config file has changed in previous lines
+
+            configurationManager.reload();
+
+            changeFileTime(file);
+            assertFalse(provider.needsReload());    // user already has stopped reloading configs
+        } finally {
+            content = content.replaceAll("<constant name=\"struts.configuration.xml.reload\" value=\"false\" />",
+                    "<constant name=\"struts.configuration.xml.reload\" value=\"true\" />");
+            Files.write(configPath, content.getBytes());
+        }
+    }
+
     public void testNeedsReloadNotReloadingConfigs() throws Exception {
         final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
         buildConfigurationProvider(filename);
diff --git a/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-reload.xml b/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-reload.xml
new file mode 100644
index 0000000..ffd5755
--- /dev/null
+++ b/core/src/test/resources/com/opensymphony/xwork2/config/providers/xwork-test-reload.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+/*
+ * 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.
+ */
+-->
+<!DOCTYPE xwork PUBLIC
+    "-//Apache Struts//XWork 2.5//EN"
+    "http://struts.apache.org/dtds/xwork-2.5.dtd"
+ >
+
+<xwork>
+
+    <constant name="struts.configuration.xml.reload" value="true" />
+
+</xwork>