Skip to content

Commit

Permalink
WELD-2597 Rename the attributes for Jetty integration #2587 (#1933)
Browse files Browse the repository at this point in the history
* WELD-2597 Rename the attributes for Jetty integration #2587

Signed-off-by: Greg Wilkins <[email protected]>
  • Loading branch information
gregw authored and manovotn committed Sep 10, 2019
1 parent 4be7e12 commit c5b20b0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
8 changes: 4 additions & 4 deletions docs/reference/src/main/asciidoc/environments.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ No further configuration is needed when starting Jetty as an embedded webapp ser
However, if you’re using a Jetty standalone instance one more configuration step is required, one of the
jetty modules listed below needs to be enabled.

===== Jetty `decorate` Module
===== Jetty `cdi-decorate` Module

Since Jetty-9.4.20 and Weld 3.1.2, the Weld/Jetty integration uses the jetty `decorate` module.
To activate this module in jetty the argument `--module=decorate` needs to be added to the
Since Jetty-9.4.20 and Weld 3.1.2.Final, the Weld/Jetty integration uses the jetty `cdi-decorate` module.
To activate this module in jetty the argument `--module=cdi-decorate` needs to be added to the
command line, which can be done for a standard distribution by running the commands:

-------------------------
cd $JETTY_BASE
java -jar $JETTY_HOME/start.jar --add-to-start=decorate
java -jar $JETTY_HOME/start.jar --add-to-start=cdi-decorate
-------------------------

