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 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions jetty-cdi/pom.xml
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
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
}
}
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
Expand Up @@ -65,7 +65,10 @@ public class CdiSpiDecorator implements Decorator
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 +95,35 @@ public CdiSpiDecorator(ServletContextHandler context) throws UnsupportedOperatio
}
}

/**
* Test if a class can be decorated.
* The default implementation calls {@link #isKnownUndecoratable(String) }
* 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 (isKnownUndecoratable(clazz.getName()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Not at all fond of the name isKnownDecoratable(). How about instead you remove isKnownDecoratable() and add a field list of _undecoratables, a setter for them, and in isDecoratable() you just check the list? This might also be a good idea in case CDI adds some other classes that can't be decorated?

return false;
return isDecoratable(clazz.getSuperclass());
}

/**
* Test if a specific class name is known to not be decoratable.
* The default implementation checks for well known classes that are used to
* setup CDI itself, and thus cannot themselves be decorated.
* @see #isDecoratable(Class)
* @param className The name of the class to check
* @return True if the class is known not to be decoratable
*/
protected boolean isKnownUndecoratable(String className)
{
return "org.jboss.weld.environment.servlet.Listener".equals(className);
gregw marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Decorate an object.
* <p>The signature of this method must match what is introspected for by the
Expand All @@ -108,7 +140,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