Skip to content

Commit

Permalink
Issue #2578 EventListener
Browse files Browse the repository at this point in the history
Resolve differences between eventListeners added as beans and beans
added as EventListeners.   The behaviour should now be the same
regardless of how they listener is added and all listeners are now
beans.

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Aug 14, 2019
1 parent 03c0144 commit 160ee44
Show file tree
Hide file tree
Showing 29 changed files with 74 additions and 58 deletions.
2 changes: 1 addition & 1 deletion jetty-home/src/main/resources/etc/jetty-setuid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- ================================================================ -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.setuid.SetUIDListener">
<Set name="startServerAsPrivileged"><Property name="jetty.setuid.startServerAsPrivileged" default="false"/></Set>
Expand Down
2 changes: 1 addition & 1 deletion jetty-home/src/main/resources/etc/jetty-started.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- Mixin the Start FileNoticeLifeCycleListener -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.util.component.FileNoticeLifeCycleListener">
<Arg><Property name="jetty.state" default="./jetty.state"/></Arg>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
</New>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Ref id="httpConnector">
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerConnectorListener">
<Set name="fileName"><Property name="jetty.port.file" default="port.txt"/></Set>
Expand Down
2 changes: 1 addition & 1 deletion jetty-maven-plugin/src/main/resources/jetty-maven.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">

