You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/09/16 15:13:18 UTC

svn commit: r1523631 - in /tomcat/trunk/java/org/apache/catalina/webresources: AbstractArchiveResourceSet.java JarResourceSet.java JarWarResourceSet.java

Author: markt
Date: Mon Sep 16 13:13:18 2013
New Revision: 1523631

URL: http://svn.apache.org/r1523631
Log:
Refactor to reduce code duplication.

Added:
    tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java
      - copied, changed from r1523630, tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java
Modified:
    tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java
    tomcat/trunk/java/org/apache/catalina/webresources/JarWarResourceSet.java

Copied: tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java (from r1523630, tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java)
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java?p2=tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java&p1=tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java&r1=1523630&r2=1523631&rev=1523631&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java Mon Sep 16 13:13:18 2013
@@ -16,130 +16,22 @@
  */
 package org.apache.catalina.webresources;
 
-import java.io.File;
-import java.io.IOException;
 import java.io.InputStream;
-import java.net.MalformedURLException;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Set;
 import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
 
-import org.apache.catalina.LifecycleException;
-import org.apache.catalina.WebResource;
-import org.apache.catalina.WebResourceRoot;
 import org.apache.catalina.util.ResourceSet;
 
-/**
- * Represents a {@link org.apache.catalina.WebResourceSet} based on a JAR file.
- */
-public class JarResourceSet extends AbstractResourceSet {
+public abstract class AbstractArchiveResourceSet extends AbstractResourceSet {
 
     protected HashMap<String,JarEntry> jarFileEntries = new HashMap<>();
     protected String baseUrl;
 
-    /**
-     * A no argument constructor is required for this to work with the digester.
-     */
-    public JarResourceSet() {
-    }
-
-    /**
-     * Creates a new {@link org.apache.catalina.WebResourceSet} based on a JAR
-     * file.
-     *
-     * @param root          The {@link WebResourceRoot} this new
-     *                          {@link org.apache.catalina.WebResourceSet} will
-     *                          be added to.
-     * @param base          The absolute path to the JAR file on the file system
-     *                          from which the resources will be served.
-     * @param webAppMount   The path within the web application at which this
-     *                          {@link org.apache.catalina.WebResourceSet} will
-     *                          be mounted.
-     * @param internalPath  The path within this new {@link
-     *                          org.apache.catalina.WebResourceSet} where
-     *                          resources will be served from. E.g. for a
-     *                          resource JAR, this would be "META-INF/resources"
-     */
-    public JarResourceSet(WebResourceRoot root, String base, String webAppMount,
-            String internalPath) throws IllegalArgumentException {
-        setRoot(root);
-        setBase(base);
-        setInternalPath(internalPath);
-        setWebAppMount(webAppMount);
-
-        if (getRoot().getState().isAvailable()) {
-            try {
-                start();
-            } catch (LifecycleException e) {
-                throw new IllegalStateException(e);
-            }
-        }
-    }
-
-    @Override
-    public WebResource getResource(String path) {
-        checkPath(path);
-        String webAppMount = getWebAppMount();
-        WebResourceRoot root = getRoot();
-
-        /*
-         * Implementation notes
-         *
-         * The path parameter passed into this method always starts with '/'.
-         *
-         * The path parameter passed into this method may or may not end with a
-         * '/'. JarFile.getEntry() will return a matching directory entry
-         * whether or not the name ends in a '/'. However, if the entry is
-         * requested without the '/' subsequent calls to JarEntry.isDirectory()
-         * will return false.
-         *
-         * Paths in JARs never start with '/'. Leading '/' need to be removed
-         * before any JarFile.getEntry() call.
-         */
-
-        // If the JAR has been mounted below the web application root, return
-        // an empty resource for requests outside of the mount point.
-
-        if (path.startsWith(webAppMount)) {
-            String pathInJar = getInternalPath() + path.substring(
-                    webAppMount.length(), path.length());
-            // Always strip off the leading '/' to get the JAR path
-            if (pathInJar.charAt(0) == '/') {
-                pathInJar = pathInJar.substring(1);
-            }
-            if (pathInJar.equals("")) {
-                // Special case
-                return new JarResourceRoot(root, new File(getBase()),
-                        pathInJar, path);
-            } else {
-                JarEntry jarEntry = null;
-                if (!(pathInJar.charAt(pathInJar.length() - 1) == '/')) {
-                    jarEntry = jarFileEntries.get(pathInJar + '/');
-                    if (jarEntry != null) {
-                        path = path + '/';
-                    }
-                }
-                if (jarEntry == null) {
-                    jarEntry = jarFileEntries.get(pathInJar);
-                }
-                if (jarEntry == null) {
-                    return new EmptyResource(root, path);
-                } else {
-                    return new JarResource(root, getBase(), baseUrl, jarEntry,
-                            getInternalPath(), path);
-                }
-            }
-        } else {
-            return new EmptyResource(root, path);
-        }
-    }
-
     @Override
-    public String[] list(String path) {
+    public final String[] list(String path) {
         checkPath(path);
         String webAppMount = getWebAppMount();
 
@@ -191,7 +83,7 @@ public class JarResourceSet extends Abst
     }
 
     @Override
-    public Set<String> listWebAppPaths(String path) {
+    public final Set<String> listWebAppPaths(String path) {
         checkPath(path);
         String webAppMount = getWebAppMount();
 
@@ -240,14 +132,14 @@ public class JarResourceSet extends Abst
     }
 
     @Override
-    public boolean mkdir(String path) {
+    public final boolean mkdir(String path) {
         checkPath(path);
 
         return false;
     }
 
     @Override
-    public boolean write(String path, InputStream is, boolean overwrite) {
+    public final boolean write(String path, InputStream is, boolean overwrite) {
         checkPath(path);
 
         if (is == null) {
@@ -257,25 +149,4 @@ public class JarResourceSet extends Abst
 
         return false;
     }
-
-    //-------------------------------------------------------- Lifecycle methods
-    @Override
-    protected void initInternal() throws LifecycleException {
-
-        try (JarFile jarFile = new JarFile(getBase())) {
-            Enumeration<JarEntry> entries = jarFile.entries();
-            while (entries.hasMoreElements()) {
-                JarEntry entry = entries.nextElement();
-                jarFileEntries.put(entry.getName(), entry);
-            }
-        } catch (IOException ioe) {
-            throw new IllegalArgumentException(ioe);
-        }
-
-        try {
-            this.baseUrl = (new File(getBase())).toURI().toURL().toString();
-        } catch (MalformedURLException e) {
-            throw new IllegalArgumentException(e);
-        }
-    }
 }

Modified: tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java?rev=1523631&r1=1523630&r2=1523631&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/JarResourceSet.java Mon Sep 16 13:13:18 2013
@@ -18,25 +18,20 @@ package org.apache.catalina.webresources
 
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.MalformedURLException;
-import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.WebResource;
 import org.apache.catalina.WebResourceRoot;
-import org.apache.catalina.util.ResourceSet;
 
 /**
  * Represents a {@link org.apache.catalina.WebResourceSet} based on a JAR file.
  */
-public class JarResourceSet extends AbstractResourceSet {
+public class JarResourceSet extends AbstractArchiveResourceSet {
 
     protected HashMap<String,JarEntry> jarFileEntries = new HashMap<>();
     protected String baseUrl;
@@ -138,126 +133,6 @@ public class JarResourceSet extends Abst
         }
     }
 
-    @Override
-    public String[] list(String path) {
-        checkPath(path);
-        String webAppMount = getWebAppMount();
-
-        ArrayList<String> result = new ArrayList<>();
-        if (path.startsWith(webAppMount)) {
-            String pathInJar =
-                    getInternalPath() + path.substring(webAppMount.length());
-            // Always strip off the leading '/' to get the JAR path
-            if (pathInJar.charAt(0) == '/') {
-                pathInJar = pathInJar.substring(1);
-            }
-            Iterator<String> entries = jarFileEntries.keySet().iterator();
-            while (entries.hasNext()) {
-                String name = entries.next();
-                if (name.length() > pathInJar.length() &&
-                        name.startsWith(pathInJar)) {
-                    if (name.charAt(name.length() - 1) == '/') {
-                        name = name.substring(
-                                pathInJar.length(), name.length() - 1);
-                    } else {
-                        name = name.substring(pathInJar.length());
-                    }
-                    if (name.length() == 0) {
-                        continue;
-                    }
-                    if (name.charAt(0) == '/') {
-                        name = name.substring(1);
-                    }
-                    if (name.length() > 0 && name.lastIndexOf('/') == -1) {
-                        result.add(name);
-                    }
-                }
-            }
-        } else {
-            if (!path.endsWith("/")) {
-                path = path + "/";
-            }
-            if (webAppMount.startsWith(path)) {
-                int i = webAppMount.indexOf('/', path.length());
-                if (i == -1) {
-                    return new String[] {webAppMount.substring(path.length())};
-                } else {
-                    return new String[] {
-                            webAppMount.substring(path.length(), i)};
-                }
-            }
-        }
-        return result.toArray(new String[result.size()]);
-    }
-
-    @Override
-    public Set<String> listWebAppPaths(String path) {
-        checkPath(path);
-        String webAppMount = getWebAppMount();
-
-        ResourceSet<String> result = new ResourceSet<>();
-        if (path.startsWith(webAppMount)) {
-            String pathInJar =
-                    getInternalPath() + path.substring(webAppMount.length());
-            // Always strip off the leading '/' to get the JAR path and make
-            // sure it ends in '/'
-            if (pathInJar.charAt(pathInJar.length() - 1) != '/') {
-                pathInJar = pathInJar.substring(1) + '/';
-            }
-            if (pathInJar.charAt(0) == '/') {
-                pathInJar = pathInJar.substring(1);
-            }
-
-            Iterator<String> entries = jarFileEntries.keySet().iterator();
-            while (entries.hasNext()) {
-                String name = entries.next();
-                if (name.length() > pathInJar.length() &&
-                        name.startsWith(pathInJar)) {
-                    int nextSlash = name.indexOf('/', pathInJar.length());
-                    if (nextSlash == -1 || nextSlash == name.length() - 1) {
-                        if (name.startsWith(pathInJar)) {
-                            result.add(webAppMount + '/' +
-                                    name.substring(getInternalPath().length()));
-                        }
-                    }
-                }
-            }
-        } else {
-            if (!path.endsWith("/")) {
-                path = path + "/";
-            }
-            if (webAppMount.startsWith(path)) {
-                int i = webAppMount.indexOf('/', path.length());
-                if (i == -1) {
-                    result.add(webAppMount + "/");
-                } else {
-                    result.add(webAppMount.substring(0, i + 1));
-                }
-            }
-        }
-        result.setLocked(true);
-        return result;
-    }
-
-    @Override
-    public boolean mkdir(String path) {
-        checkPath(path);
-
-        return false;
-    }
-
-    @Override
-    public boolean write(String path, InputStream is, boolean overwrite) {
-        checkPath(path);
-
-        if (is == null) {
-            throw new NullPointerException(
-                    sm.getString("dirResourceSet.writeNpe"));
-        }
-
-        return false;
-    }
-
     //-------------------------------------------------------- Lifecycle methods
     @Override
     protected void initInternal() throws LifecycleException {

Modified: tomcat/trunk/java/org/apache/catalina/webresources/JarWarResourceSet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/JarWarResourceSet.java?rev=1523631&r1=1523630&r2=1523631&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/JarWarResourceSet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/JarWarResourceSet.java Mon Sep 16 13:13:18 2013
@@ -33,7 +33,7 @@ import org.apache.catalina.WebResourceRo
  * that is nested inside a packed WAR file. This is only intended for internal
  * use within Tomcat and therefore cannot be created via configuration.
  */
-public class JarWarResourceSet extends JarResourceSet {
+public class JarWarResourceSet extends AbstractArchiveResourceSet {
 
     private final String archivePath;
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org