Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CallbackManager.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.common;
  23. import com.dmdirc.parser.interfaces.Parser;
  24. import com.dmdirc.parser.interfaces.SpecificCallback;
  25. import com.dmdirc.parser.interfaces.callbacks.*; //NOPMD
  26. import java.lang.reflect.InvocationHandler;
  27. import java.lang.reflect.Method;
  28. import java.lang.reflect.Proxy;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. /**
  33. * Parser Callback Manager.
  34. * Manages adding/removing/calling callbacks.
  35. */
  36. public class CallbackManager {
  37. private static final Class[] CLASSES = {
  38. AwayStateListener.class,
  39. OtherAwayStateListener.class,
  40. ChannelOtherAwayStateListener.class,
  41. ChannelActionListener.class,
  42. ChannelCtcpListener.class,
  43. ChannelCtcpReplyListener.class,
  44. ChannelListModeListener.class,
  45. ChannelNamesListener.class,
  46. ChannelJoinListener.class,
  47. ChannelKickListener.class,
  48. ChannelMessageListener.class,
  49. ChannelModeChangeListener.class,
  50. ChannelNickChangeListener.class,
  51. ChannelNonUserModeChangeListener.class,
  52. ChannelModeMessageListener.class,
  53. ChannelModeNoticeListener.class,
  54. ChannelNoticeListener.class,
  55. ChannelPartListener.class,
  56. ChannelQuitListener.class,
  57. ChannelSelfJoinListener.class,
  58. ChannelSingleModeChangeListener.class,
  59. ChannelTopicListener.class,
  60. ChannelUserModeChangeListener.class,
  61. CompositionStateChangeListener.class,
  62. ConnectErrorListener.class,
  63. DataInListener.class,
  64. DataOutListener.class,
  65. DebugInfoListener.class,
  66. ErrorInfoListener.class,
  67. GroupListStartListener.class,
  68. GroupListEntryListener.class,
  69. GroupListEndListener.class,
  70. NetworkDetectedListener.class,
  71. InviteListener.class,
  72. MotdEndListener.class,
  73. MotdLineListener.class,
  74. MotdStartListener.class,
  75. NickChangeListener.class,
  76. NickInUseListener.class,
  77. AuthNoticeListener.class,
  78. NumericListener.class,
  79. PasswordRequiredListener.class,
  80. PingFailureListener.class,
  81. PingSuccessListener.class,
  82. PingSentListener.class,
  83. PrivateActionListener.class,
  84. PrivateCtcpListener.class,
  85. PrivateCtcpReplyListener.class,
  86. PrivateMessageListener.class,
  87. PrivateNoticeListener.class,
  88. QuitListener.class,
  89. ServerErrorListener.class,
  90. ServerReadyListener.class,
  91. SocketCloseListener.class,
  92. UnknownActionListener.class,
  93. UnknownCtcpListener.class,
  94. UnknownCtcpReplyListener.class,
  95. UnknownMessageListener.class,
  96. UnknownNoticeListener.class,
  97. UserModeChangeListener.class,
  98. UserModeDiscoveryListener.class,
  99. WallDesyncListener.class,
  100. WallopListener.class,
  101. WalluserListener.class,
  102. ServerNoticeListener.class,
  103. UnknownServerNoticeListener.class,
  104. };
  105. /** Hashtable used to store the different types of callback known. */
  106. private final Map<Class<? extends CallbackInterface>, CallbackObject> callbackHash
  107. = new HashMap<Class<? extends CallbackInterface>, CallbackObject>();
  108. /** A map of implementations to use for parser interfaces. */
  109. private final Map<Class<?>, Class<?>> implementationMap;
  110. /**
  111. * Constructor to create a CallbackManager.
  112. *
  113. * @param parser Parser that owns this callback manager.
  114. * @param implementationMap A map of implementations to use
  115. */
  116. public CallbackManager(final Parser parser, final Map<Class<?>, Class<?>> implementationMap) {
  117. this.implementationMap = implementationMap;
  118. initialise(parser);
  119. }
  120. /**
  121. * Initialises this callback manager.
  122. *
  123. * @param parser The parser associated with this CallbackManager
  124. */
  125. protected void initialise(final Parser parser) {
  126. for (Class<?> type : CLASSES) {
  127. if (type.isAnnotationPresent(SpecificCallback.class)) {
  128. addCallbackType(getSpecificCallbackObject(parser, type));
  129. } else {
  130. addCallbackType(getCallbackObject(parser, type));
  131. }
  132. }
  133. }
  134. /**
  135. * Retrieves a relevant {@link CallbackObject} for the specified type.
  136. *
  137. * @param parser The parser that this manager belongs to
  138. * @param type The type of callback to create an object for
  139. * @return The relevant CallbackObject
  140. */
  141. protected CallbackObject getCallbackObject(final Parser parser, final Class<?> type) {
  142. return new CallbackObject(parser, this, type.asSubclass(CallbackInterface.class), implementationMap);
  143. }
  144. /**
  145. * Retrieves a relevant {@link CallbackObjectSpecific} for the specified type.
  146. *
  147. * @param parser The parser that this manager belongs to
  148. * @param type The type of callback to create an object for
  149. * @return The relevant CallbackObject
  150. */
  151. protected CallbackObjectSpecific getSpecificCallbackObject(final Parser parser, final Class<?> type) {
  152. return new CallbackObjectSpecific(parser, this, type.asSubclass(CallbackInterface.class), implementationMap);
  153. }
  154. /**
  155. * Add new callback type.
  156. *
  157. * @param callback CallbackObject subclass for the callback.
  158. * @return if adding succeeded or not.
  159. */
  160. public boolean addCallbackType(final CallbackObject callback) {
  161. if (!callbackHash.containsKey(callback.getType())) {
  162. callbackHash.put(callback.getType(), callback);
  163. return true;
  164. }
  165. return false;
  166. }
  167. /**
  168. * Remove a callback type.
  169. *
  170. * @param callback CallbackObject subclass to remove.
  171. * @return if removal succeeded or not.
  172. */
  173. public boolean delCallbackType(final CallbackObject callback) {
  174. if (callbackHash.containsKey(callback.getType())) {
  175. callbackHash.remove(callback.getType());
  176. return true;
  177. }
  178. return false;
  179. }
  180. /**
  181. * Get reference to callback object.
  182. *
  183. * @param callback Name of type of callback object.
  184. * @return CallbackObject returns the callback object for this type
  185. */
  186. public CallbackObject getCallbackType(final Class<? extends CallbackInterface> callback) {
  187. if (!callbackHash.containsKey(callback)) {
  188. throw new CallbackNotFoundException("Callback not found: " + callback.getName()
  189. + "\n\nMy class: " + getClass().getName()
  190. + "\nContents: " + callbackHash.keySet()
  191. + "\nThread: " + Thread.currentThread().getName());
  192. }
  193. return callbackHash.get(callback);
  194. }
  195. /**
  196. * Remove all callbacks associated with a specific object.
  197. *
  198. * @param o instance of ICallbackInterface to remove.
  199. */
  200. public void delAllCallback(final CallbackInterface o) {
  201. for (CallbackObject cb : callbackHash.values()) {
  202. if (cb != null && cb.getType().isInstance(o)) {
  203. cb.del(o);
  204. }
  205. }
  206. }
  207. /**
  208. * Add all callbacks that this object implements.
  209. *
  210. * @param o instance of ICallbackInterface to add.
  211. */
  212. public void addAllCallback(final CallbackInterface o) {
  213. for (CallbackObject cb : callbackHash.values()) {
  214. if (cb != null && cb.getType().isInstance(o)) {
  215. cb.add(o);
  216. }
  217. }
  218. }
  219. /**
  220. * Add a callback.
  221. * This method will throw a CallbackNotFoundException if the callback does not exist.
  222. *
  223. * @param <S> The type of callback
  224. * @param callback Type of callback object
  225. * @param o instance of ICallbackInterface to add.
  226. * @throws CallbackNotFoundException If callback is not found.
  227. * @throws NullPointerException If 'o' is null
  228. */
  229. public <S extends CallbackInterface> void addCallback(
  230. final Class<S> callback, final S o) throws CallbackNotFoundException {
  231. if (o == null) {
  232. throw new NullPointerException("CallbackInterface is null");
  233. }
  234. final CallbackObject cb = getCallbackType(callback);
  235. if (cb != null) {
  236. cb.add(o);
  237. }
  238. }
  239. /**
  240. * Add a callback with a specific target.
  241. * This method will throw a CallbackNotFoundException if the callback does not exist.
  242. *
  243. * @param <S> The type of callback
  244. * @param callback Type of callback object.
  245. * @param o instance of ICallbackInterface to add.
  246. * @param target Parameter to specify that a callback should only fire for specific things
  247. * @throws CallbackNotFoundException If callback is not found.
  248. * @throws NullPointerException If 'o' is null
  249. */
  250. public <S extends CallbackInterface> void addCallback(
  251. final Class<S> callback,
  252. final S o, final String target) throws CallbackNotFoundException {
  253. if (o == null) {
  254. throw new NullPointerException("CallbackInterface is null");
  255. }
  256. ((CallbackObjectSpecific) getCallbackType(callback)).add(o, target);
  257. }
  258. /**
  259. * Add a callback without an exception.
  260. * This should be used if a callback is not essential for execution (ie the DebugOut callback)
  261. *
  262. * @param <S> The type of callback object
  263. * @param callback Type of callback object.
  264. * @param o instance of ICallbackInterface to add.
  265. * @return true/false if the callback was added or not.
  266. */
  267. public <S extends CallbackInterface> boolean addNonCriticalCallback(
  268. final Class<S> callback, final S o) {
  269. try {
  270. addCallback(callback, o);
  271. return true;
  272. } catch (CallbackNotFoundException e) {
  273. return false;
  274. }
  275. }
  276. /**
  277. * Add a callback with a specific target.
  278. * This should be used if a callback is not essential for execution
  279. *
  280. * @param <S> The type of callback
  281. * @param callback Type of callback object.
  282. * @param o instance of ICallbackInterface to add.
  283. * @param target Parameter to specify that a callback should only fire for specific things
  284. * @return true/false if the callback was added or not.
  285. */
  286. public <S extends CallbackInterface> boolean addNonCriticalCallback(
  287. final Class<S> callback, final S o, final String target) {
  288. try {
  289. addCallback(callback, o, target);
  290. return true;
  291. } catch (CallbackNotFoundException e) {
  292. return false;
  293. }
  294. }
  295. /**
  296. * Remove a callback.
  297. *
  298. * @param callback Type of callback object.
  299. * @param o instance of ICallbackInterface to remove.
  300. */
  301. public void delCallback(final Class<? extends CallbackInterface> callback,
  302. final CallbackInterface o) {
  303. getCallbackType(callback).del(o);
  304. }
  305. /**
  306. * Gets a proxy object which can be used to despatch callbacks of the
  307. * specified type. Callers may pass <code>null</code> for the first two
  308. * arguments of any callback, and these will automatically be replaced
  309. * by the relevant Parser instance and the current date.
  310. *
  311. * @param <T> The type of the callback to retrieve
  312. * @param callback The callback to retrieve
  313. * @return A proxy object which can be used to call the specified callback
  314. */
  315. @SuppressWarnings("unchecked")
  316. public <T extends CallbackInterface> T getCallback(final Class<T> callback) {
  317. return (T) Proxy.newProxyInstance(getClass().getClassLoader(),
  318. new Class[]{ callback }, new CallbackHandler(callback));
  319. }
  320. /**
  321. * A Proxy invocation handler for a specified parser callback.
  322. */
  323. private class CallbackHandler implements InvocationHandler {
  324. /** The callback that should be called. */
  325. private final Class<? extends CallbackInterface> callback;
  326. /**
  327. * Creates a new callback handler for the specified callback.
  328. *
  329. * @param callback The callback to handle
  330. */
  331. public CallbackHandler(final Class<? extends CallbackInterface> callback) {
  332. this.callback = callback;
  333. }
  334. /** {@inheritDoc} */
  335. @Override
  336. public Object invoke(final Object proxy, final Method method, final Object[] args) {
  337. final Object[] modifiedArgs = new Object[args.length - 2];
  338. System.arraycopy(args, 2, modifiedArgs, 0, args.length - 2);
  339. if (args[1] == null) {
  340. getCallbackType(callback).call(modifiedArgs);
  341. } else {
  342. getCallbackType(callback).call((Date) args[1], modifiedArgs);
  343. }
  344. return null;
  345. }
  346. }
  347. }