Merge branch `jetty-9.4.x` into `jetty-10.0.x`

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-06-21 16:04:13 -05:00
commit c7b6110267
36 changed files with 1141 additions and 119 deletions

View File

@ -1,36 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.eclipse.jetty.cdi</groupId>
<artifactId>jetty-cdi-parent</artifactId>
<version>10.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cdi-2</artifactId>
<name>Jetty :: CDI 2</name>
<url>http://www.eclipse.org/jetty</url>
<packaging>jar</packaging>
<properties>
<bundle-symbolic-name>${project.groupId}.cdi2</bundle-symbolic-name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>config</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -6,12 +6,32 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.jetty.cdi</groupId>
<artifactId>jetty-cdi-parent</artifactId>
<name>Jetty :: CDI :: Parent</name>
<artifactId>cdi-2</artifactId>
<name>Jetty :: CDI 2</name>
<url>http://www.eclipse.org/jetty</url>
<packaging>pom</packaging>
<modules>
<module>cdi-2</module>
</modules>
<packaging>jar</packaging>
<properties>
<bundle-symbolic-name>${project.groupId}.cdi2</bundle-symbolic-name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>config</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,7 @@
# DO NOT EDIT - See: https://www.eclipse.org/jetty/documentation/current/startup-modules.html
[description]
Jetty setup to support Weld/CDI2 with WELD inside the webapp
[depend]
cdi2

View File

@ -1,4 +1,4 @@
DO NOT EDIT - See: https://www.eclipse.org/jetty/documentation/current/startup-modules.html
# DO NOT EDIT - See: https://www.eclipse.org/jetty/documentation/current/startup-modules.html
[description]
Jetty setup to support Weld/CDI2 with WELD inside the webapp
@ -6,6 +6,11 @@ Jetty setup to support Weld/CDI2 with WELD inside the webapp
[depend]
annotations
[lib]
lib/apache-jsp/org.mortbay.jasper.apache-el-*.jar
[xml]
etc/cdi2/jetty-cdi2.xml
[ini]
jetty.webapp.addServerClasses+=,-org.eclipse.jetty.util.Decorator,-org.eclipse.jetty.util.DecoratedObjectFactory

View File

