Skip to content

Commit

Permalink
Fixes #12588 - oejhs.AbstractHTTP2ServerConnectionFactory installs th…
Browse files Browse the repository at this point in the history
…e HTTP2SessionContainer bean twice.

Fixed ordering in `installBean()`: first add the bean, then add it as a listener, so the bean is not added twice.

This is now similar to what `addBean()` is doing.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
  • Loading branch information
sbordet committed Nov 29, 2024
1 parent 664d267 commit 8e810a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,13 @@ private boolean installBean(Object o, Managed managed)
if (o instanceof Container.Listener || !_listeners.isEmpty())
throw new IllegalArgumentException("Cannot call Container.Listeners from constructor");

if (o instanceof EventListener eventListener)
addEventListener(eventListener);

Bean newBean = new Bean(o);
newBean._managed = managed;
_beans.add(newBean);

if (o instanceof EventListener eventListener)
addEventListener(eventListener);

if (LOG.isDebugEnabled())
LOG.debug("{}@{} added {}", getClass().getSimpleName(), hashCode(), newBean);
return true;
Expand Down Expand Up @@ -499,8 +499,8 @@ public boolean addEventListener(EventListener listener)
{
// If it is not yet a bean,
if (!contains(listener))
// add it as a bean, we will be called back to add it as an event listener, but it will have
// already been added, so we will not enter this branch.
// add it as a bean, we will be called back to add it as an event listener,
// but it will have already been added, so we will not enter this branch.
addBean(listener);

if (listener instanceof Container.Listener cl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Collection;
import java.util.EventListener;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -728,4 +730,18 @@ protected void doStart() throws Exception
assertTrue(bean.isFailed());
assertTrue(container.isUnmanaged(bean));
}

@Test
public void testInstallBeanThatImplementsEventListener()
{
class Bean implements EventListener
{
}

ContainerLifeCycle container = new ContainerLifeCycle();
container.installBean(new Bean());

Collection<Bean> beans = container.getBeans(Bean.class);
assertEquals(1, beans.size());
}
}

0 comments on commit 8e810a7

Please sign in to comment.