EventDispatcher.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.events;
  20. import java.util.Queue;
  21. import java.util.concurrent.TimeUnit;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.gameserver.ThreadPoolManager;
  25. import com.l2jserver.gameserver.model.events.impl.IBaseEvent;
  26. import com.l2jserver.gameserver.model.events.listeners.AbstractEventListener;
  27. import com.l2jserver.gameserver.model.events.returns.AbstractEventReturn;
  28. /**
  29. * @author UnAfraid
  30. */
  31. public class EventDispatcher extends ListenersContainer
  32. {
  33. private static final Logger _log = Logger.getLogger(EventDispatcher.class.getName());
  34. protected EventDispatcher()
  35. {
  36. }
  37. /**
  38. * @param <T>
  39. * @param event
  40. * @return
  41. */
  42. public <T extends AbstractEventReturn> T notifyEvent(IBaseEvent event)
  43. {
  44. return notifyEvent(event, null, null);
  45. }
  46. /**
  47. * @param <T>
  48. * @param event
  49. * @param callbackClass
  50. * @return
  51. */
  52. public <T extends AbstractEventReturn> T notifyEvent(IBaseEvent event, Class<T> callbackClass)
  53. {
  54. return notifyEvent(event, null, callbackClass);
  55. }
  56. /**
  57. * @param <T>
  58. * @param event
  59. * @param container
  60. * @param callbackClass
  61. * @return
  62. */
  63. public <T extends AbstractEventReturn> T notifyEvent(IBaseEvent event, ListenersContainer container, Class<T> callbackClass)
  64. {
  65. try
  66. {
  67. return notifyEventImpl(event, container, callbackClass);
  68. }
  69. catch (Exception e)
  70. {
  71. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't notify event " + event.getClass().getSimpleName(), e);
  72. }
  73. return null;
  74. }
  75. /**
  76. * @param <T>
  77. * @param event
  78. * @param containers
  79. * @param callbackClass
  80. * @return
  81. */
  82. public <T extends AbstractEventReturn> T notifyEventToMultipleContainers(IBaseEvent event, ListenersContainer[] containers, Class<T> callbackClass)
  83. {
  84. try
  85. {
  86. if (event == null)
  87. {
  88. throw new NullPointerException("Event cannot be null!");
  89. }
  90. T callback = null;
  91. if (containers != null)
  92. {
  93. // Local listeners container first.
  94. for (ListenersContainer container : containers)
  95. {
  96. if ((callback == null) || !callback.abort())
  97. {
  98. callback = notifyToListeners(container.getListeners(event.getType()), event, callbackClass, callback);
  99. }
  100. }
  101. }
  102. // Global listener container.
  103. if ((callback == null) || !callback.abort())
  104. {
  105. callback = notifyToListeners(getListeners(event.getType()), event, callbackClass, callback);
  106. }
  107. return callback;
  108. }
  109. catch (Exception e)
  110. {
  111. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't notify event " + event.getClass().getSimpleName(), e);
  112. }
  113. return null;
  114. }
  115. /**
  116. * @param <T>
  117. * @param event
  118. * @param container
  119. * @param callbackClass
  120. * @return {@link AbstractEventReturn} object that may keep data from the first listener, or last that breaks notification.
  121. */
  122. private <T extends AbstractEventReturn> T notifyEventImpl(IBaseEvent event, ListenersContainer container, Class<T> callbackClass)
  123. {
  124. if (event == null)
  125. {
  126. throw new NullPointerException("Event cannot be null!");
  127. }
  128. T callback = null;
  129. // Local listener container first.
  130. if (container != null)
  131. {
  132. callback = notifyToListeners(container.getListeners(event.getType()), event, callbackClass, callback);
  133. }
  134. // Global listener container.
  135. if ((callback == null) || !callback.abort())
  136. {
  137. callback = notifyToListeners(getListeners(event.getType()), event, callbackClass, callback);
  138. }
  139. return callback;
  140. }
  141. /**
  142. * @param <T>
  143. * @param listeners
  144. * @param event
  145. * @param returnBackClass
  146. * @param callback
  147. * @return
  148. */
  149. private <T extends AbstractEventReturn> T notifyToListeners(Queue<AbstractEventListener> listeners, IBaseEvent event, Class<T> returnBackClass, T callback)
  150. {
  151. for (AbstractEventListener listener : listeners)
  152. {
  153. try
  154. {
  155. final T rb = listener.executeEvent(event, returnBackClass);
  156. if (rb == null)
  157. {
  158. continue;
  159. }
  160. else if ((callback == null) || rb.override()) // Let's check if this listener wants to override previous return object or we simply don't have one
  161. {
  162. callback = rb;
  163. }
  164. else if (rb.abort()) // This listener wants to abort the notification to others.
  165. {
  166. break;
  167. }
  168. }
  169. catch (Exception e)
  170. {
  171. _log.log(Level.WARNING, getClass().getSimpleName() + ": Exception during notification of event: " + event.getClass().getSimpleName() + " listener: " + listener.getClass().getSimpleName(), e);
  172. }
  173. }
  174. return callback;
  175. }
  176. /**
  177. * Executing global listener notification asynchronously
  178. * @param event
  179. */
  180. public void notifyEventAsync(final IBaseEvent event)
  181. {
  182. notifyEventAsync(event);
  183. }
  184. /**
  185. * Executing current listener notification asynchronously
  186. * @param event
  187. * @param containers
  188. */
  189. public void notifyEventAsync(final IBaseEvent event, ListenersContainer... containers)
  190. {
  191. if (event == null)
  192. {
  193. throw new NullPointerException("Event cannot be null!");
  194. }
  195. ThreadPoolManager.getInstance().executeEvent(() -> notifyEventToMultipleContainers(event, containers, null));
  196. }
  197. /**
  198. * Scheduling current listener notification asynchronously after specified delay.
  199. * @param event
  200. * @param container
  201. * @param delay
  202. */
  203. public void notifyEventAsyncDelayed(final IBaseEvent event, ListenersContainer container, long delay)
  204. {
  205. ThreadPoolManager.getInstance().scheduleEvent(() -> notifyEvent(event, container, null), delay);
  206. }
  207. /**
  208. * Scheduling current listener notification asynchronously after specified delay.
  209. * @param event
  210. * @param container
  211. * @param delay
  212. * @param unit
  213. */
  214. public void notifyEventAsyncDelayed(final IBaseEvent event, ListenersContainer container, long delay, TimeUnit unit)
  215. {
  216. ThreadPoolManager.getInstance().scheduleEvent(() -> notifyEvent(event, container, null), delay, unit);
  217. }
  218. public static final EventDispatcher getInstance()
  219. {
  220. return SingletonHolder._instance;
  221. }
  222. private static class SingletonHolder
  223. {
  224. protected static final EventDispatcher _instance = new EventDispatcher();
  225. }
  226. }