Skip to content

Commit

Permalink
Issue #5162 CDI embedded integration improvements (#5177)
Browse files Browse the repository at this point in the history
* Issue #5162 CDI embedded integration improvements

Clean up CDI integration and documentation to better support embedded usage.
 + made listener public
 + added utility class for SCIs

* Issue #5162 CDI embedded integration improvements

Clean up CDI integration and documentation to better support embedded usage.
 + moved EmbeddedWeldTest to jetty-embedded

* fix javadoc

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Issue #5162 CDI embedded integration improvements

ventilated text

* fix test pom

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Fixed javadoc

* Fixed javadoc

* Issue #5162 CDI embedded integration improvements

Moved tests to jetty-cdi to avoid consequences to other tests in embedded

* trailing new line

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* updates from review

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* Feedback from review
  • Loading branch information
gregw authored Sep 7, 2020
1 parent 2826df2 commit 7ecf42e
Show file tree
Hide file tree
Showing 15 changed files with 716 additions and 130 deletions.
6 changes: 6 additions & 0 deletions jetty-cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<version>${weld.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
/**
* A DecoratingListener that listens for "org.eclipse.jetty.cdi.decorator"
*/
class CdiDecoratingListener extends DecoratingListener
public class CdiDecoratingListener extends DecoratingListener
{
public static final String MODE = "CdiDecoratingListener";
public static final String ATTRIBUTE = "org.eclipse.jetty.cdi.decorator";

public CdiDecoratingListener(ServletContextHandler contextHandler)
{
super(contextHandler, ATTRIBUTE);
contextHandler.setAttribute(CdiServletContainerInitializer.CDI_INTEGRATION_ATTRIBUTE, MODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx)
default:
throw new IllegalStateException(mode);
}

context.setAttribute(CDI_INTEGRATION_ATTRIBUTE, mode);
LOG.info(mode + " enabled in " + ctx);
}
catch (UnsupportedOperationException | ClassNotFoundException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.Decorator;
Expand Down Expand Up @@ -61,11 +65,15 @@ public class CdiSpiDecorator implements Decorator
private final MethodHandle _inject;
private final MethodHandle _dispose;
private final MethodHandle _release;
private final Set<String> _undecorated = new HashSet<>(Collections.singletonList("org.jboss.weld.environment.servlet.Listener"));

public CdiSpiDecorator(ServletContextHandler context) throws UnsupportedOperationException
{
_context = context;
context.setAttribute(CdiServletContainerInitializer.CDI_INTEGRATION_ATTRIBUTE, MODE);
ClassLoader classLoader = _context.getClassLoader();
if (classLoader == null)
classLoader = this.getClass().getClassLoader();

try
{
Expand All @@ -92,6 +100,54 @@ public CdiSpiDecorator(ServletContextHandler context) throws UnsupportedOperatio
}
}

/**
* Test if a class can be decorated.
* The default implementation checks the set from {@link #getUndecoratable()}
* on the class and all it's super classes.
* @param clazz The class to check
* @return True if the class and all it's super classes can be decorated
*/
protected boolean isDecoratable(Class<?> clazz)
{
if (Object.class == clazz)
return true;
if (getUndecoratable().contains(clazz.getName()))
return false;
return isDecoratable(clazz.getSuperclass());
}

/**
* Get the set of classes that will not be decorated. The default set includes the listener from Weld that will itself
* setup decoration.
* @return The modifiable set of class names that will not be decorated (ie {@link #isDecoratable(Class)} will return false.
* @see #isDecoratable(Class)
*/
public Set<String> getUndecoratable()
{
return _undecorated;
}

/**
* @param classnames The set of class names that will not be decorated.
* @see #isDecoratable(Class)
*/
public void setUndecoratable(Set<String> classnames)
{
_undecorated.clear();
if (classnames != null)
_undecorated.addAll(classnames);
}

/**
* @param classname A class name that will be added to the undecoratable classes set.
* @see #getUndecoratable()
* @see #isDecoratable(Class)
*/
public void addUndecoratable(String... classname)
{
_undecorated.addAll(Arrays.asList());
}

/**
* Decorate an object.
* <p>The signature of this method must match what is introspected for by the
Expand All @@ -108,7 +164,8 @@ public <T> T decorate(T o)
if (LOG.isDebugEnabled())
LOG.debug("decorate {} in {}", o, _context);

_decorated.put(o, new Decorated(o));
if (isDecoratable(o.getClass()))
_decorated.put(o, new Decorated(o));
}
catch (Throwable th)
{
Expand Down
Loading

0 comments on commit 7ecf42e

Please sign in to comment.