@ -56,19 +56,6 @@ Modules for tag '*':
LIB: lib/apache-jstl/*.jar
Enabled: transitive provider of apache-jstl for jstl
Module: cdi
: Experimental CDI/Weld integration
Depend: cdi1
Module: cdi1
: Experimental CDI/Weld integration
: Deprecated in favour of cdi2 module.
Depend: deploy, annotations, plus, jsp
LIB: lib/cdi/*.jar
LIB: lib/cdi-core-${jetty.version}.jar
LIB: lib/cdi-servlet-${jetty.version}.jar
XML: etc/jetty-cdi.xml
Module: cdi2
: Jetty setup to support Weld/CDI2 with WELD inside the webapp
Depend: deploy

View File

@ -163,7 +163,7 @@ public class IncludeExcludeSet<T,P> implements Predicate<P>
/**
* Test Included and not Excluded
* @param item The item to test
* @return Boolean.TRUE if item is included, Boolean.FALSE if item is excluded and null if neither
* @return Boolean.TRUE if item is included, Boolean.FALSE if item is excluded or null if neither
*/
public Boolean isIncludedAndNotExcluded(P item)
{

View File

@ -19,23 +19,35 @@
package org.eclipse.jetty.util;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.module.Configuration;
import java.lang.module.ModuleReference;
import java.lang.module.ResolvedModule;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import static java.lang.invoke.MethodType.methodType;
/* ------------------------------------------------------------ */
/**
@ -171,6 +183,29 @@ public class TypeUtil
}
}
private static final MethodHandle[] LOCATION_METHODS;
static
{
List<MethodHandle> locationMethods = new ArrayList<>();
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType type = methodType(URI.class, Class.class);
try
{
locationMethods.add(lookup.findStatic(TypeUtil.class, "getCodeSourceLocation", type));
locationMethods.add(lookup.findStatic(TypeUtil.class, "getModuleLocation", type));
locationMethods.add(lookup.findStatic(TypeUtil.class, "getClassLoaderLocation", type));
locationMethods.add(lookup.findStatic(TypeUtil.class, "getSystemClassLoaderLocation", type));
LOCATION_METHODS = locationMethods.toArray(new MethodHandle[0]);
}
catch (Exception e)
{
throw new RuntimeException("Unable to establish Location Lookup Handles", e);
}
}
/* ------------------------------------------------------------ */
/** Array to List.
* <p>
@ -564,11 +599,89 @@ public class TypeUtil
}
/* ------------------------------------------------------------ */
/**
* Attempt to find the Location of a loaded Class.
* <p>
* This can be null for primitives, void, and in-memory classes.
* </p>
* @param clazz the loaded class to find a location for.
* @return the location as a URI (this is a URI pointing to a holder of the class: a directory,
* a jar file, a {@code jrt://} resource, etc), or null of no location available.
*/
public static URI getLocationOfClass(Class<?> clazz)
{
URI location;
for (MethodHandle locationMethod : LOCATION_METHODS)
{
try
{
ProtectionDomain domain = clazz.getProtectionDomain();
location = (URI)locationMethod.invoke(clazz);
if (location != null)
{
return location;
}
}
catch (Throwable cause)
{
cause.printStackTrace(System.err);
}
}
return null;
}
public static URI getClassLoaderLocation(Class<?> clazz)
{
return getClassLoaderLocation(clazz, clazz.getClassLoader());
}
public static URI getSystemClassLoaderLocation(Class<?> clazz)
{
return getClassLoaderLocation(clazz, ClassLoader.getSystemClassLoader());
}
public static URI getClassLoaderLocation(Class<?> clazz, ClassLoader loader)
{
if (loader == null)
{
return null;
}
try
{
String resourceName = TypeUtil.toClassReference(clazz);
if (loader != null)
{
URL url = loader.getResource(resourceName);
if (url != null)
{
URI uri = url.toURI();
String uriStr = uri.toASCIIString();
if(uriStr.startsWith("jar:file:"))
{
uriStr = uriStr.substring(4);
int idx = uriStr.indexOf("!/");
if (idx > 0)
{
return URI.create(uriStr.substring(0, idx));
}
}
return uri;
}
}
}
catch (URISyntaxException ignore)
{
}
return null;
}
public static URI getCodeSourceLocation(Class<?> clazz)
{
try
{
ProtectionDomain domain = AccessController.doPrivileged((PrivilegedAction<ProtectionDomain>)() -> clazz.getProtectionDomain());
if (domain != null)
{
CodeSource source = domain.getCodeSource();
@ -577,22 +690,56 @@ public class TypeUtil
URL location = source.getLocation();
if (location != null)
{
return location.toURI();
}
}
}
}
catch (URISyntaxException ignore)
{
}
return null;
}
String resourceName = TypeUtil.toClassReference(clazz);
ClassLoader loader = clazz.getClassLoader();
URL url = (loader == null ? ClassLoader.getSystemClassLoader() : loader).getResource(resourceName);
if (url != null)
public static URI getModuleLocation(Class<?> clazz)
{
return URIUtil.getJarSource(url.toURI());
}
}
catch (URISyntaxException e)
Module module = clazz.getModule();
if (module == null)
{
LOG.debug(e);
return null;
}
ModuleLayer layer = module.getLayer();
if (layer == null)
{
return null;
}
Configuration configuration = layer.configuration();
if (configuration == null)
{
return null;
}
Optional<ResolvedModule> resolvedModule = configuration.findModule(module.getName());
if ((resolvedModule == null) || !resolvedModule.isPresent())
{
return null;
}
ModuleReference moduleReference = resolvedModule.get().reference();
if (moduleReference == null)
{
return null;
}
Optional<URI> location = moduleReference.location();
if (location.isPresent())
{
return location.get();
}
return null;
}

View File

