Skip to content

Commit

Permalink
Issue #3804 CDI integration
Browse files Browse the repository at this point in the history
Rename attributes and classes to have a more regular pattern.
The DecoratingListener is now extened by the
CdiDecoratingListener which is used by the cdi-decorate module

Signed-off-by: Greg Wilkins <[email protected]>
  • Loading branch information
gregw committed Aug 8, 2019
1 parent dc939d7 commit 70fcd3d
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 197 deletions.
2 changes: 0 additions & 2 deletions jetty-cdi/src/main/config/etc/cdi/jetty-cdi2.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<!-- =============================================================== --><!-- Mixin the Weld / CDI classes to the class loader --><!-- =============================================================== -->

<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Ref refid="DeploymentManager">
<Call name="addLifeCycleBinding">
Expand Down
21 changes: 7 additions & 14 deletions jetty-cdi/src/main/config/modules/cdi-spi.mod
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
# DO NOT EDIT - See: https://www.eclipse.org/jetty/documentation/current/startup-modules.html

[description]
CDI SPI integration for CDI inside the webapp.
This module does not provide CDI, but configures jetty to look for the CDI SPI within
a webapp. If the CDI SPI is found, then a CdiDecorator will be registered to
decorate Listeners, Filters and Servlets using the standard CDI SPI.
The module indicates to the webapp that this mechanism is available by setting the
"org.eclipse.jetty.cdi" context attribute to "CdiDecorator".
This is the preferred integration for OWB.
Configures Jetty to use the "CdiSpiDecorator" that calls the CDI SPI
as the default CDI integration mode.

[tag]
cdi

[depend]
deploy
[provides]
cdi-mode

[lib]
lib/jetty-cdi-${jetty.version}.jar
lib/apache-jsp/org.mortbay.jasper.apache-el-*.jar
[depend]
cdi

[ini]
jetty.webapp.addSystemClasses+=,org.eclipse.jetty.cdi.CdiServletContainerInitializer
jetty.webapp.addServerClasses+=,-org.eclipse.jetty.cdi.CdiServletContainerInitializer
jetty.cdi.mode=CdiSpiDecorator
27 changes: 25 additions & 2 deletions jetty-cdi/src/main/config/modules/cdi.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
# DO NOT EDIT - See: https://www.eclipse.org/jetty/documentation/current/startup-modules.html

[description]
Deprecated CDI module. Current depends on cdi-spi.
Support for CDI inside the webapp.
This module does not provide CDI, but configures jetty to support various
integration modes with a CDI implementation on the webapp classpath.
CDI integration modes can be selected per webapp with the "jetty.cdi.mode"
init parameter or default to the mode set by the "jetty.cdi.mode" server attribute.
Supported modes are:
CdiDecorator - Jetty will call the CDI SPI within the webapp to decorate
objects (default).
DecoratingLister - The webapp may register a decorator on the context attribute
"org.eclipse.jetty.cdi.decorator".

[tag]
cdi

[provides]
cdi

[depend]
cdi-spi
deploy

[xml]
etc/cdi/jetty-cdi.xml

[lib]
lib/jetty-cdi-${jetty.version}.jar
lib/apache-jsp/org.mortbay.jasper.apache-el-*.jar

[ini]
jetty.webapp.addSystemClasses+=,org.eclipse.jetty.cdi.CdiServletContainerInitializer
jetty.webapp.addServerClasses+=,-org.eclipse.jetty.cdi.CdiServletContainerInitializer
3 changes: 3 additions & 0 deletions jetty-cdi/src/main/config/modules/cdi2.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ implementation in the webapp.
[tag]
cdi

[provides]
cdi-mode

[depend]
deploy