===== Jetty `cdi2` Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
* @author <a href="mailto:[email protected]">Greg Wilkins</a>
*/
public class JettyContainer extends AbstractJettyContainer {

public static final Container INSTANCE = new JettyContainer();
public static final String JETTY_DECORATING_ATTRIBUTE = "org.eclipse.jetty.webapp.DecoratingListener";
public static final String JETTY_CDI_ATTRIBUTE = "org.eclipse.jetty.cdi";
public static final String JETTY_CDI_VALUE = "CdiDecorator";
public static final String CDI_SPI_DECORATOR_MODE = "CdiSpiDecorator";
public static final String CDI_DECORATING_LISTENER_MODE = "CdiDecoratingListener";
public static final String CDI_DECORATING_LISTENER_ATTRIBUTE = "org.eclipse.jetty.cdi.decorator";
public static final String DECORATING_LISTENER_MODE = "DecoratingListener";
public static final String DECORATING_LISTENER_ATTRIBUTE = "org.eclipse.jetty.webapp.DecoratingListener";

protected String classToCheck() {
// Never called because touch is overridden below.
Expand All @@ -55,24 +57,49 @@ protected String classToCheck() {

@Override
public boolean touch(ResourceLoader resourceLoader, ContainerContext context) throws Exception {
ServletContext sc = context.getServletContext();
// The jetty decorate module from 9.4.20 sets this attribute to indicate that a DecoratingListener is registered.
return sc.getAttribute(JETTY_DECORATING_ATTRIBUTE) instanceof String ||
JETTY_CDI_VALUE.equals(sc.getAttribute(JETTY_CDI_ATTRIBUTE));
ServletContext servletContext = context.getServletContext();
// The jetty cdi modules from 9.4.20 sets JETTY_CDI_ATTRIBUTE to indicate that a
// DecoratingListener is registered. Weld-3.1.2.Final documented that the decorate module
// could be used directly, which sets DECORATING_LISTENER_ATTRIBUTE
return servletContext.getAttribute(JETTY_CDI_ATTRIBUTE) instanceof String ||
servletContext.getAttribute(DECORATING_LISTENER_ATTRIBUTE) instanceof String;
}

@Override
public void initialize(ContainerContext context) {
try {
ServletContext servletContext = context.getServletContext();
// Is the Jetty server doing its own CDI SPI integration?
if (JettyContainer.JETTY_CDI_VALUE.equals(servletContext.getAttribute(JettyContainer.JETTY_CDI_ATTRIBUTE))) {
// Yes, no further integration required
JettyLogger.LOG.jettyCdiSpiIsSupported();
} else {
// No, we need to initialize a JettyWeldInjector and WeldDecorator for it
super.initialize(context);
WeldDecorator.process(servletContext);
String mode = (String)servletContext.getAttribute(JETTY_CDI_ATTRIBUTE);
if (mode == null) {
mode = DECORATING_LISTENER_MODE;
}
switch (mode) {
case CDI_SPI_DECORATOR_MODE:
// For use with the cdi-spi module
// No further integration required
JettyLogger.LOG.jettyCdiSpiIsSupported();
break;

case CDI_DECORATING_LISTENER_MODE:
// For use with the cdi-decorate module
// Initialize a JettyWeldInjector and create WeldDecorator for it
super.initialize(context);
servletContext.setAttribute(CDI_DECORATING_LISTENER_ATTRIBUTE, new WeldDecorator(servletContext));
JettyLogger.LOG.jettyCdiDecorationIsSupported();
break;

case DECORATING_LISTENER_MODE:
// For use with the decorate module
// This mode is only needed to match the Weld-3.1.2 documentation.
// Initialize a JettyWeldInjector and create WeldDecorator for it
super.initialize(context);
String attribute = (String) servletContext.getAttribute(DECORATING_LISTENER_ATTRIBUTE);
servletContext.setAttribute(attribute, new WeldDecorator(servletContext));
JettyLogger.LOG.jettyDecorationIsSupported();
break;

default:
throw JettyLogger.LOG.unknownIntegrationMode(mode);
}
} catch (Exception e) {
JettyLogger.LOG.unableToCreateJettyWeldInjector(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
* @author <a href="mailto:[email protected]">Greg Wilkins</a>
*/
public class WeldDecorator {

private JettyWeldInjector injector;

protected WeldDecorator(ServletContext servletContext) {
Expand All @@ -40,17 +39,6 @@ protected WeldDecorator(ServletContext servletContext) {
}
}

public static void process(ServletContext context) {
String attribute = (String)context.getAttribute(JettyContainer.JETTY_DECORATING_ATTRIBUTE);
if (attribute != null) {
// The jetty decorate module from 9.4.20 listens for attributes as decorators.
context.setAttribute(attribute, new WeldDecorator(context));
JettyLogger.LOG.jettyDecorationIsSupported();
} else {
throw new IllegalStateException("No Jetty Decorating Listener integration detected");
}
}

/**
* Decorate an object.
* <p>The signature of this method must match what is introspected for by the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,18 @@ public interface JettyLogger extends WeldEnvironmentLogger {
@Message(id = 1210, value = "No such Jetty injector found in servlet context attributes.")
IllegalStateException noSuchJettyInjectorFound();

@LogMessage(level = Level.INFO)
@Message(id = 1211, value = "Jetty Decorate support detected, CDI injection will be available in Listeners, Servlets and Filters.")
@LogMessage(level = Level.WARN)
@Message(id = 1211, value = "Deprecated Jetty DecoratingListener support detected, CDI injection will be available in Listeners, Servlets and Filters.")
void jettyDecorationIsSupported();

@LogMessage(level = Level.INFO)
@Message(id = 1212, value = "Jetty CDI SPI support detected, CDI injection will be available in Listeners, Servlets and Filters.")
@Message(id = 1212, value = "Jetty CdiDecoratingListener support detected, CDI injection will be available in Listeners, Servlets and Filters.")
void jettyCdiDecorationIsSupported();

@LogMessage(level = Level.INFO)
@Message(id = 1213, value = "Jetty CDI SPI support detected, CDI injection will be available in Listeners, Servlets and Filters.")
void jettyCdiSpiIsSupported();

@Message(id = 1214, value = "Unknown Jetty integration mode: {0}", format = Format.MESSAGE_FORMAT)
IllegalStateException unknownIntegrationMode(String mode);
}

0 comments on commit c5b20b0

Please sign in to comment.