Skip to content

Commit

Permalink
[WICKET-7080] 1) make default events delivery machinery pluggable 2) …
Browse files Browse the repository at this point in the history
…allow to disabled sending events
  • Loading branch information
reiern70 committed Nov 1, 2023
1 parent 2285816 commit 756ff28
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
8 changes: 6 additions & 2 deletions wicket-core/src/main/java/org/apache/wicket/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -4445,8 +4445,12 @@ public void onEvent(IEvent<?> event)
@Override
public final <T> void send(IEventSink sink, Broadcast type, T payload)
{
new ComponentEventSender(this, getApplication().getFrameworkSettings()).send(sink, type,
payload);
// if there are no event dispatchers then don't even try to send event
if (getApplication().getFrameworkSettings().hasAnyAnyEventDispatchers())
{
new ComponentEventSender(this, getApplication().getFrameworkSettings()).send(sink, type,
payload);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void stop()
stop = true;
}

boolean isStop()
public boolean isStop()
{
return stop;
}
Expand Down
5 changes: 5 additions & 0 deletions wicket-core/src/main/java/org/apache/wicket/event/IEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public interface IEvent<T>
*/
void stop();

/**
* @return true iff event has been stopped.
*/
boolean isStop();

/**
* Stops the broadcast of this event any deeper into the hierarchy of the current sink
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,28 @@
*/
public class FrameworkSettings implements IEventDispatcher
{
/**
* Does the standard delivery of events. Override and do nothing if you want to disable it.
*/
private static class DefaultEventDispatcher implements IEventDispatcher
{
@Override
public void dispatchEvent(Object sink, IEvent<?> event, Component component)
{
// direct delivery
if (component != null && sink instanceof IComponentAwareEventSink)
{
((IComponentAwareEventSink)sink).onEvent(component, event);
}
else if (sink instanceof IEventSink)
{
((IEventSink)sink).onEvent(event);
}
}
}
private IDetachListener detachListener;

private IEventDispatcher defaultEventDispatcher = new DefaultEventDispatcher();
private List<IEventDispatcher> eventDispatchers = null;

/**
Expand All @@ -68,7 +88,7 @@ public FrameworkSettings(final Application application)
* Gets the Wicket version. The Wicket version is in the same format as the version element in
* the pom.xml file (project descriptor). The version is generated by maven in the build/release
* cycle and put in the /META-INF/MANIFEST.MF file located in the root folder of the Wicket jar.
*
* <p></p>
* The version usually follows one of the following formats:
* <ul>
* <li>major.minor[.bug] for stable versions. 1.1, 1.2, 1.2.1 are examples</li>
Expand Down Expand Up @@ -112,7 +132,7 @@ public FrameworkSettings setDetachListener(IDetachListener detachListener)
/**
* Registers a new event dispatcher
*
* @param dispatcher
* @param dispatcher {@link IEventDispatcher}
* @return {@code this} object for chaining
*/
public FrameworkSettings add(IEventDispatcher dispatcher)
Expand All @@ -129,26 +149,30 @@ public FrameworkSettings add(IEventDispatcher dispatcher)
return this;
}

/**
* @return Returns <code>true</code> if there is at least one event dispatcher
*/
public final boolean hasAnyAnyEventDispatchers()
{
if (defaultEventDispatcher != null)
{
return true;
}
return eventDispatchers != null && !eventDispatchers.isEmpty();
}

/**
* Dispatches event to registered dispatchers
*
* @see IEventDispatcher#dispatchEvent(Object, IEvent, Component)
*
* @param sink
* @param event
* @param component
*
*/
@Override
public void dispatchEvent(Object sink, IEvent<?> event, Component component)
{
// direct delivery
if (component != null && sink instanceof IComponentAwareEventSink)
if (defaultEventDispatcher != null)
{
((IComponentAwareEventSink)sink).onEvent(component, event);
}
else if (sink instanceof IEventSink)
{
((IEventSink)sink).onEvent(event);
defaultEventDispatcher.dispatchEvent(sink, event, component);
}

// additional dispatchers delivery
Expand All @@ -162,6 +186,19 @@ else if (sink instanceof IEventSink)
}
}

/**
* Allows to set the default events dispatcher
*
* @param defaultEventDispatcher
* IEventDispatcher
* @return {@code this} object for chaining
*/
public FrameworkSettings setDefaultEventDispatcher(IEventDispatcher defaultEventDispatcher)
{
this.defaultEventDispatcher = defaultEventDispatcher;
return this;
}

/**
* Sets the {@link ISerializer} that will be used to convert objects to/from byte arrays
*
Expand Down

0 comments on commit 756ff28

Please sign in to comment.