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:57 UTC

svn commit: r1523632 - in /tomcat/trunk/java/org/apache/catalina/webresources: AbstractArchiveResource.java JarResource.java JarResourceSet.java JarWarResource.java JarWarResourceSet.java

Author: markt
Date: Mon Sep 16 13:13:56 2013
New Revision: 1523632

URL: http://svn.apache.org/r1523632
Log:
Further refactoring to support resource JARs in packed WARs

Added:
    tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResource.java
      - copied, changed from r1523631, tomcat/trunk/java/org/apache/catalina/webresources/JarResource.java
    tomcat/trunk/java/org/apache/catalina/webresources/JarWarResource.java
Modified:
    tomcat/trunk/java/org/apache/catalina/webresources/JarResource.java
    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/AbstractArchiveResource.java (from r1523631, tomcat/trunk/java/org/apache/catalina/webresources/JarResource.java)
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResource.java?p2=tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResource.java&p1=tomcat/trunk/java/org/apache/catalina/webresources/JarResource.java&r1=1523631&r2=1523632&rev=1523632&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/JarResource.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResource.java Mon Sep 16 13:13:56 2013
@@ -18,50 +18,20 @@ package org.apache.catalina.webresources
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.net.URL;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
 import org.apache.catalina.WebResourceRoot;
-import org.apache.juli.logging.Log;
-import org.apache.juli.logging.LogFactory;
 
