struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yasserzam...@apache.org
Subject [struts] 01/02: monitor not-monitored loaded files on demand
Date Sat, 03 Nov 2018 10:43:29 GMT
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 <yasserzamani@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 {


Mime
View raw message