Expand Down
165 changes: 0 additions & 165 deletions jetty-cdi/src/main/java/org/eclipse/jetty/cdi/CdiDecorator.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* A {@link ServletContainerInitializer} that introspects for a CDI API
* implementation within a web application. If the CDI API is found, then
* a {@link CdiDecorator} is registered as a {@link org.eclipse.jetty.util.Decorator}
* a {@link CdiSpiDecorator} is registered as a {@link org.eclipse.jetty.util.Decorator}
* for the context.
* @see AnnotationConfiguration.ServletContainerInitializerOrdering
*/
Expand All @@ -47,9 +47,31 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx)
{
ServletContextHandler context = ServletContextHandler.getServletContextHandler(ctx);
Objects.requireNonNull(context);
context.getObjectFactory().addDecorator(new CdiDecorator(context));
context.setAttribute(CDI_INTEGRATION_ATTRIBUTE, "CdiDecorator");
LOG.info("CdiDecorator enabled in " + ctx);

String mode = ctx.getInitParameter(CDI_INTEGRATION_ATTRIBUTE);
if (mode == null)
{
mode = (String)context.getServer().getAttribute(CDI_INTEGRATION_ATTRIBUTE);
if (mode == null)
mode = CdiSpiDecorator.MODE;
}

switch (mode)
{
case CdiSpiDecorator.MODE:
context.getObjectFactory().addDecorator(new CdiSpiDecorator(context));
break;

case CdiDecoratingListener.MODE:
context.addEventListener(new CdiDecoratingListener());
break;

default:
throw new IllegalStateException(mode);
}

context.setAttribute(CDI_INTEGRATION_ATTRIBUTE, mode);
LOG.info(mode + " enabled in " + ctx);
}
catch (UnsupportedOperationException e)
{
Expand Down
7 changes: 1 addition & 6 deletions jetty-deploy/src/main/config/modules/decorate.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# DO NOT EDIT - See: https://www.eclipse.org/jetty/documentation/current/startup-modules.html

[description]
Jetty setup to support Decoration of Listeners, Filters and Servlets within a deployed
webapp (as used by some CDI integrations).
Jetty setup to support Decoration of Listeners, Filters and Servlets within a deployed webapp.
This module uses the DecoratingListener to register an object set as a context attribute
as a dynamic decorator. This module sets the "org.eclipse.jetty.webapp.DecoratingListener"
context attribute with the name of the context attribute that will be listened to.
By default the attribute is "org.eclipse.jetty.webapp.decorator".
This is the preferred integration for Weld >= 3.1.2

[tag]
cdi

[depend]
deploy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public DecoratingListener(ServletContextHandler context, String attributeName)
_context = context;
Objects.requireNonNull(_context);
_attributeName = attributeName == null ? DECORATOR_ATTRIBUTE : attributeName;
checkAndSetAttribute();
checkAndSetAttributeName();
Object decorator = _context.getAttribute(_attributeName);
if (decorator != null)
_context.getObjectFactory().addDecorator(asDecorator(decorator));
}

protected void checkAndSetAttribute()
protected void checkAndSetAttributeName()
{
// If not set (by another DecoratingListener), flag the attribute that are
// listening for. If more than one DecoratingListener is used then this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public static Stream<Arguments> tests()

List<Object[]> tests = new ArrayList<>();

tests.add(new Object[]{"weld", "cdi-spi", null}); // Requires Weld >= 3.1.2
tests.add(new Object[]{"weld", "cdi-spi", null}); // Weld >= 3.1.2
tests.add(new Object[]{"weld", "cdi2", null});
tests.add(new Object[]{"weld", "decorate", null}); // Requires Weld >= 3.1.2
tests.add(new Object[]{"weld", "decorate", null}); // Weld == 3.1.2
// TODO tests.add(new Object[]{"weld", "cdi-decorate", null}); // Weld > 3.1.2
tests.add(new Object[]{"owb", "cdi-spi", removeJettyWebXml});
tests.add(new Object[]{"owb", "cdi2", null});
// tests.add(new Object[]{"owb", "decorate", null}); // Will not be supported
// tests.add(new Object[]{"owb", "cdi-decorate", null}); // Will not be supported
return tests.stream().map(Arguments::of);
}

Expand Down

0 comments on commit 70fcd3d

Please sign in to comment.