struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (WW-4974) NullPointerException in DefaultStaticContentLoader#findStaticResource
Date Sat, 03 Nov 2018 09:48:00 GMT

    [ https://issues.apache.org/jira/browse/WW-4974?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16673988#comment-16673988
] 

ASF GitHub Bot commented on WW-4974:
------------------------------------

lukaszlenart closed pull request #260: WW-4974 monitor not-monitored loaded files on demand
URL: https://github.com/apache/struts/pull/260
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 21d6cc0ad..5708bd5f2 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,8 @@
     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>());
+    private static final List<URL> lazyMonitoredFilesCache = Collections.synchronizedList(new
ArrayList<URL>());
 
     protected boolean reloadingConfigs = false;
 
@@ -48,6 +49,16 @@ public DefaultFileManager() {
     }
 
     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;
+            synchronized (lazyMonitoredFilesCache) {
+                for (URL fileUrl : lazyMonitoredFilesCache) {
+                    monitorFile(fileUrl);
+                }
+                lazyMonitoredFilesCache.clear();
+            }
+        }
         this.reloadingConfigs = reloadingConfigs;
     }
 
@@ -70,9 +81,7 @@ public InputStream loadFile(URL fileUrl) {
             return null;
         }
         InputStream is = openFile(fileUrl);
-        if (reloadingConfigs) {
-            monitorFile(fileUrl);
-        }
+        monitorFile(fileUrl);
         return is;
     }
 
@@ -90,6 +99,12 @@ private InputStream openFile(URL fileUrl) {
 
     public void monitorFile(URL fileUrl) {
         String fileName = fileUrl.toString();
+        if (!reloadingConfigs) {
+            //reserve file for monitoring on demand because of performance
+            files.remove(fileName);
+            lazyMonitoredFilesCache.add(fileUrl);
+            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 b3a19a79f..7b7de30ca 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 @@ private boolean isJBoss7() {
 
     @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 b66c2ff57..faa6b937a 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.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;
@@ -80,10 +83,10 @@ private static void changeFileTime(File f) throws Exception {
     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
 
@@ -94,6 +97,36 @@ public void testNeedsReload() throws Exception {
         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);
@@ -176,10 +209,10 @@ public void testGuessResultType() {
     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 +248,6 @@ public void testEmptySpacesNotReloadingConfigs() throws Exception {
     }
 
     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 +261,8 @@ public void testConfigsInJarFiles() throws Exception {
 
     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 +274,9 @@ private void testProvider(String configFile) throws Exception {
         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 {
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 000000000..ffd575523
--- /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>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> NullPointerException in DefaultStaticContentLoader#findStaticResource
> ---------------------------------------------------------------------
>
>                 Key: WW-4974
>                 URL: https://issues.apache.org/jira/browse/WW-4974
>             Project: Struts 2
>          Issue Type: Bug
>    Affects Versions: 2.5.18
>         Environment: Tomcat 9 on Ubuntu 18 Java 8 struts 2.5.18
>            Reporter: nikos dimitrakas
>            Priority: Major
>             Fix For: 2.6, 2.5.19
>
>
> pathPrefixes is null. The defaults that were loaded correctly in previous versions are
now not loaded. The method setHostConfig does not get called like in previous versions.
> This causes all js files to not be found. For example 
> /struts/js/base/version.min.js
> /struts/js/base/jquery-2.2.4.min.js
> /struts/js/struts2/jquery.ui.struts2.min.js
> /struts/js/struts2/jquery.datatables.struts2.min.js
> /struts/js/plugins/jquery.dataTables.min.js



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message