<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.maven.plugin.ServerListener">
<Set name="tokenFile"><Property name="jetty.token.file"/></Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.osgi.boot.utils.ServerConnectorListener">
<Set name="sysPropertyName">boot.context.service.port</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.osgi.boot.utils.ServerConnectorListener">
<Set name="sysPropertyName">boot.webapp.service.port</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.osgi.boot.utils.ServerConnectorListener">
<Set name="sysPropertyName">boot.annotations.port</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.osgi.boot.utils.ServerConnectorListener">
<Set name="sysPropertyName">boot.bundle.port</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.osgi.boot.utils.ServerConnectorListener">
<Set name="sysPropertyName">boot.javax.websocket.port</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.osgi.boot.utils.ServerConnectorListener">
<Set name="sysPropertyName">boot.jsp.port</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.osgi.boot.utils.ServerConnectorListener">
<Set name="sysPropertyName">boot.websocket.port</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</Item>
</Array>
</Arg>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.osgi.boot.utils.ServerConnectorListener">
<Set name="sysPropertyName">foo.foo</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</New>
</Arg>
</Call>
<Call name="addLifeCycleListener">
<Call name="addEventListener">
<Arg>
<New class="org.eclipse.jetty.osgi.boot.utils.ServerConnectorListener">
<Set name="sysPropertyName">boot.https.port</Set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ public void dump(Appendable out, String indent) throws IOException
{
dumpObjects(out, indent,
new ClassLoaderDump(getClassLoader()),
new DumpableCollection("eventListeners " + this, _eventListeners),
new DumpableCollection("handler attributes " + this, ((AttributesMap)getAttributes()).getAttributeEntrySet()),
new DumpableCollection("context attributes " + this, ((Context)getServletContext()).getAttributeEntrySet()),
new DumpableCollection("initparams " + this, getInitParams().entrySet()));
Expand Down Expand Up @@ -688,6 +687,8 @@ public void addEventListener(EventListener listener)
*/
public void removeEventListener(EventListener listener)
{
super.removeEventListener(listener);

_eventListeners.remove(listener);

if (listener instanceof ContextScopeListener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
* If adding a bean that is shared between multiple {@link ContainerLifeCycle} instances, then it should be started
* before being added, so it is unmanaged, or the API must be used to explicitly set it as unmanaged.
* <p>
* All {@link EventListener}s added via {@link #addEventListener(EventListener)} are also added as beans and all beans
* added via an {@link #addBean(Object)} method that are also {@link EventListener}s are added as listeners via a
* call to {@link #addEventListener(EventListener)}.
* <p>
* This class also provides utility methods to dump deep structures of objects.
* In the dump, the following symbols are used to indicate the type of contained object:
* <pre>
Expand Down Expand Up @@ -347,18 +351,17 @@ public boolean addBean(Object o, Managed managed)

Bean newBean = new Bean(o);

// if the bean is a Listener
if (o instanceof EventListener)
addEventListener((EventListener)o);

// Add the bean
_beans.add(newBean);

// Tell existing listeners about the new bean
// Tell any existing listeners about the new bean
for (Container.Listener l : _listeners)
{
l.beanAdded(this, o);
}

// if the bean is a Listener. Note we check the _eventListenerBeans here
// to avoid calling extended version of addEventListener before detecting it is already added.
if (o instanceof EventListener && !_eventListenerBeans.contains(o))
addEventListener((EventListener)o);

try
{
Expand Down Expand Up @@ -457,12 +460,20 @@ public void addManaged(LifeCycle lifecycle)
@Override
public void addEventListener(EventListener listener)
{
// Has it already been added as a listener?
if (_eventListenerBeans.contains(listener))
// yes - nothing to do
return;

super.addEventListener(listener);
_eventListenerBeans.add(listener);

// If it is not yet a bean,
if (!contains(listener))
// add it as a bean first, we wont be called back because we've already added it to the _eventListenerBeans list
// but we've not added it to the _listeners list, so it won't be told about itself!
addBean(listener);

if (listener instanceof Container.Listener)
{
Container.Listener cl = (Container.Listener)listener;
Expand All @@ -485,6 +496,27 @@ public void addEventListener(EventListener listener)
}
}

@Override
public void removeEventListener(EventListener listener)
{
if (_eventListenerBeans.remove(listener))
{
super.removeEventListener(listener);
removeBean(listener);
if (_listeners.remove(listener))
{
// remove existing beans
for (Bean b : _beans)
{
((Container.Listener)listener).beanRemoved(this, b._bean);

if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
((Container)b._bean).removeBean(listener);
}
}
}
}

/**
* Manages a bean already contained by this aggregate, so that it is started/stopped/destroyed with this
* aggregate.
Expand Down Expand Up @@ -651,7 +683,8 @@ private boolean remove(Bean bean)
l.beanRemoved(this, bean._bean);
}

if (bean._bean instanceof EventListener)
// Remove event listeners, checking list here to avoid calling extended removeEventListener if already removed.
if (bean._bean instanceof EventListener && _eventListenerBeans.contains(bean._bean))
removeEventListener((EventListener)bean._bean);

// stop managed beans
Expand All @@ -675,24 +708,6 @@ private boolean remove(Bean bean)
return false;
}

@Override
public void removeEventListener(EventListener listener)
{
_eventListenerBeans.remove(listener);
super.removeEventListener(listener);
if (_listeners.remove(listener))
{
// remove existing beans
for (Bean b : _beans)
{
((Container.Listener)listener).beanRemoved(this, b._bean);

if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
((Container)b._bean).removeBean(listener);
}
}
}

@Override
public void setStopTimeout(long stopTimeout)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,20 +447,20 @@ String toString()

c0.addBean(inherited);

assertEquals("inherited", handled.poll());
assertEquals("listener", handled.poll());
assertEquals("added", operation.poll());
assertEquals(c0, parent.poll());
assertEquals(c00, child.poll());
assertEquals(inherited, child.poll());

assertEquals("inherited", handled.poll());
assertEquals("added", operation.poll());
assertEquals(c0, parent.poll());
assertEquals(listener, child.poll());
assertEquals(c00, child.poll());

assertEquals("listener", handled.poll());
assertEquals("inherited", handled.poll());
assertEquals("added", operation.poll());
assertEquals(c0, parent.poll());
assertEquals(inherited, child.poll());
assertEquals(listener, child.poll());

assertEquals("inherited", handled.poll());
assertEquals("added", operation.poll());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testSessionListeners()
server.setHandler(wac);
wac.addEventListener(new MySessionListener());

Collection<MySessionListener> listeners = wac.getSessionHandler().getBeans(org.eclipse.jetty.webapp.WebAppContextTest.MySessionListener.class);
Collection<MySessionListener> listeners = wac.getSessionHandler().getBeans(MySessionListener.class);
assertNotNull(listeners);
assertEquals(1, listeners.size());
}
Expand Down

0 comments on commit 160ee44

Please sign in to comment.