@ -18,12 +18,9 @@
package org.eclipse.jetty.util;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.EnabledOnJre;
@ -105,7 +102,7 @@ public class TypeUtilTest
}
@Test
public void testIsTrue() throws Exception
public void testIsTrue()
{
assertTrue(TypeUtil.isTrue(Boolean.TRUE));
assertTrue(TypeUtil.isTrue(true));
@ -120,7 +117,7 @@ public class TypeUtilTest
}
@Test
public void testIsFalse() throws Exception
public void testIsFalse()
{
assertTrue(TypeUtil.isFalse(Boolean.FALSE));
assertTrue(TypeUtil.isFalse(false));
@ -135,19 +132,21 @@ public class TypeUtilTest
}
@Test
@Disabled // TODO fails if the mavenRepo is a symbolic link
public void testGetLocationOfClass() throws Exception
public void testGetLocationOfClass_FromMavenRepo()
{
String mavenRepoPathProperty = System.getProperty( "mavenRepoPath");
String mavenRepoPathProperty = System.getProperty("mavenRepoPath");
assumeTrue(mavenRepoPathProperty != null);
Path mavenRepoPath = Paths.get( mavenRepoPathProperty );
Path mavenRepoPath = Paths.get(mavenRepoPathProperty);
System.err.println("mavenRepoPath "+mavenRepoPath);
String mavenRepo = mavenRepoPath.toFile().getPath().replaceAll("\\\\", "/");
// Classes from maven dependencies
assertThat(TypeUtil.getLocationOfClass(Assertions.class).toASCIIString(),containsString(mavenRepo));
assertThat(TypeUtil.getLocationOfClass(org.junit.jupiter.api.Assertions.class).toASCIIString(), containsString(mavenRepo));
}
@Test
public void getLocationOfClass_ClassDirectory()
{
// Class from project dependencies
assertThat(TypeUtil.getLocationOfClass(TypeUtil.class).toASCIIString(),containsString("/classes/"));
}
@ -157,10 +156,19 @@ public class TypeUtilTest
public void testGetLocation_JvmCore_JPMS()
{
// Class from JVM core
String expectedJavaBase = "/java.base/";
String expectedJavaBase = "/java.base";
assertThat(TypeUtil.getLocationOfClass(String.class).toASCIIString(),containsString(expectedJavaBase));
}
@Test
@DisabledOnJre(JRE.JAVA_8)
public void testGetLocation_JavaLangThreadDeath_JPMS()
{
// Class from JVM core
String expectedJavaBase = "/java.base";
assertThat(TypeUtil.getLocationOfClass(java.lang.ThreadDeath.class).toASCIIString(),containsString(expectedJavaBase));
}
@Test
@EnabledOnJre(JRE.JAVA_8)
public void testGetLocation_JvmCore_Java8RT()
@ -169,4 +177,13 @@ public class TypeUtilTest
String expectedJavaBase = "/rt.jar";
assertThat(TypeUtil.getLocationOfClass(String.class).toASCIIString(),containsString(expectedJavaBase));
}
@Test
@EnabledOnJre(JRE.JAVA_8)
public void testGetLocation_JavaLangThreadDeath_Java8RT()
{
// Class from JVM core
String expectedJavaBase = "/rt.jar";
assertThat(TypeUtil.getLocationOfClass(java.lang.ThreadDeath.class).toASCIIString(),containsString(expectedJavaBase));
}
}

View File

