Skip to content

Commit

Permalink
Add support for Jetty 12
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay committed Aug 22, 2023
1 parent b57d3e9 commit b05955a
Showing 1 changed file with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import jakarta.servlet.ServletContext;

import java.util.Map;
import java.util.Map.Entry;

import org.jboss.weld.environment.servlet.Container;
import org.jboss.weld.environment.servlet.ContainerContext;
import org.jboss.weld.environment.servlet.logging.JettyLogger;
Expand All @@ -43,33 +46,54 @@
*/
public class JettyContainer extends AbstractJettyContainer {
public static final Container INSTANCE = new JettyContainer();
public static final String JETTY_CDI_ATTRIBUTE = "org.eclipse.jetty.cdi";
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";
public static final Map<String, String> CDI_DECORATING_LISTENER_ATTRIBUTE_MAP = Map.of(
"org.eclipse.jetty.cdi", "org.eclipse.jetty.cdi.decorator",
"org.eclipse.jetty.ee9.cdi", "org.eclipse.jetty.ee9.cdi.decorator",
"org.eclipse.jetty.ee10.cdi" ,"org.eclipse.jetty.ee10.cdi.decorator");

protected String classToCheck() {
// Never called because touch is overridden below.
throw new UnsupportedOperationException("touch method reimplemented in JettyContainer");
}

protected String getCdiAttribute(ServletContext servletContext) {
for(Entry<String, String> entry : CDI_DECORATING_LISTENER_ATTRIBUTE_MAP.entrySet()) {
Object value = servletContext.getAttribute(entry.getKey());
if(value instanceof String) {
return (String) value;
}
}
return null;
}

protected String getCdiDecoratingListenerAttribute(ServletContext servletContext) {
for(Entry<String, String> entry : CDI_DECORATING_LISTENER_ATTRIBUTE_MAP.entrySet()) {
if(servletContext.getAttribute(entry.getKey()) instanceof String) {
return entry.getValue();
}
}
return null;
}

@Override
public boolean touch(ResourceLoader resourceLoader, ContainerContext context) throws Exception {
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 ||
return getCdiAttribute(servletContext) instanceof String ||
servletContext.getAttribute(DECORATING_LISTENER_ATTRIBUTE) instanceof String;
}

@Override
public void initialize(ContainerContext context) {
try {
ServletContext servletContext = context.getServletContext();
String mode = (String)servletContext.getAttribute(JETTY_CDI_ATTRIBUTE);
String mode = getCdiAttribute(servletContext);
if (mode == null) {
mode = DECORATING_LISTENER_MODE;
}
Expand All @@ -84,7 +108,7 @@ public void initialize(ContainerContext context) {
// 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));
servletContext.setAttribute(getCdiDecoratingListenerAttribute(servletContext), new WeldDecorator(servletContext));
JettyLogger.LOG.jettyCdiDecorationIsSupported();
break;

Expand Down

0 comments on commit b05955a

Please sign in to comment.