Commits

Anonymous committed 2c82b51

FileManager refactoring, new test for FileManager and resource files, details http://jira.opensymphony.com/browse/XW-674

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1922e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (6)

src/java/com/opensymphony/xwork2/util/FileManager.java

 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.URL;
 import java.net.URISyntaxException;
+import java.net.URL;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.zip.ZipEntry;
 import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
 
 
 /**
         return loadFile(fileUrl);
     }
 
-     /**
+    /**
      * Loads opens the named file and returns the InputStream
      *
      * @param fileUrl - the URL of the file to open
      * @throws IllegalArgumentException if there is no file with the given file name
      */
     public static InputStream loadFile(URL fileUrl) {
-         return loadFile(fileUrl, true);
-     }
+        return loadFile(fileUrl, true);
+    }
 
     /**
      * Loads opens the named file and returns the InputStream
      *
-     * @param fileUrl - the URL of the file to open
+     * @param fileUrl    - the URL of the file to open
      * @param openStream - if true, open an InputStream to the file and return it
      * @return an InputStream of the file contents or null
      * @throws IllegalArgumentException if there is no file with the given file name
             return null;
         }
 
-        String fileName = fileUrl.toString();
+        String fileName =  fileUrl.toString();
         InputStream is = null;
 
         if (openStream) {
         }
 
         if (isReloadingConfigs()) {
-            if (!fileName.startsWith(JAR_FILE_NAME_PREFIX)) {
-
-                File file;
-                try {
-                    file = new File(fileUrl.toURI());
-                } catch (URISyntaxException e) {
-                    file = new File(fileUrl.getPath());
-                }
-                long lastModified;
-
-                if (!file.exists() || !file.canRead()) {
-                    file = null;
-                }
-
-                if (file != null) {
-                    lastModified = file.lastModified();
-                    files.put(fileName, new FileRevision(file, lastModified));
-                } else {
-                    // Never expire a non-file resource
-                    files.put(fileName, new Revision());
-                }
-            }  else {
-                // File within a Jar
-                // Find separator index of jar filename and filename within jar
-                int separatorIndex = fileName.indexOf(JAR_FILE_NAME_SEPARATOR);
-                // Split file name
-                String jarFileName =
-                    fileName.substring(JAR_FILE_NAME_PREFIX.length(),
-                                       separatorIndex);
-                String fileNameInJar =
-                    fileName.substring(
-                        separatorIndex + JAR_FILE_NAME_SEPARATOR.length());
-
-                ZipEntry entry;
-                JarFile jarFile;
-                try {
-                    jarFile = new JarFile(jarFileName);
-                    entry = jarFile.getEntry(fileNameInJar);
-                }
-                catch(IOException e) {
-                    entry = null;
-                }
+            Revision revision;
 
-                if (entry != null) {
-                    files.put(fileName,
-                              new JarEntryRevision(jarFileName,
-                                                   fileNameInJar,
-                                                   entry.getTime()));
-                } else {
-                    // Never expire a non-file resource
-                    files.put(fileName, new Revision());
-                }
+            if (fileName.startsWith(JAR_FILE_NAME_PREFIX)) {
+                revision = JarEntryRevision.build(fileUrl);
+            } else {
+                revision = FileRevision.build(fileUrl);
+            }
+            if (revision == null) {
+                files.put(fileName, Revision.build(fileUrl));
+            } else {
+                files.put(fileName, revision);
             }
-
         }
-
         return is;
     }
 
     //~ Inner Classes //////////////////////////////////////////////////////////
 
+    /**
+     * Class represents common revsion resource, should be used as default class when no other option exists
+     */
     private static class Revision {
+
         public Revision() {
         }
 
         public boolean needsReloading() {
             return false;
         }
+
+        public static Revision build(URL fileUrl) {
+            return new Revision();
+        }
     }
 
+    /**
+     * Represents file resource revision, used for file://* resources
+     */
     private static class FileRevision extends Revision {
         private File file;
         private long lastModified;
             return this.lastModified < this.file.lastModified();
         }
 
+        public static Revision build(URL fileUrl) {
+            File file;
+            try {
+                file = new File(fileUrl.toURI());
+            } catch (URISyntaxException e) {
+                file = new File(fileUrl.getPath());
+            }
+            if (file.exists() && file.canRead()) {
+                long lastModified = file.lastModified();
+                return new FileRevision(file, lastModified);
+            }
+            return null;
+        }
     }
 
