Merge remote-tracking branch 'origin/jetty-9.4.x'

This commit is contained in:
Jan Bartel 2017-11-16 15:32:41 +01:00
commit 406ebd9fe4
9 changed files with 358 additions and 57 deletions

View File

@ -46,6 +46,7 @@ import javax.servlet.annotation.HandlesTypes;
import org.eclipse.jetty.annotations.AnnotationParser.Handler;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.util.JavaVersion;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
@ -77,7 +78,7 @@ public class AnnotationConfiguration extends AbstractConfiguration
public static final String CONTAINER_INITIALIZER_STARTER = "org.eclipse.jetty.containerInitializerStarter";
public static final String MULTI_THREADED = "org.eclipse.jetty.annotations.multiThreaded";
public static final String MAX_SCAN_WAIT = "org.eclipse.jetty.annotations.maxWait";
public static final String JAVA_TARGET_PLATFORM = "org.eclipse.jetty.javaTargetPlatform";
public static final int DEFAULT_MAX_SCAN_WAIT = 60; /* time in sec */
public static final boolean DEFAULT_MULTI_THREADED = true;
@ -434,7 +435,7 @@ public class AnnotationConfiguration extends AbstractConfiguration
throws Exception
{
int javaPlatform = 0;
Object target = context.getAttribute(JAVA_TARGET_PLATFORM);
Object target = context.getAttribute(JavaVersion.JAVA_TARGET_PLATFORM);
if (target!=null)
javaPlatform = Integer.valueOf(target.toString());
AnnotationParser parser = createAnnotationParser(javaPlatform);

View File

@ -45,6 +45,17 @@ public class AntMetaInfConfiguration extends MetaInfConfiguration
{
return MetaInfConfiguration.class;
}
@Override
public void findAndFilterContainerPaths(WebAppContext context) throws Exception
{
// TODO Auto-generated method stub
super.findAndFilterContainerPaths(context);
}
/* ------------------------------------------------------------------------------- */
@Override

View File

@ -27,6 +27,13 @@ import java.util.regex.Pattern;
*/
public class JavaVersion
{
/**
* Context attribute that can be set to target a different version of the jvm than the current runtime.
* Acceptable values should correspond to those returned by JavaVersion.getPlatform().
*/
public static final String JAVA_TARGET_PLATFORM = "org.eclipse.jetty.javaTargetPlatform";
// Copy of version in jetty-start
private static final Pattern PRE_JDK9 = Pattern.compile("1\\.(\\d)(\\.(\\d+)(_(\\d+))?)?(-.+)?");

View File

@ -38,6 +38,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useManifestOnlyJar>false</useManifestOnlyJar>
<excludes>
<exclude>org.eclipse.jetty.webapp.WebAppClassLoaderUrlStreamTest</exclude>
</excludes>
@ -87,4 +88,22 @@
<optional>true</optional>
</dependency>
</dependencies>
<profiles>
<profile>
<id>jdk9</id>
<activation>
<jdk>[1.9,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} --module-path src/test/resources/mods</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -43,6 +43,7 @@ import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import org.eclipse.jetty.util.JavaVersion;
import org.eclipse.jetty.util.PatternMatcher;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -83,7 +84,86 @@ public class MetaInfConfiguration extends AbstractConfiguration
public static final String WEBINF_JAR_PATTERN = "org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern";
public static final List<String> __allScanTypes = (List<String>) Arrays.asList(METAINF_TLDS, METAINF_RESOURCES, METAINF_FRAGMENTS);
/**
* ContainerPathNameMatcher
*
* Matches names of jars on the container classpath
* against a pattern. If no pattern is specified, no
* jars match.
*/
public class ContainerPathNameMatcher extends PatternMatcher
{
protected final WebAppContext _context;
protected final String _pattern;
public ContainerPathNameMatcher(WebAppContext context, String pattern)
{
if (context == null)
throw new IllegalArgumentException("Context null");
_context = context;
_pattern = pattern;
}
public void match (List<URI> uris)
throws Exception
{
if (uris == null)
return;
match(_pattern, uris.toArray(new URI[uris.size()]), false);
}
/**
* @see org.eclipse.jetty.util.PatternMatcher#matched(java.net.URI)
*/
@Override
public void matched(URI uri) throws Exception
{
_context.getMetaData().addContainerResource(Resource.newResource(uri));
}
}
/**
* WebAppPathNameMatcher
*
* Matches names of jars or dirs on the webapp classpath
* against a pattern. If there is no pattern, all jars or dirs
* will match.
*/
public class WebAppPathNameMatcher extends PatternMatcher
{
protected final WebAppContext _context;
protected final String _pattern;
public WebAppPathNameMatcher (WebAppContext context, String pattern)
{
if (context == null)
throw new IllegalArgumentException("Context null");
_context=context;
_pattern=pattern;
}
public void match (List<URI> uris)
throws Exception
{
match(_pattern, uris.toArray(new URI[uris.size()]), true);
}
/**
* @see org.eclipse.jetty.util.PatternMatcher#matched(java.net.URI)
*/
@Override
public void matched(URI uri) throws Exception
{
_context.getMetaData().addWebInfJar(Resource.newResource(uri));
}
}
/**
* If set, to a list of URLs, these resources are added to the context
* resource base as a resource collection.
@ -97,6 +177,146 @@ public class MetaInfConfiguration extends AbstractConfiguration
}
/* ------------------------------------------------------------------------------- */
@Override
public void preConfigure(final WebAppContext context) throws Exception
{
//find container jars/modules and select which ones to scan
findAndFilterContainerPaths(context);
//find web-app jars and select which ones to scan
findAndFilterWebAppPaths(context);
//No pattern to appy to classes, just add to metadata
context.getMetaData().setWebInfClassesDirs(findClassDirs(context));
scanJars(context);
}
/* ------------------------------------------------------------------------------- */
/**
* Find jars and directories that are on the container's classpath
* and apply an optional filter. The filter is a pattern applied to the
* full jar or directory names. If there is no pattern, then no jar
* or dir is considered to match.
*
* Those jars that do match will be later examined for META-INF
* information and annotations.
*
* To find them, examine the classloaders in the hierarchy above the
* webapp classloader that are URLClassLoaders. For jdk-9 we also
* look at the java.class.path, and the jdk.module.path.
*
* @param context the WebAppContext being deployed
* @throws Exception
*/
public void findAndFilterContainerPaths (final WebAppContext context)
throws Exception
{
//assume the target jvm is the same as that running
int targetPlatform = JavaVersion.VERSION.getPlatform();
//allow user to specify target jvm different to current runtime
Object target = context.getAttribute(JavaVersion.JAVA_TARGET_PLATFORM);
if (target!=null)
targetPlatform = Integer.valueOf(target.toString()).intValue();
//Apply an initial name filter to the jars to select which will be eventually
//scanned for META-INF info and annotations. The filter is based on inclusion patterns.
ContainerPathNameMatcher containerPathNameMatcher = new ContainerPathNameMatcher(context, (String)context.getAttribute(CONTAINER_JAR_PATTERN));
List<URI> containerUris = getAllContainerJars(context);
if (LOG.isDebugEnabled()) LOG.debug("Matching container urls {}", containerUris);
containerPathNameMatcher.match(containerUris);
//if running on jvm 9 or above, we we won't be able to look at the application classloader
//to extract urls, so we need to examine the classpath instead.
if (JavaVersion.VERSION.getPlatform() >= 9)
{
String tmp = System.getProperty("java.class.path");
if (tmp != null)
{
List<URI> cpUris = new ArrayList<>();
String[] entries = tmp.split(File.pathSeparator);
for (String entry:entries)
{
File f = new File(entry);
cpUris.add(f.toURI());
}
if (LOG.isDebugEnabled()) LOG.debug("Matching java.class.path {}", cpUris);
containerPathNameMatcher.match(cpUris);
}
}
//if we're targetting jdk 9 or above, we also need to examine the
//module path
if (targetPlatform >= 9)
{
//TODO need to consider the jdk.module.upgrade.path - how to resolve
//which modules will be actually used. If its possible, it can
//only be attempted in jetty-10 with jdk-9 specific apis.
String tmp = System.getProperty("jdk.module.path");
if (tmp != null)
{
List<URI> moduleUris = new ArrayList<>();
String[] entries = tmp.split(File.pathSeparator);
for (String entry:entries)
{
File dir = new File(entry);
File[] files = dir.listFiles();
if (files != null)
{
for (File f:files)
{
moduleUris.add(f.toURI());
}
}
}
if (LOG.isDebugEnabled()) LOG.debug("Matching jdk.module.path {}", moduleUris);
containerPathNameMatcher.match(moduleUris);
}
}
if (LOG.isDebugEnabled()) LOG.debug("Container paths selected:{}", context.getMetaData().getContainerResources());
}
/* ------------------------------------------------------------------------------- */
/**
* Finds the jars that are either physically or virtually in
* WEB-INF/lib, and applies an optional filter to their full
* pathnames.
*
* The filter selects which jars will later be examined for META-INF
* information and annotations. If there is no pattern, then
* all jars are considered selected.
*
* @param context the WebAppContext being deployed
* @throws Exception
*/
public void findAndFilterWebAppPaths (WebAppContext context)
throws Exception
{
//Apply filter to WEB-INF/lib jars
WebAppPathNameMatcher matcher = new WebAppPathNameMatcher(context, (String)context.getAttribute(WEBINF_JAR_PATTERN));
List<Resource> jars = findJars(context);
//Convert to uris for matching
if (jars != null)
{
List<URI> uris = new ArrayList<>();
int i=0;
for (Resource r: jars)
{
uris.add(r.getURI());
}
matcher.match(uris);
}
}
/* ------------------------------------------------------------------------------- */
protected List<URI> getAllContainerJars(final WebAppContext context) throws URISyntaxException
{
@ -120,51 +340,6 @@ public class MetaInfConfiguration extends AbstractConfiguration
}
/* ------------------------------------------------------------------------------- */
@Override
public void preConfigure(final WebAppContext context) throws Exception
{
// discover matching container jars
if (context.getClassLoader() != null)
{
List<URI> uris = getAllContainerJars(context);
new PatternMatcher ()
{
public void matched(URI uri) throws Exception
{
context.getMetaData().addContainerResource(Resource.newResource(uri));
}
}.match((String)context.getAttribute(CONTAINER_JAR_PATTERN),
uris.toArray(new URI[uris.size()]),
false);
}
//Discover matching WEB-INF/lib jars
List<Resource> jars = findJars(context);
if (jars!=null)
{
List<URI> uris = jars.stream().map(Resource::getURI).collect(Collectors.toList());
new PatternMatcher ()
{
@Override
public void matched(URI uri) throws Exception
{
context.getMetaData().addWebInfJar(Resource.newResource(uri));
}
}.match((String)context.getAttribute(WEBINF_JAR_PATTERN),
uris.toArray(new URI[uris.size()]),
true);
}
//No pattern to appy to classes, just add to metadata
context.getMetaData().setWebInfClassesDirs(findClassDirs(context));
scanJars(context);
}
protected void scanJars (WebAppContext context) throws Exception
{
boolean useContainerCache = DEFAULT_USE_CONTAINER_METAINF_CACHE;
@ -196,21 +371,22 @@ public class MetaInfConfiguration extends AbstractConfiguration
scanJars(context, context.getMetaData().getWebInfJars(), false, scanTypes);
}
/**
* For backwards compatibility. This method will always scan for all types of data.
*
/* ------------------------------------------------------------------------------- */
/**
* For backwards compatibility. This method will always scan for all types of data.
*
* @param context the context for the scan
* @param jars the jars to scan
* @param useCaches if true, the scanned info is cached
* @throws Exception
*/
public void scanJars (final WebAppContext context, Collection<Resource> jars, boolean useCaches)
throws Exception
throws Exception
{
scanJars(context, jars, useCaches, __allScanTypes);
}
/* ------------------------------------------------------------------------------- */
@Override
public void configure(WebAppContext context) throws Exception
{
@ -229,6 +405,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
}
}
/* ------------------------------------------------------------------------------- */
/**
* Look into the jars to discover info in META-INF. If useCaches == true, then we will
* cache the info discovered indexed by the jar in which it was discovered: this speeds
@ -283,6 +460,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
}
}
/* ------------------------------------------------------------------------------- */
/**
* Scan for META-INF/resources dir in the given jar.
*
@ -355,6 +533,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
dirs.add(resourcesDir);
}
/* ------------------------------------------------------------------------------- */
/**
* Scan for META-INF/web-fragment.xml file in the given jar.
*
@ -421,7 +600,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
if (LOG.isDebugEnabled()) LOG.debug(webFrag+" added to context");
}
/* ------------------------------------------------------------------------------- */
/**
* Discover META-INF/*.tld files in the given jar
*
@ -485,7 +664,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
if (LOG.isDebugEnabled()) LOG.debug("tlds added to context");
}
/* ------------------------------------------------------------------------------- */
@Override
public void postConfigure(WebAppContext context) throws Exception
{
@ -496,6 +675,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
context.setAttribute(METAINF_TLDS, null);
}
/* ------------------------------------------------------------------------------- */
/**
* Find all .tld files in all subdirs of the given dir.
*
@ -528,6 +708,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
return tlds;
}
/* ------------------------------------------------------------------------------- */
/**
* Find all .tld files in the given jar.
*
@ -559,7 +740,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
return tlds;
}
/* ------------------------------------------------------------------------------- */
protected List<Resource> findClassDirs (WebAppContext context)
throws Exception
{
@ -578,7 +759,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
return classDirs;
}
/* ------------------------------------------------------------------------------- */
/**
* Look for jars that should be treated as if they are in WEB-INF/lib
*
@ -599,6 +780,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
return jarResources;
}
/* ------------------------------------------------------------------------------- */
/**
* Look for jars in <code>WEB-INF/lib</code>
*
@ -645,7 +827,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
}
/* ------------------------------------------------------------------------------- */
/**
* Get jars from WebAppContext.getExtraClasspath as resources
*
@ -676,6 +858,7 @@ public class MetaInfConfiguration extends AbstractConfiguration
return jarResources;
}
/* ------------------------------------------------------------------------------- */
/**
* Get <code>WEB-INF/classes</code> dir
*

View File

@ -20,6 +20,7 @@ package org.eclipse.jetty.webapp;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import org.eclipse.jetty.server.Connector;
@ -48,6 +49,7 @@ public class WebInfConfiguration extends AbstractConfiguration
protected Resource _preUnpackBaseResource;
public WebInfConfiguration()
{
}
@ -62,6 +64,7 @@ public class WebInfConfiguration extends AbstractConfiguration
unpack (context);
}
@Override
public void configure(WebAppContext context) throws Exception
{

View File

@ -30,7 +30,9 @@ import java.util.Collection;
import java.util.List;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.JavaVersion;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.Assume;
import org.junit.Test;
/**
@ -136,5 +138,80 @@ public class MetaInfConfigurationTest
context31false.getServletContext().setEffectiveMinorVersion(1);
meta31false.preConfigure(context31false);
}
/**
* Assume target < jdk9. In this case, we should be able to extract
* the urls from the application classloader, and we should not look
* at the java.class.path property.
* @throws Exception
*/
@Test
public void testFindAndFilterContainerPaths()
throws Exception
{
Assume.assumeTrue(JavaVersion.VERSION.getMajor() < 9);
MetaInfConfiguration config = new MetaInfConfiguration();
WebAppContext context = new WebAppContext();
context.setAttribute(MetaInfConfiguration.CONTAINER_JAR_PATTERN, ".*/jetty-util-[^/]*\\.jar$|.*/jetty-util/target/classes/");
WebAppClassLoader loader = new WebAppClassLoader(context);
context.setClassLoader(loader);
config.findAndFilterContainerPaths(context);
List<Resource> containerResources = context.getMetaData().getContainerResources();
assertEquals(1, containerResources.size());
assertTrue(containerResources.get(0).toString().contains("jetty-util"));
}
/**
* Assume target jdk9 or above. In this case we should extract what we need
* from the java.class.path. We should also examine the module path.
* @throws Exception
*/
@Test
public void testFindAndFilterContainerPathsJDK9()
throws Exception
{
Assume.assumeTrue(JavaVersion.VERSION.getMajor() >= 9);
Assume.assumeTrue(System.getProperty("jdk.module.path") != null);
MetaInfConfiguration config = new MetaInfConfiguration();
WebAppContext context = new WebAppContext();
context.setAttribute(MetaInfConfiguration.CONTAINER_JAR_PATTERN, ".*/jetty-util-[^/]*\\.jar$|.*/jetty-util/target/classes/$|.*/foo-bar-janb.jar");
WebAppClassLoader loader = new WebAppClassLoader(context);
context.setClassLoader(loader);
config.findAndFilterContainerPaths(context);
List<Resource> containerResources = context.getMetaData().getContainerResources();
assertEquals(2, containerResources.size());
for (Resource r:containerResources)
{
String s = r.toString();
assertTrue(s.endsWith("foo-bar-janb.jar") || s.contains("jetty-util"));
}
}
/**
* Assume runtime is jdk9 or above. Target is jdk 8. In this
* case we must extract from the java.class.path (because jdk 9
* has no url based application classloader), but we should
* ignore the module path.
* @throws Exception
*/
@Test
public void testFindAndFilterContainerPathsTarget8()
throws Exception
{
Assume.assumeTrue(JavaVersion.VERSION.getMajor() >= 9);
Assume.assumeTrue(System.getProperty("jdk.module.path") != null);
MetaInfConfiguration config = new MetaInfConfiguration();
WebAppContext context = new WebAppContext();
context.setAttribute(JavaVersion.JAVA_TARGET_PLATFORM, "8");
context.setAttribute(MetaInfConfiguration.CONTAINER_JAR_PATTERN, ".*/jetty-util-[^/]*\\.jar$|.*/jetty-util/target/classes/$|.*/foo-bar-janb.jar");
WebAppClassLoader loader = new WebAppClassLoader(context);
context.setClassLoader(loader);
config.findAndFilterContainerPaths(context);
List<Resource> containerResources = context.getMetaData().getContainerResources();
assertEquals(1, containerResources.size());
assertTrue(containerResources.get(0).toString().contains("jetty-util"));
}
}

Binary file not shown.

Binary file not shown.