EventDispatcher.java 7.6 KB

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