-    private static class JarEntryRevision
-        extends Revision {
+    /**
+     * Represents jar resurce revision, used for jar://* resource
+     */
+    private static class JarEntryRevision extends Revision {
+
         private String jarFileName;
         private String fileNameInJar;
         private long lastModified;
 
-        public JarEntryRevision(String jarFileName,
-                                String fileNameInJar,
-                                long lastModified) {
+        public JarEntryRevision(String jarFileName, String fileNameInJar, long lastModified) {
             if ((jarFileName == null) || (fileNameInJar == null)) {
-                throw new IllegalArgumentException(
-                    "JarFileName and FileNameInJar cannot be null");
+                throw new IllegalArgumentException("JarFileName and FileNameInJar cannot be null");
             }
             this.jarFileName = jarFileName;
             this.fileNameInJar = fileNameInJar;
             this.lastModified = lastModified;
         }
 
-        public boolean needsReloading()
-        {
+        public boolean needsReloading() {
             ZipEntry entry;
             try {
                 JarFile jarFile = new JarFile(this.jarFileName);
                 entry = jarFile.getEntry(this.fileNameInJar);
             }
-            catch(IOException e) {
+            catch (IOException e) {
                 entry = null;
             }
 
             if (entry != null) {
                 return (this.lastModified < entry.getTime());
-            }
-            else {
+            } else {
                 return false;
             }
         }
+
+        public static Revision build(URL fileUrl) {
+            // File within a Jar
+            // Find separator index of jar filename and filename within jar
+            try {
+                String fileName = fileUrl.toString();
+                int separatorIndex = fileName.indexOf(JAR_FILE_NAME_SEPARATOR);
+                // Split file name
+                String jarFileName = fileName.substring(JAR_FILE_NAME_PREFIX.length(), separatorIndex);
+                String fileNameInJar = fileName.substring(separatorIndex + JAR_FILE_NAME_SEPARATOR.length()).replaceAll("%20", " ");
+
+                JarFile jarFile = new JarFile(jarFileName);
+                ZipEntry entry = jarFile.getEntry(fileNameInJar);
+                return new JarEntryRevision(jarFileName.toString(), fileNameInJar, entry.getTime());
+            } catch (IOException e) {
+                return null;
+            }
+        }
     }
-    
+
 }

src/test/com/opensymphony/xwork2/config/providers/XmlConfigurationProviderTest.java

 import com.opensymphony.xwork2.util.FileManager;
 
 import java.io.File;
-import java.net.URL;
+import java.net.URI;
 
 
 public class XmlConfigurationProviderTest extends ConfigurationTestBase {
     }
 
     public void testEmptySpaces() throws Exception {
-        FileManager.setReloadingConfigs(true);
         final String filename = "com/opensymphony/xwork2/config/providers/xwork- test.xml";
-        ConfigurationProvider provider = buildConfigurationProvider(filename);
+        FileManager.setReloadingConfigs(true);
 
+        ConfigurationProvider provider = buildConfigurationProvider(filename);
         assertTrue(!provider.needsReload());
 
-        URL url = ClassLoaderUtil.getResource(filename, XmlConfigurationProvider.class);
-        File file = new File(getClass().getResource("/" + filename).toURI());
+        URI uri = ClassLoaderUtil.getResource(filename, ConfigurationProvider.class).toURI();
+
+        File file = new File(uri);
+
         assertTrue(file.exists());
         file.setLastModified(System.currentTimeMillis());
 
         assertTrue(provider.needsReload());
     }
 
+    public void testJaredFiles() throws Exception {
+        FileManager.setReloadingConfigs(true);
+        testProvider("xwork-jar.xml");
+        testProvider("xwork-zip.xml");
+        testProvider("xwork - jar.xml");
+        testProvider("xwork - zip.xml");
+
+        testProvider("xwork-jar2.xml");
+        testProvider("xwork-zip2.xml");
+        testProvider("xwork - jar2.xml");
+        testProvider("xwork - zip2.xml");
+    }
+
+    private void testProvider(String configFile) throws Exception {
+        ConfigurationProvider provider = buildConfigurationProvider(configFile);
+        assertTrue(!provider.needsReload());
+
+        String fullPath = ClassLoaderUtil.getResource(configFile, ConfigurationProvider.class).toString();
+
+        int startIndex = fullPath.indexOf(":file:/");
+        int endIndex = fullPath.indexOf("!/");
+
+        String jar = fullPath.substring(startIndex + ":file:/".length(), endIndex).replaceAll("%20", " ");
+
+        File file = new File(jar);
+
+        assertTrue(file.exists());
+        file.setLastModified(System.currentTimeMillis());
+
+        assertTrue(!provider.needsReload());
+    }
+
 }

src/test/xwork - jar.jar

Binary file added.

src/test/xwork - zip.zip

Binary file added.

src/test/xwork-jar.jar

Binary file added.

src/test/xwork-zip.zip

Binary file added.