monitor file only if needed

See also: WW-4948
This commit is contained in:
Yasser Zamani
2018-09-10 18:18:42 +04:30
parent b802baa4a9
commit b816cc1374
3 changed files with 83 additions and 27 deletions
@@ -70,7 +70,9 @@ public class DefaultFileManager implements FileManager {
return null; return null;
} }
InputStream is = openFile(fileUrl); InputStream is = openFile(fileUrl);
monitorFile(fileUrl); if (reloadingConfigs) {
monitorFile(fileUrl);
}
return is; return is;
} }
@@ -78,12 +78,14 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
} }
public void testNeedsReload() throws Exception { public void testNeedsReload() throws Exception {
container.getInstance(FileManagerFactory.class).setReloadingConfigs("true");
final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml"; final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
ConfigurationProvider provider = buildConfigurationProvider(filename); ConfigurationProvider provider = new XmlConfigurationProvider(filename, true);
container.getInstance(FileManagerFactory.class).setReloadingConfigs("true"); container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
container.inject(provider);
provider.init(configuration);
provider.loadPackages();
assertTrue(!provider.needsReload()); // Revision exists and timestamp didn't change assertFalse(provider.needsReload()); // Revision exists and timestamp didn't change
File file = new File(getClass().getResource("/" + filename).toURI()); File file = new File(getClass().getResource("/" + filename).toURI());
assertTrue("not exists: " + file.toString(), file.exists()); assertTrue("not exists: " + file.toString(), file.exists());
@@ -92,6 +94,24 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
assertTrue(provider.needsReload()); assertTrue(provider.needsReload());
} }
public void testNeedsReloadNotReloadingConfigs() throws Exception {
final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
buildConfigurationProvider(filename);
ConfigurationProvider provider = new XmlConfigurationProvider(filename, true);
container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(false);
container.inject(provider);
provider.init(configuration);
provider.loadPackages();
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());
changeFileTime(file);
assertFalse(provider.needsReload());
}
public void testInheritence() throws Exception { public void testInheritence() throws Exception {
final String filename = "com/opensymphony/xwork2/config/providers/xwork-include-parent.xml"; final String filename = "com/opensymphony/xwork2/config/providers/xwork-include-parent.xml";
ConfigurationProvider provider = buildConfigurationProvider(filename); ConfigurationProvider provider = buildConfigurationProvider(filename);
@@ -155,10 +175,13 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
public void testEmptySpaces() throws Exception { public void testEmptySpaces() throws Exception {
final String filename = "com/opensymphony/xwork2/config/providers/xwork- test.xml"; 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.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
container.inject(provider);
provider.init(configuration);
provider.loadPackages();
ConfigurationProvider provider = buildConfigurationProvider(filename); assertFalse(provider.needsReload());
assertTrue(!provider.needsReload());
URI uri = ClassLoaderUtil.getResource(filename, ConfigurationProvider.class).toURI(); URI uri = ClassLoaderUtil.getResource(filename, ConfigurationProvider.class).toURI();
@@ -170,6 +193,27 @@ public class XmlConfigurationProviderTest extends ConfigurationTestBase {
assertTrue(provider.needsReload()); assertTrue(provider.needsReload());
} }
public void testEmptySpacesNotReloadingConfigs() throws Exception {
final String filename = "com/opensymphony/xwork2/config/providers/xwork- test.xml";
buildConfigurationProvider(filename);
ConfigurationProvider provider = new XmlConfigurationProvider(filename, true);
container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(false);
container.inject(provider);
provider.init(configuration);
provider.loadPackages();
assertFalse(provider.needsReload());
URI uri = ClassLoaderUtil.getResource(filename, ConfigurationProvider.class).toURI();
File file = new File(uri);
assertTrue(file.exists());
changeFileTime(file);
assertFalse(provider.needsReload());
}
public void testConfigsInJarFiles() throws Exception { public void testConfigsInJarFiles() throws Exception {
container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true); container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
testProvider("xwork-jar.xml"); testProvider("xwork-jar.xml");
@@ -21,16 +21,9 @@ package com.opensymphony.xwork2.util;
import com.opensymphony.xwork2.FileManager; import com.opensymphony.xwork2.FileManager;
import com.opensymphony.xwork2.FileManagerFactory; import com.opensymphony.xwork2.FileManagerFactory;
import com.opensymphony.xwork2.XWorkTestCase; import com.opensymphony.xwork2.XWorkTestCase;
import com.opensymphony.xwork2.util.fs.DefaultFileManager;
import org.apache.struts2.util.fs.JBossFileManager;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
/** /**
* FileManager Tester. * FileManager Tester.
@@ -49,23 +42,40 @@ public class DefaultFileManagerTest extends XWorkTestCase {
fileManager = container.getInstance(FileManagerFactory.class).getFileManager(); fileManager = container.getInstance(FileManagerFactory.class).getFileManager();
} }
public void disabled_testGetFileInJar() throws Exception { public void testGetFileInJar() throws Exception {
testLoadFile("xwork-jar.xml"); testLoadFile("xwork-jar.xml", false);
testLoadFile("xwork - jar.xml"); testLoadFile("xwork - jar.xml", false);
testLoadFile("xwork-zip.xml"); testLoadFile("xwork-zip.xml", false);
testLoadFile("xwork - zip.xml"); testLoadFile("xwork - zip.xml", false);
testLoadFile("xwork-jar2.xml"); testLoadFile("xwork-jar2.xml", false);
testLoadFile("xwork - jar2.xml"); testLoadFile("xwork - jar2.xml", false);
testLoadFile("xwork-zip2.xml"); testLoadFile("xwork-zip2.xml", false);
testLoadFile("xwork - zip2.xml"); testLoadFile("xwork - zip2.xml", false);
testLoadFile("xwork-jar.xml", true);
testLoadFile("xwork - jar.xml", true);
testLoadFile("xwork-zip.xml", true);
testLoadFile("xwork - zip.xml", true);
testLoadFile("xwork-jar2.xml", true);
testLoadFile("xwork - jar2.xml", true);
testLoadFile("xwork-zip2.xml", true);
testLoadFile("xwork - zip2.xml", true);
} }
private void testLoadFile(String fileName) { private void testLoadFile(String fileName, boolean reloadConfigs) throws Exception {
fileManager.setReloadingConfigs(true); fileManager.setReloadingConfigs(reloadConfigs);
URL url = ClassLoaderUtil.getResource(fileName, DefaultFileManagerTest.class); URL url = ClassLoaderUtil.getResource(fileName, DefaultFileManagerTest.class);
InputStream file = fileManager.loadFile(url); InputStream file = fileManager.loadFile(url);
assertNotNull(file); assertNotNull(file);
assertTrue(fileManager.fileNeedsReloading(fileName)); file.close();
assertFalse(fileManager.fileNeedsReloading(url.toString()));
long now = System.currentTimeMillis();
JarURLConnection conn = (JarURLConnection) url.openConnection();
conn.getJarEntry().setTime(now + 60000);
conn.getInputStream().close();
assertEquals(reloadConfigs, fileManager.fileNeedsReloading(url.toString()));
} }
public void testReloadingConfigs() throws Exception { public void testReloadingConfigs() throws Exception {