Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #5162 CDI embedded integration improvements #5177

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
gregw marked this conversation as resolved.
Show resolved Hide resolved
{
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);
gregw marked this conversation as resolved.
Show resolved Hide resolved
}
}
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();
gregw marked this conversation as resolved.
Show resolved Hide resolved

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 #getUndecoratedClasses()}
* 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 (getUndecoratedClasses().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> getUndecoratedClasses()
{
return _undecorated;
}

/**
* @param undecorated The set of class names that will not be decorated.
* @see #isDecoratable(Class)
*/
public void setUndecoratedClasses(Set<String> undecorated)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant "setUndecoratableClasses". Undecorated makes it sound like they haven't been decorated yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janb I saw that, but I think Undecorated also works, is less of a mouthful and eitherway you need to check with the javadoc to really know what it means.

{
_undecorated.clear();
if (undecorated != null)
_undecorated.addAll(undecorated);
}

/**
* @param undecorated A class name that will be added to the undecoratable classes set.
* @see #getUndecoratedClasses()
* @see #isDecoratable(Class)
*/
public void addUndecoratedClass(String... undecorated)
{
_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