@ -18,28 +18,27 @@
package org.eclipse.jetty.util.resource;
import java.net.URI;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.TypeUtil;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import java.net.URI;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.TypeUtil;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;
public class JrtResourceTest
{
private String testResURI = MavenTestingUtils.getTestResourcesDir().getAbsoluteFile().toURI().toASCIIString();
@Test
@DisabledOnJre(JRE.JAVA_8)
@Disabled("Not supported on Java 9+ Module API")
public void testResourceFromUriForString()
throws Exception
{
@ -57,6 +56,7 @@ public class JrtResourceTest
@Test
@DisabledOnJre(JRE.JAVA_8)
@Disabled("Not supported on Java 9+ Module API")
public void testResourceFromStringForString()
throws Exception
{
@ -73,6 +73,7 @@ public class JrtResourceTest
@Test
@DisabledOnJre(JRE.JAVA_8)
@Disabled("Not supported on Java 9+ Module API")
public void testResourceFromURLForString()
throws Exception
{
@ -111,7 +112,4 @@ public class JrtResourceTest
assertThat(resource.isDirectory(), is(false));
assertThat(resource.length(),is(-1L));
}
}

View File

@ -354,6 +354,8 @@ public class ClassMatcher extends AbstractSet<String>
@Override
public boolean test(URI uri)
{
if ((uri == null) || (!uri.isAbsolute()))
return false;
if (!uri.getScheme().equals("file"))
return false;
Path path = Paths.get(uri);
@ -391,6 +393,8 @@ public class ClassMatcher extends AbstractSet<String>
@Override
public boolean test(URI uri)
{
if ((uri == null) || (!uri.isAbsolute()))
return false;
if (!uri.getScheme().equalsIgnoreCase("jrt"))
return false;
String module = uri.getPath();
@ -444,6 +448,8 @@ public class ClassMatcher extends AbstractSet<String>
@Override
public boolean test(URI name)
{
if ((name == null) || (!name.isAbsolute()))
return false;
return _byLocation.test(name) || _byModule.test(name);
}
@ -749,13 +755,18 @@ public class ClassMatcher extends AbstractSet<String>
if (Boolean.FALSE==byName)
return false;
Boolean byLocation = locations.isIncludedAndNotExcluded(location.get());
if (Boolean.FALSE==byLocation)
URI uri = location.get();
if (uri != null)
{
Boolean byLocation = locations.isIncludedAndNotExcluded(uri);
if (Boolean.FALSE == byLocation)
return false;
return Boolean.TRUE.equals(byName)
|| Boolean.TRUE.equals(byLocation)
|| !(names.hasIncludes() || locations.hasIncludes());
}
return false;
}
}

View File

@ -18,12 +18,7 @@
package org.eclipse.jetty.webapp;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.net.URI;
import java.nio.file.Paths;
import java.util.Arrays;
import org.eclipse.jetty.util.TypeUtil;
@ -34,6 +29,10 @@ import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ClassMatcherTest
{
private final ClassMatcher _pattern = new ClassMatcher();
@ -122,15 +121,12 @@ public class ClassMatcherTest
{
// jar from JVM classloader
URI loc_string = TypeUtil.getLocationOfClass(String.class);
// System.err.println(loc_string);
// a jar from maven repo jar
URI loc_junit = TypeUtil.getLocationOfClass(Test.class);
// System.err.println(loc_junit);
// class file
URI loc_test = TypeUtil.getLocationOfClass(ClassMatcherTest.class);
// System.err.println(loc_test);
ClassMatcher pattern = new ClassMatcher();
pattern.include("something");
@ -139,7 +135,7 @@ public class ClassMatcherTest
assertThat(pattern.match(ClassMatcherTest.class), Matchers.is(false));
// Add directory for both JVM classes
pattern.include(Paths.get(loc_string).getParent().toUri().toString());
pattern.include(loc_string.toASCIIString());
// Add jar for individual class and classes directory
pattern.include(loc_junit.toString(), loc_test.toString());
@ -220,7 +216,7 @@ public class ClassMatcherTest
assertThat(pattern.match(ClassMatcherTest.class), Matchers.is(true));
// Add directory for both JVM classes
pattern.exclude(Paths.get(loc_string).getParent().toUri().toString());
pattern.exclude(loc_string.toString());
// Add jar for individual class and classes directory
pattern.exclude(loc_junit.toString(), loc_test.toString());

View File

@ -75,6 +75,13 @@
<type>war</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-cdi2-webapp</artifactId>
<version>${project.version}</version>
<type>war</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
@ -91,6 +98,13 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-felix-webapp</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>war</type>
</dependency>
</dependencies>
<build>

View File

@ -191,15 +191,17 @@ public class DistributionTester
*
* @param warFile the war file to install
* @param context the context path
* @return the path to the installed webapp exploded directory
* @throws IOException if the installation fails
*/
public void installWarFile(File warFile, String context) throws IOException
public Path installWarFile(File warFile, String context) throws IOException
{
//webapps
Path webapps = config.jettyBase.resolve("webapps").resolve(context);
if (!Files.exists(webapps))
Files.createDirectories(webapps);
unzip(warFile, webapps.toFile());
return webapps;
}
/**

View File

@ -0,0 +1,144 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.tests.distribution;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CDITests extends AbstractDistributionTest
{
/**
* Tests a WAR file that is CDI complete as it includes the weld
* library in its WEB-INF/lib directory.
*
* @throws Exception
*/
@Test
public void testCDI2_IncludedInWebapp() throws Exception
{
String jettyVersion = System.getProperty("jettyVersion");
DistributionTester distribution = DistributionTester.Builder.newInstance()
.jettyVersion(jettyVersion)
.mavenLocalRepository(System.getProperty("mavenRepoPath"))
.build();
String[] args1 = {
"--create-startd",
"--approve-all-licenses",
"--add-to-start=http,deploy,annotations,jsp"
};
try (DistributionTester.Run run1 = distribution.start(args1))
{
assertTrue(run1.awaitFor(5, TimeUnit.SECONDS));
assertEquals(0, run1.getExitValue());
File war = distribution.resolveArtifact("org.eclipse.jetty.tests:test-cdi2-webapp:war:" + jettyVersion);
distribution.installWarFile(war, "demo");
distribution.installBaseResource("cdi/demo_context.xml", "webapps/demo.xml");
int port = distribution.freePort();
try (DistributionTester.Run run2 = distribution.start("jetty.http.port=" + port))
{
assertTrue(run2.awaitConsoleLogsFor("Started @", 10, TimeUnit.SECONDS));
startHttpClient();
ContentResponse response = client.GET("http://localhost:" + port + "/demo/greetings");
assertEquals(HttpStatus.OK_200, response.getStatus());
// Confirm Servlet based CDI
assertThat(response.getContentAsString(), containsString("Hello GreetingsServlet"));
// Confirm Listener based CDI (this has been a problem in the past, keep this for regression testing!)
assertThat(response.getHeaders().get("Server"), containsString("CDI-Demo-org.eclipse.jetty.test"));
run2.stop();
assertTrue(run2.awaitFor(5, TimeUnit.SECONDS));
}
}
}
/**
* Tests a WAR file that is expects CDI to be configured by the server.
*
* <p>
* This means the WAR has the weld libs in its
* WEB-INF/lib directory.
* </p>
*
* <p>
* The expectation is that a context xml deployable file is not
* required when using this `cdi2` module, and the appropriate
* server side libs are made available to allow weld to function.
* (the required server side javax.el support comes from the cdi2 module)
* </p>
*
* @throws Exception
*/
@Test
public void testCDI2_ConfiguredByServer() throws Exception
{
String jettyVersion = System.getProperty("jettyVersion");
DistributionTester distribution = DistributionTester.Builder.newInstance()
.jettyVersion(jettyVersion)
.mavenLocalRepository(System.getProperty("mavenRepoPath"))
.build();
String[] args1 = {
"--create-startd",
"--approve-all-licenses",
// standard entries
"--add-to-start=http,deploy,annotations",
// cdi2 specific entry (should transitively pull in what it needs, the user should not be expected to know the transitive entries)
"--add-to-start=cdi2"
};
try (DistributionTester.Run run1 = distribution.start(args1))
{
assertTrue(run1.awaitFor(5, TimeUnit.SECONDS));
assertEquals(0, run1.getExitValue());
File war = distribution.resolveArtifact("org.eclipse.jetty.tests:test-cdi2-webapp:war:" + jettyVersion);
distribution.installWarFile(war, "demo");
int port = distribution.freePort();
try (DistributionTester.Run run2 = distribution.start("jetty.http.port=" + port))
{
assertTrue(run2.awaitConsoleLogsFor("Started @", 10, TimeUnit.SECONDS));
startHttpClient();
ContentResponse response = client.GET("http://localhost:" + port + "/demo/greetings");
assertEquals(HttpStatus.OK_200, response.getStatus());
// Confirm Servlet based CDI
assertThat(response.getContentAsString(), containsString("Hello GreetingsServlet"));
// Confirm Listener based CDI (this has been a problem in the past, keep this for regression testing!)
assertThat(response.getHeaders().get("Server"), containsString("CDI-Demo-org.eclipse.jetty.test"));
run2.stop();
assertTrue(run2.awaitFor(5, TimeUnit.SECONDS));
}
}
}
}

View File

@ -0,0 +1,69 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.tests.distribution;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class OsgiAppTests extends AbstractDistributionTest
{
@Test
public void testFelixWebappStart() throws Exception
{
String jettyVersion = System.getProperty("jettyVersion");
DistributionTester distribution = DistributionTester.Builder.newInstance()
.jettyVersion(jettyVersion)
.mavenLocalRepository(System.getProperty("mavenRepoPath"))
.build();
String[] args1 = {
"--create-startd",
"--approve-all-licenses",
"--add-to-start=http,deploy,annotations,plus,resources"
};
try (DistributionTester.Run run1 = distribution.start(args1))
{
assertTrue(run1.awaitFor(5, TimeUnit.SECONDS));
assertEquals(0, run1.getExitValue());
File war = distribution.resolveArtifact("org.eclipse.jetty.tests:test-felix-webapp:war:" + jettyVersion);
distribution.installWarFile(war, "test");
int port = distribution.freePort();
try (DistributionTester.Run run2 = distribution.start("jetty.http.port=" + port))
{
assertTrue(run2.awaitConsoleLogsFor("Started @", 10, TimeUnit.SECONDS));
startHttpClient();
ContentResponse response = client.GET("http://localhost:" + port + "/test/info");
assertEquals(HttpStatus.OK_200, response.getStatus());
assertThat(response.getContentAsString(), containsString("Framework: org.apache.felix.framework"));
}
}
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/demo</Set>
<Set name="war"><Property name="jetty.webapps"/>/demo/</Set>
<Get name="serverClasspathPattern">
<Call name="add">
<Arg>-org.eclipse.jetty.util.Decorator</Arg>
</Call>
<Call name="add">
<Arg>-org.eclipse.jetty.util.DecoratedObjectFactory</Arg>
</Call>
<Call name="add">
<Arg>-org.eclipse.jetty.server.handler.ContextHandler.</Arg>
</Call>
<Call name="add">
<Arg>-org.eclipse.jetty.server.handler.ContextHandler</Arg>
</Call>
<Call name="add">
<Arg>-org.eclipse.jetty.servlet.ServletContextHandler</Arg>
</Call>
</Get>
</Configure>

View File

@ -40,5 +40,7 @@
<module>test-jndi-webapp</module>
<module>test-http2-webapp</module>
<module>test-simple-webapp</module>
<module>test-felix-webapp</module>
<module>test-cdi2-webapp</module>
</modules>
</project>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-webapps-parent</artifactId>
<version>10.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-cdi2-webapp</artifactId>
<name>Test :: CDI2 On Jetty :: Included in WebApp</name>
<packaging>war</packaging>
<properties>
<bundle-symbolic-name>${project.groupId}.cdi2.webapp</bundle-symbolic-name>
</properties>
<build>
<finalName>cdi2-demo</finalName>
</build>
<dependencies>
<!-- provided by container -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- included in webapp -->
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>${weld.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,33 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Named;
public class FriendlyGreetings
{
@Produces
@Named("friendly")
public Greetings getGreetings(InjectionPoint ip)
{
return () -> "Hello " + ip.getMember().getDeclaringClass().getSimpleName();
}
}

View File

@ -0,0 +1,24 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
public interface Greetings
{
String getGreeting();
}

View File

@ -0,0 +1,42 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/greetings")
public class GreetingsServlet extends HttpServlet
{
@Inject
@Named("friendly")
public Greetings greetings;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.setContentType("text/plain");
resp.getWriter().println("[" + greetings.getGreeting() + "]");
}
}

View File

@ -0,0 +1,52 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/info")
public class InfoServlet extends HttpServlet
{
@Inject
BeanManager beanManager;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.setContentType("text/plain");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.println("Bean Manager: " + beanManager);
Set<Bean<?>> beans = beanManager.getBeans("");
for (Bean<?> bean : beans)
{
out.println(" " + bean);
}
}
}

View File

@ -0,0 +1,38 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
import javax.enterprise.inject.Produces;
public class ManifestServerID
{
@Produces
public ServerID getServerID()
{
return () ->
{
String implVersion = this.getClass().getPackage().getImplementationVersion();
if(implVersion == null)
implVersion = this.getClass().getPackage().getName();
if(implVersion == null)
implVersion = "unknown";
return "CDI-Demo-" + implVersion;
};
}
}

View File

@ -0,0 +1,42 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
import javax.inject.Inject;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyContextListener implements ServletContextListener
{
@Inject
public ServerID serverId;
@Override
public void contextInitialized(ServletContextEvent sce)
{
sce.getServletContext().setAttribute("ServerID", serverId.get());
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
}
}

View File

@ -0,0 +1,32 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
public class OldGreetings
{
@Produces
@Named("old")
public Greetings getGreeting()
{
return () -> "Salutations!";
}
}

View File

@ -0,0 +1,24 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
public interface ServerID
{
String get();
}

View File

@ -0,0 +1,57 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
/**
* A Web Filter that doesn't use CDI.
*/
@WebFilter("/*")
public class ServerIDFilter implements Filter
{
@Override
public void init(FilterConfig filterConfig)
{
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
if (response instanceof HttpServletResponse)
{
String serverID = (String)request.getServletContext().getAttribute("ServerID");
((HttpServletResponse)response).setHeader("Server", serverID);
}
chain.doFilter(request, response);
}
@Override
public void destroy()
{
}
}

View File

@ -0,0 +1,8 @@
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>
<Ref refid="webAppCtx"/>
</Arg>
<Arg>BeanManager</Arg>
<Arg>
<New class="javax.naming.Reference">
<Arg>javax.enterprise.inject.spi.BeanManager</Arg>
<Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
<Arg/>
</New>
</Arg>
</New>
</Configure>

View File

@ -0,0 +1,17 @@
<?xml version="1.0"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>CDI Integration Test WebApp</display-name>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<resource-env-ref>
<description>Object factory for the CDI Bean Manager</description>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
</web-app>

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-webapps-parent</artifactId>
<version>10.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-felix-webapp</artifactId>
<name>Test :: Jetty Felix Webapp</name>
<packaging>war</packaging>
<properties>
<bundle-symbolic-name>${project.groupId}.felix</bundle-symbolic-name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>6.0.3</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,82 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.demo;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
@WebListener
public class AppListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent sce)
{
Framework framework = initFelix();
sce.getServletContext().setAttribute(Framework.class.getName(), framework);
}
private Framework initFelix()
{
Map<String, String> properties = new HashMap<>();
try
{
Path cacheDir = Files.createTempDirectory("felix-cache");
properties.put(Constants.FRAMEWORK_STORAGE, cacheDir.toAbsolutePath().toString());
properties.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
properties.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK);
properties.put(Constants.FRAMEWORK_BOOTDELEGATION, "*");
}
catch (IOException e)
{
throw new RuntimeException("Unable to configure Felix", e);
}
Framework framework = ServiceLoader.load(FrameworkFactory.class).iterator().next().newFramework(properties);
try
{
System.err.println("Initializing felix");
framework.init();
System.err.println("Starting felix");
framework.start();
}
catch (Exception e)
{
e.printStackTrace(System.err);
throw new RuntimeException("Unable to start Felix", e);
}
return framework;
}
public void contextDestroyed(ServletContextEvent sce)
{
}
}

View File

@ -0,0 +1,54 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.launch.Framework;
@WebServlet("/info")
public class InfoServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("text/plain");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
Framework framework = (Framework)getServletContext().getAttribute(Framework.class.getName());
out.printf("Framework: %s\n", framework);
BundleContext bundleContext = framework.getBundleContext();
out.printf("BundleContext: %s\n", bundleContext);
Bundle bundleSelf = bundleContext.getBundle();
out.printf("BundleContext.bundle: %s\n", bundleSelf);
for (Bundle bundle : bundleContext.getBundles())
{
out.printf("bundle[]: %s\n", bundle);
}
}
}