EventDispatcher.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 final 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 hasListener(event.getType()) ? notifyEvent(event, null, 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 hasListener(event.getType()) ? notifyEvent(event, null, callbackClass) : null;
  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 hasListener(event.getType()) || container.hasListener(event.getType()) ? notifyEventImpl(event, container, callbackClass) : null;
  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. * Executing current listener notification asynchronously
  77. * @param event
  78. * @param containers
  79. */
  80. public void notifyEventAsync(IBaseEvent event, ListenersContainer... containers)
  81. {
  82. if (event == null)
  83. {
  84. throw new NullPointerException("Event cannot be null!");
  85. }
  86. boolean hasListeners = hasListener(event.getType());
  87. if (!hasListeners)
  88. {
  89. for (ListenersContainer container : containers)
  90. {
  91. if (container.hasListener(event.getType()))
  92. {
  93. hasListeners = true;
  94. break;
  95. }
  96. }
  97. }
  98. if (hasListeners)
  99. {
  100. ThreadPoolManager.getInstance().executeEvent(() -> notifyEventToMultipleContainers(event, containers, null));
  101. }
  102. }
  103. /**
  104. * Scheduling current listener notification asynchronously after specified delay.
  105. * @param event
  106. * @param container
  107. * @param delay
  108. */
  109. public void notifyEventAsyncDelayed(IBaseEvent event, ListenersContainer container, long delay)
  110. {
  111. if (hasListener(event.getType()) || container.hasListener(event.getType()))
  112. {
  113. ThreadPoolManager.getInstance().scheduleEvent(() -> notifyEvent(event, container, null), delay);
  114. }
  115. }
  116. /**
  117. * Scheduling current listener notification asynchronously after specified delay.
  118. * @param event
  119. * @param container
  120. * @param delay
  121. * @param unit
  122. */
  123. public void notifyEventAsyncDelayed(IBaseEvent event, ListenersContainer container, long delay, TimeUnit unit)
  124. {
  125. if (hasListener(event.getType()) || container.hasListener(event.getType()))
  126. {
  127. ThreadPoolManager.getInstance().scheduleEvent(() -> notifyEvent(event, container, null), delay, unit);
  128. }
  129. }
  130. /**
  131. * @param <T>
  132. * @param event
  133. * @param containers
  134. * @param callbackClass
  135. * @return
  136. */
  137. private <T extends AbstractEventReturn> T notifyEventToMultipleContainers(IBaseEvent event, ListenersContainer[] containers, Class<T> callbackClass)
  138. {
  139. try
  140. {
  141. if (event == null)
  142. {
  143. throw new NullPointerException("Event cannot be null!");
  144. }
  145. T callback = null;
  146. if (containers != null)
  147. {
  148. // Local listeners container first.
  149. for (ListenersContainer container : containers)
  150. {
  151. if ((callback == null) || !callback.abort())
  152. {
  153. callback = notifyToListeners(container.getListeners(event.getType()), event, callbackClass, callback);
  154. }
  155. }
  156. }
  157. // Global listener container.
  158. if ((callback == null) || !callback.abort())
  159. {
  160. callback = notifyToListeners(getListeners(event.getType()), event, callbackClass, callback);
  161. }
  162. return callback;
  163. }
  164. catch (Exception e)
  165. {
  166. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't notify event " + event.getClass().getSimpleName(), e);
  167. }
  168. return null;
  169. }
  170. /**
  171. * @param <T>
  172. * @param event
  173. * @param container
  174. * @param callbackClass
  175. * @return {@link AbstractEventReturn} object that may keep data from the first listener, or last that breaks notification.
  176. */
  177. private <T extends AbstractEventReturn> T notifyEventImpl(IBaseEvent event, ListenersContainer container, Class<T> callbackClass)
  178. {
  179. if (event == null)
  180. {
  181. throw new NullPointerException("Event cannot be null!");
  182. }
  183. T callback = null;
  184. // Local listener container first.
  185. if (container != null)
  186. {
  187. callback = notifyToListeners(container.getListeners(event.getType()), event, callbackClass, callback);
  188. }
  189. // Global listener container.
  190. if ((callback == null) || !callback.abort())
  191. {
  192. callback = notifyToListeners(getListeners(event.getType()), event, callbackClass, callback);
  193. }
  194. return callback;
  195. }
  196. /**
  197. * @param <T>
  198. * @param listeners
  199. * @param event
  200. * @param returnBackClass
  201. * @param callback
  202. * @return
  203. */
  204. private <T extends AbstractEventReturn> T notifyToListeners(Queue<AbstractEventListener> listeners, IBaseEvent event, Class<T> returnBackClass, T callback)
  205. {
  206. for (AbstractEventListener listener : listeners)
  207. {
  208. try
  209. {
  210. final T rb = listener.executeEvent(event, returnBackClass);
  211. if (rb == null)
  212. {
  213. continue;
  214. }
  215. 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
  216. {
  217. callback = rb;
  218. }
  219. else if (rb.abort()) // This listener wants to abort the notification to others.
  220. {
  221. break;
  222. }
  223. }
  224. catch (Exception e)
  225. {
  226. _log.log(Level.WARNING, getClass().getSimpleName() + ": Exception during notification of event: " + event.getClass().getSimpleName() + " listener: " + listener.getClass().getSimpleName(), e);
  227. }
  228. }
  229. return callback;
  230. }
  231. public static EventDispatcher getInstance()
  232. {
  233. return SingletonHolder._instance;
  234. }
  235. private static class SingletonHolder
  236. {
  237. protected static final EventDispatcher _instance = new EventDispatcher();
  238. }
  239. }