PageRenderTime 50ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/azureus-4.7.0.2/org/gudy/azureus2/pluginsimpl/local/download/DownloadEventNotifierImpl.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 338 lines | 273 code | 39 blank | 26 comment | 22 complexity | e82728dc2e05d601d613cee0f024bf49 MD5 | raw file
  1. /*
  2. * Created on 12 Feb 2007
  3. * Created by Allan Crooks
  4. * Copyright (C) 2007 Aelitis, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * AELITIS, SAS au capital de 46,603.30 euros
  19. * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
  20. */
  21. package org.gudy.azureus2.pluginsimpl.local.download;
  22. import java.util.*;
  23. import org.gudy.azureus2.core3.util.*;
  24. import org.gudy.azureus2.plugins.download.Download;
  25. import org.gudy.azureus2.plugins.download.DownloadActivationEvent;
  26. import org.gudy.azureus2.plugins.download.DownloadActivationListener;
  27. import org.gudy.azureus2.plugins.download.DownloadAnnounceResult;
  28. import org.gudy.azureus2.plugins.download.DownloadAttributeListener;
  29. import org.gudy.azureus2.plugins.download.DownloadCompletionListener;
  30. import org.gudy.azureus2.plugins.download.DownloadEventNotifier;
  31. import org.gudy.azureus2.plugins.download.DownloadListener;
  32. import org.gudy.azureus2.plugins.download.DownloadManager;
  33. import org.gudy.azureus2.plugins.download.DownloadManagerListener;
  34. import org.gudy.azureus2.plugins.download.DownloadPeerListener;
  35. import org.gudy.azureus2.plugins.download.DownloadPropertyEvent;
  36. import org.gudy.azureus2.plugins.download.DownloadPropertyListener;
  37. import org.gudy.azureus2.plugins.download.DownloadRemovalVetoException;
  38. import org.gudy.azureus2.plugins.download.DownloadScrapeResult;
  39. import org.gudy.azureus2.plugins.download.DownloadTrackerListener;
  40. import org.gudy.azureus2.plugins.download.DownloadWillBeRemovedListener;
  41. import org.gudy.azureus2.plugins.torrent.TorrentAttribute;
  42. import org.gudy.azureus2.plugins.peers.PeerManager;
  43. /**
  44. * This is an implementation of DownloadEventNotifier to be simplify life for
  45. * plugins if they want to register event listeners across all downloads managed
  46. * by Azureus.
  47. */
  48. public class DownloadEventNotifierImpl implements DownloadEventNotifier {
  49. private DownloadActivationNotifier download_activation_notifier;
  50. private DownloadNotifier download_notifier;
  51. private DownloadPeerNotifier download_peer_notifier;
  52. private DownloadPropertyNotifier download_property_notifier;
  53. private DownloadTrackerNotifier download_tracker_notifier;
  54. private DownloadTrackerNotifier download_tracker_notifier_instant;
  55. private DownloadWillBeRemovedNotifier download_will_be_removed_notifier;
  56. private DownloadCompletionNotifier download_completion_notifier;
  57. private DownloadManager dm;
  58. private HashMap read_attribute_listeners;
  59. private HashMap write_attribute_listeners;
  60. public DownloadEventNotifierImpl(DownloadManager dm) {
  61. this.dm = dm;
  62. this.download_activation_notifier = new DownloadActivationNotifier();
  63. this.download_notifier = new DownloadNotifier();
  64. this.download_peer_notifier = new DownloadPeerNotifier();
  65. this.download_property_notifier = new DownloadPropertyNotifier();
  66. this.download_tracker_notifier = new DownloadTrackerNotifier(false);
  67. this.download_tracker_notifier_instant = new DownloadTrackerNotifier(true);
  68. this.download_will_be_removed_notifier = new DownloadWillBeRemovedNotifier();
  69. this.download_completion_notifier = new DownloadCompletionNotifier();
  70. this.read_attribute_listeners = new HashMap();
  71. this.write_attribute_listeners = new HashMap();
  72. }
  73. public void addActivationListener(DownloadActivationListener l) {
  74. this.download_activation_notifier.addListener(l);
  75. }
  76. public void addCompletionListener(DownloadCompletionListener l) {
  77. this.download_completion_notifier.addListener(l);
  78. }
  79. public void addDownloadWillBeRemovedListener(DownloadWillBeRemovedListener l) {
  80. this.download_will_be_removed_notifier.addListener(l);
  81. }
  82. public void addListener(DownloadListener l) {
  83. this.download_notifier.addListener(l);
  84. }
  85. public void addPeerListener(DownloadPeerListener l) {
  86. this.download_peer_notifier.addListener(l);
  87. }
  88. public void addPropertyListener(DownloadPropertyListener l) {
  89. this.download_property_notifier.addListener(l);
  90. }
  91. public void addTrackerListener(DownloadTrackerListener l) {
  92. this.download_tracker_notifier.addListener(l);
  93. }
  94. public void addTrackerListener(DownloadTrackerListener l, boolean immediateTrigger) {
  95. (immediateTrigger ? this.download_tracker_notifier_instant : this.download_tracker_notifier).addListener(l);
  96. }
  97. public void removeActivationListener(DownloadActivationListener l) {
  98. this.download_activation_notifier.removeListener(l);
  99. }
  100. public void removeCompletionListener(DownloadCompletionListener l) {
  101. this.download_completion_notifier.removeListener(l);
  102. }
  103. public void removeDownloadWillBeRemovedListener(DownloadWillBeRemovedListener l) {
  104. this.download_will_be_removed_notifier.removeListener(l);
  105. }
  106. public void removeListener(DownloadListener l) {
  107. this.download_notifier.removeListener(l);
  108. }
  109. public void removePeerListener(DownloadPeerListener l) {
  110. this.download_peer_notifier.removeListener(l);
  111. }
  112. public void removePropertyListener(DownloadPropertyListener l) {
  113. this.download_property_notifier.removeListener(l);
  114. }
  115. public void removeTrackerListener(DownloadTrackerListener l) {
  116. // We don't know which notifier we added it to, so remove it from both.
  117. this.download_tracker_notifier.removeListener(l);
  118. this.download_tracker_notifier_instant.removeListener(l);
  119. }
  120. public void addAttributeListener(DownloadAttributeListener listener, TorrentAttribute ta, int event_type) {
  121. Map attr_map = getAttributeListenerMap(event_type);
  122. DownloadAttributeNotifier l = (DownloadAttributeNotifier)attr_map.get(ta);
  123. if (l == null) {
  124. l = new DownloadAttributeNotifier(ta, event_type);
  125. attr_map.put(ta, l);
  126. }
  127. l.addListener(listener);
  128. }
  129. public void removeAttributeListener(DownloadAttributeListener listener, TorrentAttribute ta, int event_type) {
  130. Map attr_map = getAttributeListenerMap(event_type);
  131. DownloadAttributeNotifier l = (DownloadAttributeNotifier)attr_map.get(ta);
  132. if (l == null) {return;}
  133. l.removeListener(listener);
  134. }
  135. private abstract class BaseDownloadListener implements DownloadManagerListener {
  136. protected ArrayList listeners = new ArrayList();
  137. private AEMonitor this_mon;
  138. private BaseDownloadListener() {
  139. this_mon = new AEMonitor(getClass().getName());
  140. }
  141. void addListener(Object o) {
  142. boolean register_with_downloads = false;
  143. try {
  144. this_mon.enter();
  145. register_with_downloads = listeners.isEmpty();
  146. ArrayList new_listeners = new ArrayList(listeners);
  147. new_listeners.add(o);
  148. this.listeners = new_listeners;
  149. }
  150. finally {
  151. this_mon.exit();
  152. }
  153. if (register_with_downloads) {
  154. dm.addListener(this, true);
  155. }
  156. }
  157. void removeListener(Object o) {
  158. boolean unregister_from_downloads = false;
  159. try {
  160. this_mon.enter();
  161. ArrayList new_listeners = new ArrayList(listeners);
  162. new_listeners.remove(o);
  163. this.listeners = new_listeners;
  164. unregister_from_downloads = this.listeners.isEmpty();
  165. }
  166. finally {
  167. this_mon.exit();
  168. }
  169. if (unregister_from_downloads) {
  170. dm.removeListener(this, true);
  171. }
  172. }
  173. }
  174. public class DownloadActivationNotifier extends BaseDownloadListener implements DownloadActivationListener {
  175. public void downloadAdded(Download download) {download.addActivationListener(this);}
  176. public void downloadRemoved(Download download) {download.removeActivationListener(this);}
  177. public boolean activationRequested(DownloadActivationEvent event) {
  178. Iterator itr = this.listeners.iterator();
  179. while (itr.hasNext()) {
  180. try {if (((DownloadActivationListener)itr.next()).activationRequested(event)) {return true;}}
  181. catch (Throwable t) {Debug.printStackTrace(t);}
  182. }
  183. return false;
  184. }
  185. }
  186. public class DownloadCompletionNotifier extends BaseDownloadListener implements DownloadCompletionListener {
  187. public void downloadAdded(Download download) {download.addCompletionListener(this);}
  188. public void downloadRemoved(Download download) {download.removeCompletionListener(this);}
  189. public void onCompletion(Download download) {
  190. Iterator itr = this.listeners.iterator();
  191. while (itr.hasNext()) {
  192. try {((DownloadCompletionListener)itr.next()).onCompletion(download);}
  193. catch (Throwable t) {Debug.printStackTrace(t);}
  194. }
  195. }
  196. }
  197. public class DownloadNotifier extends BaseDownloadListener implements DownloadListener {
  198. public void downloadAdded(Download download) {download.addListener(this);}
  199. public void downloadRemoved(Download download) {download.removeListener(this);}
  200. public void stateChanged(Download download, int old_state, int new_state) {
  201. Iterator itr = this.listeners.iterator();
  202. while (itr.hasNext()) {
  203. try {((DownloadListener)itr.next()).stateChanged(download, old_state, new_state);}
  204. catch (Throwable t) {Debug.printStackTrace(t);}
  205. }
  206. }
  207. public void positionChanged(Download download, int old_position, int new_position) {
  208. Iterator itr = this.listeners.iterator();
  209. while (itr.hasNext()) {
  210. try {((DownloadListener)itr.next()).positionChanged(download, old_position, new_position);}
  211. catch (Throwable t) {Debug.printStackTrace(t);}
  212. }
  213. }
  214. }
  215. public class DownloadPeerNotifier extends BaseDownloadListener implements DownloadPeerListener {
  216. public void downloadAdded(Download download) {download.addPeerListener(this);}
  217. public void downloadRemoved(Download download) {download.removePeerListener(this);}
  218. public void peerManagerAdded(Download download, PeerManager peer_manager) {
  219. Iterator itr = this.listeners.iterator();
  220. while (itr.hasNext()) {
  221. try {((DownloadPeerListener)itr.next()).peerManagerAdded(download, peer_manager);}
  222. catch (Throwable t) {Debug.printStackTrace(t);}
  223. }
  224. }
  225. public void peerManagerRemoved(Download download, PeerManager peer_manager) {
  226. Iterator itr = this.listeners.iterator();
  227. while (itr.hasNext()) {
  228. try {((DownloadPeerListener)itr.next()).peerManagerRemoved(download, peer_manager);}
  229. catch (Throwable t) {Debug.printStackTrace(t);}
  230. }
  231. }
  232. }
  233. public class DownloadPropertyNotifier extends BaseDownloadListener implements DownloadPropertyListener {
  234. public void downloadAdded(Download download) {download.addPropertyListener(this);}
  235. public void downloadRemoved(Download download) {download.removePropertyListener(this);}
  236. public void propertyChanged(Download download, DownloadPropertyEvent event) {
  237. Iterator itr = this.listeners.iterator();
  238. while (itr.hasNext()) {
  239. try {((DownloadPropertyListener)itr.next()).propertyChanged(download, event);}
  240. catch (Throwable t) {Debug.printStackTrace(t);}
  241. }
  242. }
  243. }
  244. public class DownloadWillBeRemovedNotifier extends BaseDownloadListener implements DownloadWillBeRemovedListener {
  245. public void downloadAdded(Download download) {download.addDownloadWillBeRemovedListener(this);}
  246. public void downloadRemoved(Download download) {download.removeDownloadWillBeRemovedListener(this);}
  247. public void downloadWillBeRemoved(Download download) throws DownloadRemovalVetoException {
  248. Iterator itr = this.listeners.iterator();
  249. while (itr.hasNext()) {
  250. try {((DownloadWillBeRemovedListener)itr.next()).downloadWillBeRemoved(download);}
  251. catch (DownloadRemovalVetoException e) {throw e;}
  252. catch (Throwable t) {Debug.printStackTrace(t);}
  253. }
  254. }
  255. }
  256. public class DownloadAttributeNotifier extends BaseDownloadListener implements DownloadAttributeListener {
  257. private TorrentAttribute ta;
  258. private int event_type;
  259. public DownloadAttributeNotifier(TorrentAttribute ta, int event_type) {
  260. this.ta = ta; this.event_type = event_type;
  261. }
  262. public void downloadAdded(Download d) {d.addAttributeListener(this, ta, event_type);}
  263. public void downloadRemoved(Download d) {d.removeAttributeListener(this, ta, event_type);}
  264. public void attributeEventOccurred(Download d, TorrentAttribute ta, int event_type) {
  265. Iterator itr = this.listeners.iterator();
  266. while (itr.hasNext()) {
  267. try {((DownloadAttributeListener)itr.next()).attributeEventOccurred(d, ta, event_type);}
  268. catch (Throwable t) {Debug.printStackTrace(t);}
  269. }
  270. }
  271. }
  272. public class DownloadTrackerNotifier extends BaseDownloadListener implements DownloadTrackerListener {
  273. private boolean instant_notify;
  274. public DownloadTrackerNotifier(boolean instant_notify) {this.instant_notify = instant_notify;}
  275. public void downloadAdded(Download download) {download.addTrackerListener(this, this.instant_notify);}
  276. public void downloadRemoved(Download download) {download.removeTrackerListener(this);}
  277. public void scrapeResult(DownloadScrapeResult result) {
  278. Iterator itr = this.listeners.iterator();
  279. while (itr.hasNext()) {
  280. try {((DownloadTrackerListener)itr.next()).scrapeResult(result);}
  281. catch (Throwable t) {Debug.printStackTrace(t);}
  282. }
  283. }
  284. public void announceResult(DownloadAnnounceResult result) {
  285. Iterator itr = this.listeners.iterator();
  286. while (itr.hasNext()) {
  287. try {((DownloadTrackerListener)itr.next()).announceResult(result);}
  288. catch (Throwable t) {Debug.printStackTrace(t);}
  289. }
  290. }
  291. }
  292. private Map getAttributeListenerMap(int event_type) {
  293. if (event_type == DownloadAttributeListener.WRITTEN)
  294. return this.write_attribute_listeners;
  295. else if (event_type == DownloadAttributeListener.WILL_BE_READ)
  296. return this.read_attribute_listeners;
  297. throw new IllegalArgumentException("invalid event type " + event_type);
  298. }
  299. }