-/**
- * Represents a single resource (file or directory) that is located within a
- * JAR.
- */
-public class JarResource extends AbstractResource {
-
-    private static final Log log = LogFactory.getLog(JarResource.class);
-
-    private final String base;
-    private final String baseUrl;
-    private final JarEntry resource;
-    private final String name;
+public abstract class AbstractArchiveResource extends AbstractResource {
 
-    public JarResource(WebResourceRoot root, String base, String baseUrl,
-            JarEntry jarEntry, String internalPath, String webAppPath) {
+    protected final JarEntry resource;
+    protected String name;
+
+    protected AbstractArchiveResource(WebResourceRoot root, String webAppPath,
+            JarEntry jarEntry) {
         super(root, webAppPath);
-        this.base = base;
-        this.baseUrl = "jar:" + baseUrl;
         this.resource = jarEntry;
-
-        String resourceName = resource.getName();
-        if (resourceName.charAt(resourceName.length() - 1) == '/') {
-            resourceName = resourceName.substring(0, resourceName.length() - 1);
-        }
-        if (internalPath.length() > 0 && resourceName.equals(
-                internalPath.subSequence(1, internalPath.length()))) {
-            name = "";
-        } else {
-            int index = resourceName.lastIndexOf('/');
-            if (index == -1) {
-                name = resourceName;
-            } else {
-                name = resourceName.substring(index + 1);
-            }
-        }
     }
 
     @Override
@@ -115,44 +85,12 @@ public class JarResource extends Abstrac
     }
 
     @Override
-    public InputStream getInputStream() {
-        try {
-            JarFile jarFile = new JarFile(base);
-            InputStream is = jarFile.getInputStream(resource);
-            return new JarInputStreamWrapper(jarFile, is);
-        } catch (IOException e) {
-            if (log.isDebugEnabled()) {
-                log.debug(sm.getString("fileResource.getInputStreamFail",
-                        resource.getName(), baseUrl), e);
-            }
-            return null;
-        }
-    }
-
-    @Override
     public long getCreation() {
         return resource.getTime();
     }
 
-    @Override
-    public URL getURL() {
-        try {
-            return new URL(baseUrl + "!/" + resource.getName());
-        } catch (MalformedURLException e) {
-            if (log.isDebugEnabled()) {
-                log.debug(sm.getString("fileResource.getUrlFail",
-                        resource.getName(), baseUrl), e);
-            }
-            return null;
-        }
-    }
-
-    @Override
-    protected Log getLog() {
-        return log;
-    }
 
-    private static class JarInputStreamWrapper extends InputStream {
+    protected static class JarInputStreamWrapper extends InputStream {
 
         private final JarFile jarFile;
         private final InputStream is;

Modified: tomcat/trunk/java/org/apache/catalina/webresources/JarResource.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/JarResource.java?rev=1523632&r1=1523631&r2=1523632&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/JarResource.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/JarResource.java Mon Sep 16 13:13:56 2013
@@ -31,21 +31,18 @@ import org.apache.juli.logging.LogFactor
  * Represents a single resource (file or directory) that is located within a
  * JAR.
  */
-public class JarResource extends AbstractResource {
+public class JarResource extends AbstractArchiveResource {
 
     private static final Log log = LogFactory.getLog(JarResource.class);
 
     private final String base;
     private final String baseUrl;
-    private final JarEntry resource;
-    private final String name;
 
     public JarResource(WebResourceRoot root, String base, String baseUrl,
             JarEntry jarEntry, String internalPath, String webAppPath) {
-        super(root, webAppPath);
+        super(root, webAppPath, jarEntry);
         this.base = base;
         this.baseUrl = "jar:" + baseUrl;
-        this.resource = jarEntry;
 
         String resourceName = resource.getName();
         if (resourceName.charAt(resourceName.length() - 1) == '/') {
@@ -65,56 +62,6 @@ public class JarResource extends Abstrac
     }
 
     @Override
-    public long getLastModified() {
-        return resource.getTime();
-    }
-
-    @Override
-    public boolean exists() {
-        return true;
-    }
-
-    @Override
-    public boolean isVirtual() {
-        return false;
-    }
-
-    @Override
-    public boolean isDirectory() {
-        return resource.isDirectory();
-    }
-
-    @Override
-    public boolean isFile() {
-        return !resource.isDirectory();
-    }
-
-    @Override
-    public boolean delete() {
-        return false;
-    }
-
-    @Override
-    public String getName() {
-        return name;
-    }
-
-    @Override
-    public long getContentLength() {
-        return resource.getSize();
-    }
-
-    @Override
-    public String getCanonicalPath() {
-        return null;
-    }
-
-    @Override
-    public boolean canRead() {
-        return true;
-    }
-
-    @Override
     public InputStream getInputStream() {
         try {
             JarFile jarFile = new JarFile(base);
@@ -130,11 +77,6 @@ public class JarResource extends Abstrac
     }
 
     @Override
-    public long getCreation() {
-        return resource.getTime();
-    }
-
-    @Override
     public URL getURL() {
         try {
             return new URL(baseUrl + "!/" + resource.getName());
@@ -151,72 +93,4 @@ public class JarResource extends Abstrac
     protected Log getLog() {
         return log;
     }
-
-    private static class JarInputStreamWrapper extends InputStream {
-
-        private final JarFile jarFile;
-        private final InputStream is;
-
-
-        public JarInputStreamWrapper(JarFile jarFile, InputStream is) {
-            this.jarFile = jarFile;
-            this.is = is;
-        }
-
-
-        @Override
-        public int read() throws IOException {
-            return is.read();
-        }
-
-
-        @Override
-        public int read(byte[] b) throws IOException {
-            return is.read(b);
-        }
-
-
-        @Override
-        public int read(byte[] b, int off, int len) throws IOException {
-            return is.read(b, off, len);
-        }
-
-
-        @Override
-        public long skip(long n) throws IOException {
-            return is.skip(n);
-        }
-
-
-        @Override
-        public int available() throws IOException {
-            return is.available();
-        }
-
-
-        @Override
-        public void close() throws IOException {
-            // Closing the JarFile releases the file lock on the JAR and also
-            // closes all input streams created from the JarFile.
-            jarFile.close();
-        }
-
-
-        @Override
-        public synchronized void mark(int readlimit) {
-            is.mark(readlimit);
-        }
-
-
-        @Override
-        public synchronized void reset() throws IOException {
-            is.reset();
-        }
-
-
-        @Override
-        public boolean markSupported() {
-            return is.markSupported();
-        }
-    }
 }

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=1523632&r1=1523631&r2=1523632&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:56 2013
@@ -20,7 +20,6 @@ import java.io.File;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.util.Enumeration;
-import java.util.HashMap;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
@@ -33,7 +32,6 @@ import org.apache.catalina.WebResourceRo
  */
 public class JarResourceSet extends AbstractArchiveResourceSet {
 
-    protected HashMap<String,JarEntry> jarFileEntries = new HashMap<>();
     protected String baseUrl;
 
     /**

Added: tomcat/trunk/java/org/apache/catalina/webresources/JarWarResource.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/JarWarResource.java?rev=1523632&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/JarWarResource.java (added)
+++ tomcat/trunk/java/org/apache/catalina/webresources/JarWarResource.java Mon Sep 16 13:13:56 2013
@@ -0,0 +1,123 @@
+/*
+ * 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.
+ */
+package org.apache.catalina.webresources;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
+
+import org.apache.catalina.WebResourceRoot;
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+
+/**
+ * Represents a single resource (file or directory) that is located within a
+ * JAR that in turn is located in a WAR file.
+ */
+public class JarWarResource extends AbstractArchiveResource {
+
+    private static final Log log = LogFactory.getLog(JarResource.class);
+
+    private final String base;
+    private final String baseUrl;
+    private final String archivePath;
+
+    public JarWarResource(WebResourceRoot root, String base, String baseUrl,
+            JarEntry jarEntry, String archivePath, String internalPath,
+            String webAppPath) {
+        super(root, webAppPath, jarEntry);
+        this.base = base;
+        this.archivePath = archivePath;
+        this.baseUrl = "jar:" + baseUrl;
+
+        String resourceName = resource.getName();
+        if (resourceName.charAt(resourceName.length() - 1) == '/') {
+            resourceName = resourceName.substring(0, resourceName.length() - 1);
+        }
+        if (internalPath.length() > 0 && resourceName.equals(
+                internalPath.subSequence(1, internalPath.length()))) {
+            name = "";
+        } else {
+            int index = resourceName.lastIndexOf('/');
+            if (index == -1) {
+                name = resourceName;
+            } else {
+                name = resourceName.substring(index + 1);
+            }
+        }
+    }
+
+    @Override
+    public InputStream getInputStream() {
+        try {
+            JarFile warFile = new JarFile(base);
+            JarEntry jarFileInWar = warFile.getJarEntry(archivePath);
+            InputStream isInWar = warFile.getInputStream(jarFileInWar);
+
+            JarInputStream jarIs = new JarInputStream(isInWar);
+            JarEntry entry = jarIs.getNextJarEntry();
+            while (entry != null &&
+                    !entry.getName().equals(resource.getName())) {
+                entry = jarIs.getNextJarEntry();
+            }
+
+            if (entry == null) {
+                try {
+                    jarIs.close();
+                } catch (IOException ioe) {
+                    // Ignore
+                }
+                try {
+                    warFile.close();
+                } catch (IOException ioe) {
+                    // Ignore
+                }
+                return null;
+            }
+
+            return new JarInputStreamWrapper(warFile, jarIs);
+        } catch (IOException e) {
+            if (log.isDebugEnabled()) {
+                log.debug(sm.getString("fileResource.getInputStreamFail",
+                        resource.getName(), baseUrl), e);
+            }
+            return null;
+        }
+    }
+
+    @Override
+    public URL getURL() {
+        try {
+            return new URL(baseUrl + "!/" + resource.getName());
+        } catch (MalformedURLException e) {
+            if (log.isDebugEnabled()) {
+                log.debug(sm.getString("fileResource.getUrlFail",
+                        resource.getName(), baseUrl), e);
+            }
+            return null;
+        }
+    }
+
+    @Override
+    protected Log getLog() {
+        return log;
+    }
+}

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=1523632&r1=1523631&r2=1523632&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:56 2013
@@ -123,8 +123,8 @@ public class JarWarResourceSet extends A
                 if (jarEntry == null) {
                     return new EmptyResource(root, path);
                 } else {
-                    return new JarResource(root, getBase(), baseUrl, jarEntry,
-                            getInternalPath(), path);
+                    return new JarWarResource(root, getBase(), baseUrl, jarEntry,
+                            archivePath, getInternalPath(), path);
                 }
             }
         } else {



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