Skip to content

Commit

Permalink
Pass Jar rather than URL to a JAR in the JarScanner call back for a JAR
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1742249 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed May 4, 2016
1 parent 80e37fa commit a80cf1b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
38 changes: 17 additions & 21 deletions java/org/apache/jasper/servlet/TldScanner.java
Expand Up @@ -47,7 +47,6 @@
import org.apache.tomcat.util.descriptor.tld.TaglibXml; import org.apache.tomcat.util.descriptor.tld.TaglibXml;
import org.apache.tomcat.util.descriptor.tld.TldParser; import org.apache.tomcat.util.descriptor.tld.TldParser;
import org.apache.tomcat.util.descriptor.tld.TldResourcePath; import org.apache.tomcat.util.descriptor.tld.TldResourcePath;
import org.apache.tomcat.util.scan.JarFactory;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;


/** /**
Expand Down Expand Up @@ -296,27 +295,24 @@ class TldScannerCallback implements JarScannerCallback {




@Override @Override
public void scan(URL jarUrl, String webappPath, boolean isWebapp) throws IOException { public void scan(Jar jar, String webappPath, boolean isWebapp) throws IOException {
boolean found = false; boolean found = false;
URL jarFileUrl; URL jarFileUrl = jar.getJarFileURL();
try (Jar jar = JarFactory.newInstance(jarUrl)) { jar.nextEntry();
jarFileUrl = jar.getJarFileURL(); for (String entryName = jar.getEntryName();
jar.nextEntry(); entryName != null;
for (String entryName = jar.getEntryName(); jar.nextEntry(), entryName = jar.getEntryName()) {
entryName != null; if (!(entryName.startsWith("META-INF/") &&
jar.nextEntry(), entryName = jar.getEntryName()) { entryName.endsWith(TLD_EXT))) {
if (!(entryName.startsWith("META-INF/") && continue;
entryName.endsWith(TLD_EXT))) { }
continue; found = true;
} TldResourcePath tldResourcePath =
found = true; new TldResourcePath(jarFileUrl, webappPath, entryName);
TldResourcePath tldResourcePath = try {
new TldResourcePath(jarFileUrl, webappPath, entryName); parseTld(tldResourcePath);
try { } catch (SAXException e) {
parseTld(tldResourcePath); throw new IOException(e);
} catch (SAXException e) {
throw new IOException(e);
}
} }
} }
if (found) { if (found) {
Expand Down
7 changes: 3 additions & 4 deletions java/org/apache/tomcat/JarScannerCallback.java
Expand Up @@ -18,7 +18,6 @@


import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL;


/** /**
* This interface is implemented by clients of the {@link JarScanner} to enable * This interface is implemented by clients of the {@link JarScanner} to enable
Expand All @@ -28,17 +27,17 @@ public interface JarScannerCallback {


/** /**
* A JAR was found and may be accessed for further processing via the * A JAR was found and may be accessed for further processing via the
* provided URL connection. * provided URL connection. The caller is responsible for closing the JAR.
* *
* @param jarURL The URL for the identified JAR * @param jar The JAR to process
* @param webappPath The path, if any, to the JAR within the web application * @param webappPath The path, if any, to the JAR within the web application
* @param isWebapp Indicates if the JAR was found within a web * @param isWebapp Indicates if the JAR was found within a web
* application. If <code>false</code> the JAR should * application. If <code>false</code> the JAR should
* be treated as being provided by the container * be treated as being provided by the container
* *
* @throws IOException if an I/O error occurs while scanning the JAR * @throws IOException if an I/O error occurs while scanning the JAR
*/ */
public void scan(URL jarURL, String webappPath, boolean isWebapp) public void scan(Jar jar, String webappPath, boolean isWebapp)
throws IOException; throws IOException;


