Commits

Anonymous committed f5ba475

Solved issues XW-669 and XW-674

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

Comments (0)

Files changed (4)

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

  */
 package com.opensymphony.xwork2.util;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 public class FileManager {
     //~ Static fields/initializers /////////////////////////////////////////////
 
+    private static Log LOG = LogFactory.getLog(FileManager.class);
+
     private static Map<String, Revision> files = Collections.synchronizedMap(new HashMap<String, Revision>());
     protected static boolean reloadingConfigs = true;
 
-    private static final String JAR_FILE_NAME_PREFIX = "jar:file:";
     private static final String JAR_FILE_NAME_SEPARATOR = "!/";
+    private static final String JAR_FILE_EXTENSION_END = ".jar/";
 
 
     //~ Constructors ///////////////////////////////////////////////////////////
         if (isReloadingConfigs()) {
             Revision revision;
 
-            if (fileName.startsWith(JAR_FILE_NAME_PREFIX)) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Creating revision for URL: " +fileName);
+            }
+            if (URLUtil.isJBoss5Url(fileUrl)) {
+                revision = JBossFileRevision.build(fileUrl);
+            } else if (URLUtil.isJarURL(fileUrl)) {
                 revision = JarEntryRevision.build(fileUrl);
             } else {
                 revision = FileRevision.build(fileUrl);
     }
 
     /**
+     * Represents file resource revision, used for vfszip://* resources
+     */
+    private static class JBossFileRevision extends FileRevision {
+
+        public JBossFileRevision(File file, long lastUpdated) {
+            super(file, lastUpdated);
+        }
+
+        public static Revision build(URL fileUrl) {
+            File file;
+            URL url = URLUtil.normalizeToFileProtocol(fileUrl);
+            try {
+                if (url != null) {
+                    file = new File(url.toURI());
+                } else {
+                    return null;
+                }
+            } catch (URISyntaxException e) {
+                file = new File(url.getPath());
+            }
+            if (file.exists() && file.canRead()) {
+                long lastModified = file.lastModified();
+                return new FileRevision(file, lastModified);
+            }
+            return null;
+        }
+    }
+
+    /**
      * Represents jar resurce revision, used for jar://* resource
      */
     private static class JarEntryRevision extends Revision {
             try {
                 String fileName = fileUrl.toString();
                 int separatorIndex = fileName.indexOf(JAR_FILE_NAME_SEPARATOR);
+                if (separatorIndex == -1) {
+                    separatorIndex = fileName.lastIndexOf(JAR_FILE_EXTENSION_END);
+                }
+                if (separatorIndex == -1) {
+                    LOG.warn("Could not find end of jar file!");
+                    return null;
+                }
                 // Split file name
-                String jarFileName = fileName.substring(JAR_FILE_NAME_PREFIX.length(), separatorIndex);
+                String jarFileName = fileName.substring(0, 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());
+                URL url = URLUtil.normalizeToFileProtocol(fileUrl);
+                if (url != null) {
+                    JarFile jarFile = new JarFile(new File(url.getPath().replaceAll("%20", " ")));
+                    ZipEntry entry = jarFile.getEntry(fileNameInJar);
+                    return new JarEntryRevision(jarFileName.toString(), fileNameInJar, entry.getTime());
+                } else {
+                    return null;
+                }
             } catch (Throwable e) {
+                LOG.warn("Could not create JarEntryRevision!", e);
                 return null;
             }
         }

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

  * Helper class to extract file paths from different urls
  */
 public class URLUtil {
-    public static final Pattern JAR_PATTERN = Pattern.compile("^(jar:|wsjar:|zip:|vfsfile:)?(file:)?(.*?)(\\!/)(.*)");
+
+    /**
+     * Prefix for Jar files in JBoss Virtual File System
+     */
+    public static final String JBOSS5_VFSZIP = "vfszip";
+
+    private static final Pattern JAR_PATTERN = Pattern.compile("^(jar:|wsjar:|zip:|vfsfile:|code-source:)?(file:)?(.*?)(\\!/|.jar/)(.*)");
     private static final int JAR_FILE_PATH = 3;
 
     /**
         String fileName = url.toExternalForm();
         Matcher jarMatcher = JAR_PATTERN.matcher(fileName);
         try {
-            if (jarMatcher.matches()) {
+            if (isJBoss5Url(url)){
+                return new URL("file", null, fileName.substring(JBOSS5_VFSZIP.length() + 1));
+            } else  if (jarMatcher.matches()) {
                 String path = jarMatcher.group(JAR_FILE_PATH);
                 return new URL("file", "", path);
             } else {
             return false;
         }
     }
+
+    /**
+     * Check if given URL is matching Jar pattern for different servers
+     * @param fileUrl
+     * @return
+     */
+    public static boolean isJarURL(URL fileUrl) {
+        Matcher jarMatcher = URLUtil.JAR_PATTERN.matcher(fileUrl.getPath());
+        return jarMatcher.matches(); 
+    }
+
+    /**
+     * Check if given URL is pointing to JBoss 5 VFS resource
+     * @param fileUrl
+     * @return
+     */
+    public static boolean isJBoss5Url(URL fileUrl) {
+        return JBOSS5_VFSZIP.equals(fileUrl.getProtocol());
+    }
+
 }

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

         FileManager.setReloadingConfigs(true);
         final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
         ConfigurationProvider provider = buildConfigurationProvider(filename);
-        
+
         assertTrue(!provider.needsReload());
-        
+
         File file = new File(getClass().getResource("/"+filename).getFile());
         assertTrue(file.exists());
         file.setLastModified(System.currentTimeMillis());
-        
+
         assertTrue(provider.needsReload());
     }
-    
+
     public void testInheritence() throws Exception {
         final String filename = "com/opensymphony/xwork2/config/providers/xwork-include-parent.xml";
         ConfigurationProvider provider = buildConfigurationProvider(filename);
         int startIndex = fullPath.indexOf(":file:/");
         int endIndex = fullPath.indexOf("!/");
 
-        String jar = fullPath.substring(startIndex + ":file:/".length() - 1, endIndex).replaceAll("%20", " ");
+        String jar = fullPath.substring(startIndex + ":file:/".length(), endIndex).replaceAll("%20", " ");
 
         File file = new File(jar);
 

src/test/com/opensymphony/xwork2/util/URLUtilTest.java

         url = new URL("vfsfile:c:/somefile.jar!/somestuf/bla/bla");
         outputURL = URLUtil.normalizeToFileProtocol(url);
         assertEquals("file:c:/somefile.jar", outputURL.toExternalForm());
+
+        url = new URL("vfszip:/c:/somefile.war/somelibrary.jar");
+        outputURL = URLUtil.normalizeToFileProtocol(url);
+        assertEquals("file:/c:/somefile.war/somelibrary.jar", outputURL.toExternalForm());
     }
 
     protected void setUp() throws Exception {
         assertEquals(true, URLUtil.verifyUrl("https://www.opensymphony.com:443/login"));
         assertEquals(true, URLUtil.verifyUrl("http://localhost:8080/myapp"));
     }
+
+    public void testIsJarURL() throws Exception {
+        assertTrue(URLUtil.isJarURL(new URL("jar:file:/c:/somelibrary.jar!/com/opensymphony")));
+        assertTrue(URLUtil.isJarURL(new URL("zip:/c:/somelibrary.jar!/com/opensymphony")));
+        assertTrue(URLUtil.isJarURL(new URL("wsjar:/c:/somelibrary.jar!/com/opensymphony")));
+        assertTrue(URLUtil.isJarURL(new URL("vfsfile:/c:/somelibrary.jar!/com/opensymphony")));
+        assertTrue(URLUtil.isJarURL(new URL("vfszip:/c:/somelibrary.jar/com/opensymphony")));
+    }
+
+    public void testIsJBoss5Url() throws Exception {
+        assertTrue(URLUtil.isJBoss5Url(new URL("vfszip:/c:/somewar.war/somelibrary.jar")));
+        assertFalse(URLUtil.isJBoss5Url(new URL("vfsfile:/c:/somewar.war/somelibrary.jar")));
+        assertFalse(URLUtil.isJBoss5Url(new URL("jar:file:/c:/somelibrary.jar")));
+    }
 }