/** /**
Expand Down
Expand Up @@ -26,7 +26,6 @@


import org.apache.tomcat.Jar; import org.apache.tomcat.Jar;
import org.apache.tomcat.JarScannerCallback; import org.apache.tomcat.JarScannerCallback;
import org.apache.tomcat.util.scan.JarFactory;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;


/** /**
Expand All @@ -51,9 +50,8 @@ public FragmentJarScannerCallback(WebXmlParser webXmlParser, boolean delegate,




@Override @Override
public void scan(URL jarUrl, String webappPath, boolean isWebapp) throws IOException { public void scan(Jar jar, String webappPath, boolean isWebapp) throws IOException {


Jar jar = null;
InputStream is = null; InputStream is = null;
WebXml fragment = new WebXml(); WebXml fragment = new WebXml();
fragment.setWebappJar(isWebapp); fragment.setWebappJar(isWebapp);
Expand All @@ -65,7 +63,6 @@ public void scan(URL jarUrl, String webappPath, boolean isWebapp) throws IOExcep
// web-fragment.xml files don't need to be parsed if they are never // web-fragment.xml files don't need to be parsed if they are never
// going to be used. // going to be used.
if (isWebapp && parseRequired) { if (isWebapp && parseRequired) {
jar = JarFactory.newInstance(jarUrl);
is = jar.getInputStream(FRAGMENT_LOCATION); is = jar.getInputStream(FRAGMENT_LOCATION);
} }


Expand All @@ -74,7 +71,6 @@ public void scan(URL jarUrl, String webappPath, boolean isWebapp) throws IOExcep
// distributable // distributable
fragment.setDistributable(true); fragment.setDistributable(true);
} else { } else {
@SuppressWarnings("null") // Cannot be null here
String fragmentUrl = jar.getURL(FRAGMENT_LOCATION); String fragmentUrl = jar.getURL(FRAGMENT_LOCATION);
InputSource source = new InputSource(fragmentUrl); InputSource source = new InputSource(fragmentUrl);
source.setByteStream(is); source.setByteStream(is);
Expand All @@ -83,14 +79,11 @@ public void scan(URL jarUrl, String webappPath, boolean isWebapp) throws IOExcep
} }
} }
} finally { } finally {
if (jar != null) { fragment.setURL(jar.getJarFileURL());
jar.close();
}
fragment.setURL(jarUrl);
if (fragment.getName() == null) { if (fragment.getName() == null) {
fragment.setName(fragment.getURL().toString()); fragment.setName(fragment.getURL().toString());
} }
fragment.setJarName(extractJarFileName(jarUrl)); fragment.setJarName(extractJarFileName(jar.getJarFileURL()));
fragments.put(fragment.getName(), fragment); fragments.put(fragment.getName(), fragment);
} }
} }
Expand All @@ -107,6 +100,7 @@ private String extractJarFileName(URL input) {
return url.substring(url.lastIndexOf('/') + 1); return url.substring(url.lastIndexOf('/') + 1);
} }



@Override @Override
public void scan(File file, String webappPath, boolean isWebapp) throws IOException { public void scan(File file, String webappPath, boolean isWebapp) throws IOException {


Expand Down
9 changes: 7 additions & 2 deletions java/org/apache/tomcat/util/scan/StandardJarScanner.java
Expand Up @@ -30,6 +30,7 @@


import org.apache.juli.logging.Log; import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory; import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.Jar;
import org.apache.tomcat.JarScanFilter; import org.apache.tomcat.JarScanFilter;
import org.apache.tomcat.JarScanType; import org.apache.tomcat.JarScanType;
import org.apache.tomcat.JarScanner; import org.apache.tomcat.JarScanner;
Expand Down Expand Up @@ -306,15 +307,19 @@ private void process(JarScanType scanType, JarScannerCallback callback,
} }


if ("jar".equals(url.getProtocol()) || url.getPath().endsWith(Constants.JAR_EXT)) { if ("jar".equals(url.getProtocol()) || url.getPath().endsWith(Constants.JAR_EXT)) {
callback.scan(url, webappPath, isWebapp); try (Jar jar = JarFactory.newInstance(url)) {
callback.scan(jar, webappPath, isWebapp);
}
} else if ("file".equals(url.getProtocol())) { } else if ("file".equals(url.getProtocol())) {
File f; File f;
try { try {
f = new File(url.toURI()); f = new File(url.toURI());
if (f.isFile() && isScanAllFiles()) { if (f.isFile() && isScanAllFiles()) {
// Treat this file as a JAR // Treat this file as a JAR
URL jarURL = UriUtil.buildJarUrl(f); URL jarURL = UriUtil.buildJarUrl(f);
callback.scan(jarURL, webappPath, isWebapp); try (Jar jar = JarFactory.newInstance(jarURL)) {
callback.scan(jar, webappPath, isWebapp);
}
} else if (f.isDirectory()) { } else if (f.isDirectory()) {
if (scanType == JarScanType.PLUGGABILITY) { if (scanType == JarScanType.PLUGGABILITY) {
callback.scan(f, webappPath, isWebapp); callback.scan(f, webappPath, isWebapp);
Expand Down
6 changes: 5 additions & 1 deletion test/org/apache/jasper/servlet/TestTldScanner.java
Expand Up @@ -29,7 +29,9 @@
import org.apache.catalina.Context; import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest; import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.Jar;
import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.scan.JarFactory;
import org.apache.tomcat.util.scan.StandardJarScanner; import org.apache.tomcat.util.scan.StandardJarScanner;
import org.easymock.EasyMock; import org.easymock.EasyMock;


Expand Down Expand Up @@ -109,7 +111,9 @@ private static void scan(TldScanner.TldScannerCallback callback, File webapp, St
throws Exception { throws Exception {
String fullPath = new File(webapp, path).toURI().toString(); String fullPath = new File(webapp, path).toURI().toString();
URL jarUrl = new URL("jar:" + fullPath + "!/"); URL jarUrl = new URL("jar:" + fullPath + "!/");
callback.scan(jarUrl, path, true); try (Jar jar = JarFactory.newInstance(jarUrl)) {
callback.scan(jar, path, true);
}
} }
} }


6 changes: 3 additions & 3 deletions test/org/apache/tomcat/util/scan/TestStandardJarScanner.java
Expand Up @@ -26,7 +26,7 @@


import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;

import org.apache.tomcat.Jar;
import org.apache.tomcat.JarScanType; import org.apache.tomcat.JarScanType;
import org.apache.tomcat.JarScannerCallback; import org.apache.tomcat.JarScannerCallback;
import org.apache.tomcat.unittest.TesterServletContext; import org.apache.tomcat.unittest.TesterServletContext;
Expand Down Expand Up @@ -98,9 +98,9 @@ private static class LoggingCallback implements JarScannerCallback {
List<String> callbacks = new ArrayList<>(); List<String> callbacks = new ArrayList<>();


@Override @Override
public void scan(URL jarUrl, String webappPath, public void scan(Jar jar, String webappPath,
boolean isWebapp) throws IOException { boolean isWebapp) throws IOException {
callbacks.add(jarUrl.toString() + "::" + webappPath + "::" + isWebapp); callbacks.add(jar.getJarFileURL().toString() + "::" + webappPath + "::" + isWebapp);
} }


@Override @Override
Expand Down

0 comments on commit a80cf1b

Please sign in to comment.