You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ErrorManager.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.logger;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.util.Downloader;
  25. import com.dmdirc.util.ListenerList;
  26. import java.io.IOException;
  27. import java.io.Serializable;
  28. import java.net.MalformedURLException;
  29. import java.util.ArrayList;
  30. import java.util.Arrays;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. /**
  35. * Error manager.
  36. */
  37. public final class ErrorManager implements Serializable, Runnable {
  38. /**
  39. * A version number for this class. It should be changed whenever the class
  40. * structure is changed (or anything else that would prevent serialized
  41. * objects being unserialized with the new class).
  42. */
  43. private static final long serialVersionUID = 4;
  44. /** Time to wait between error submissions. */
  45. private static final int SLEEP_TIME = 5000;
  46. /** Previously instantiated instance of ErrorManager. */
  47. private static ErrorManager me = new ErrorManager();
  48. /** Queue of errors to be reported. */
  49. private final List<ProgramError> reportQueue = new ArrayList<ProgramError>();
  50. /** Thread used for sending errors. */
  51. private volatile Thread reportThread;
  52. /** Error list. */
  53. private final Map<Long, ProgramError> errors;
  54. /** Listener list. */
  55. private final ListenerList errorListeners = new ListenerList();
  56. /** Next error ID. */
  57. private long nextErrorID;
  58. /** Creates a new instance of ErrorListDialog. */
  59. private ErrorManager() {
  60. errors = new HashMap<Long, ProgramError>();
  61. nextErrorID = 0;
  62. }
  63. /**
  64. * Returns the instance of ErrorManager.
  65. *
  66. * @return Instance of ErrorManager
  67. */
  68. public static ErrorManager getErrorManager() {
  69. synchronized (me) {
  70. return me;
  71. }
  72. }
  73. /**
  74. * Called when an error occurs in the program.
  75. *
  76. * @param error ProgramError that occurred
  77. */
  78. public void addError(final ProgramError error) {
  79. errors.put(error.getID(), error);
  80. if (error.getLevel() == ErrorLevel.FATAL) {
  81. fireFatalError(error);
  82. } else {
  83. fireErrorAdded(error);
  84. }
  85. }
  86. /**
  87. * Called when an error needs to be deleted from the list.
  88. *
  89. * @param error ProgramError that changed
  90. */
  91. public void deleteError(final ProgramError error) {
  92. errors.remove(error.getID());
  93. fireErrorDeleted(error);
  94. }
  95. /**
  96. * Returns a list of errors.
  97. *
  98. * @return Error list
  99. */
  100. public Map<Long, ProgramError> getErrorList() {
  101. synchronized (errors) {
  102. return new HashMap<Long, ProgramError>(errors);
  103. }
  104. }
  105. /**
  106. * Returns the number of errors.
  107. *
  108. * @return Number of ProgramErrors
  109. */
  110. public int getErrorCount() {
  111. return errors.size();
  112. }
  113. /**
  114. * Returns the next error ID.
  115. *
  116. * @return Next error ID
  117. */
  118. public long getNextErrorID() {
  119. return nextErrorID++;
  120. }
  121. /**
  122. * Returns specified program error
  123. *
  124. * @param id ID of the error to fetch
  125. *
  126. * @return ProgramError with specified ID
  127. */
  128. public ProgramError getError(final long id) {
  129. return errors.get(id);
  130. }
  131. /**
  132. * Returns the list of program errors.
  133. *
  134. * @return Program error list
  135. */
  136. public List<ProgramError> getErrors() {
  137. synchronized (errors) {
  138. return new ArrayList<ProgramError>(errors.values());
  139. }
  140. }
  141. /**
  142. * Sends an error to the developers.
  143. *
  144. * @param error error to be sent
  145. */
  146. public void sendError(final ProgramError error) {
  147. for (String line : error.getTrace()) {
  148. if (line.startsWith("com.dmdirc.ui.swing.DMDircEventQueue")) {
  149. error.setReportStatus(ErrorReportStatus.NOT_APPLICABLE);
  150. error.setFixedStatus(ErrorFixedStatus.INVALID);
  151. return;
  152. } else if (line.startsWith("com.dmdirc")) {
  153. break;
  154. }
  155. }
  156. if (!errors.containsValue(error)) {
  157. reportQueue.add(error);
  158. if (reportThread == null || !reportThread.isAlive()) {
  159. reportThread = new Thread(this, "Error reporting thread");
  160. reportThread.start();
  161. }
  162. } else {
  163. error.setReportStatus(ErrorReportStatus.FINISHED);
  164. error.setFixedStatus(ErrorFixedStatus.UNREPORTED);
  165. }
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. public void run() {
  170. while (reportQueue.size() > 0) {
  171. sendErrorInternal(reportQueue.remove(0));
  172. try {
  173. Thread.sleep(SLEEP_TIME);
  174. } catch (InterruptedException ex) {
  175. // Do nothing
  176. }
  177. }
  178. }
  179. /**
  180. * Sends an error to the developers.
  181. *
  182. * @param error ProgramError to be sent
  183. */
  184. private void sendErrorInternal(final ProgramError error) {
  185. final Map<String, String> postData = new HashMap<String, String>();
  186. List<String> response = new ArrayList<String>();
  187. int tries = 0;
  188. postData.put("message", error.getMessage());
  189. postData.put("trace", Arrays.toString(error.getTrace()));
  190. postData.put("version", Main.VERSION + "(" + Main.SVN_REVISION + ")");
  191. error.setReportStatus(ErrorReportStatus.SENDING);
  192. do {
  193. if (tries != 0) {
  194. try {
  195. Thread.sleep(5000);
  196. } catch (InterruptedException ex) {
  197. //Ignore
  198. }
  199. }
  200. try {
  201. response = Downloader.getPage("http://www.dmdirc.com/error.php", postData);
  202. } catch (MalformedURLException ex) {
  203. //Ignore, wont happen
  204. } catch (IOException ex) {
  205. //Ignore being handled
  206. }
  207. tries++;
  208. } while((response.isEmpty() || !response.get(response.size() - 1).
  209. equalsIgnoreCase("Error report submitted. Thank you."))
  210. || tries >= 5);
  211. checkResponses(error, response);
  212. }
  213. /**
  214. * Checks the responses and sets status accordingly.
  215. *
  216. * @param error Error to check response
  217. * @param response Response to check
  218. */
  219. private static void checkResponses(final ProgramError error,
  220. final List<String> response) {
  221. if (!response.isEmpty() || response.get(response.size() - 1).
  222. equalsIgnoreCase("Error report submitted. Thank you.")) {
  223. error.setReportStatus(ErrorReportStatus.FINISHED);
  224. } else {
  225. error.setReportStatus(ErrorReportStatus.ERROR);
  226. }
  227. if (response.size() == 1) {
  228. error.setFixedStatus(ErrorFixedStatus.NEW);
  229. return;
  230. }
  231. final String responseToCheck = response.get(0);
  232. if (responseToCheck.matches(".*fixed.*")) {
  233. error.setFixedStatus(ErrorFixedStatus.FIXED);
  234. } else if (responseToCheck.matches(".*more recent version.*")) {
  235. error.setFixedStatus(ErrorFixedStatus.TOOOLD);
  236. } else if (responseToCheck.matches(".*invalid.*")) {
  237. error.setFixedStatus(ErrorFixedStatus.INVALID);
  238. } else if (responseToCheck.matches(".*previously.*")) {
  239. error.setFixedStatus(ErrorFixedStatus.KNOWN);
  240. } else {
  241. error.setFixedStatus(ErrorFixedStatus.NEW);
  242. }
  243. }
  244. /**
  245. * Adds an ErrorListener to the listener list.
  246. *
  247. * @param listener Listener to add
  248. */
  249. public void addErrorListener(final ErrorListener listener) {
  250. synchronized (errorListeners) {
  251. if (listener == null) {
  252. return;
  253. }
  254. errorListeners.add(ErrorListener.class, listener);
  255. }
  256. }
  257. /**
  258. * Removes an ErrorListener from the listener list.
  259. *
  260. * @param listener Listener to remove
  261. */
  262. public void removeErrorListener(final ErrorListener listener) {
  263. errorListeners.remove(ErrorListener.class, listener);
  264. }
  265. /**
  266. * Fired when the program encounters an error.
  267. *
  268. * @param error Error that occurred
  269. */
  270. protected void fireErrorAdded(final ProgramError error) {
  271. int firedListeners = 0;
  272. for (ErrorListener listener : errorListeners.get(ErrorListener.class)) {
  273. if (listener.isReady()) {
  274. listener.errorAdded(error);
  275. firedListeners++;
  276. }
  277. }
  278. if (firedListeners == 0) {
  279. System.err.println("An error has occurred: " + error.getLevel()
  280. + ": " + error.getMessage());
  281. for (String line : error.getTrace()) {
  282. System.err.println("\t" + line);
  283. }
  284. }
  285. }
  286. /**
  287. * Fired when the program encounters a fatal error.
  288. *
  289. * @param error Error that occurred
  290. */
  291. protected void fireFatalError(final ProgramError error) {
  292. int firedListeners = 0;
  293. for (ErrorListener listener : errorListeners.get(ErrorListener.class)) {
  294. if (listener.isReady()) {
  295. listener.fatalError(error);
  296. firedListeners++;
  297. }
  298. }
  299. if (firedListeners == 0) {
  300. System.err.println("A fatal error has occurred: " + error.getMessage());
  301. for (String line : error.getTrace()) {
  302. System.err.println("\t" + line);
  303. }
  304. System.exit(-1);
  305. }
  306. }
  307. /**
  308. * Fired when an error is deleted.
  309. *
  310. * @param error Error that has been deleted
  311. */
  312. protected void fireErrorDeleted(final ProgramError error) {
  313. for (ErrorListener listener : errorListeners.get(ErrorListener.class)) {
  314. listener.errorDeleted(error);
  315. }
  316. }
  317. /**
  318. * Fired when an error's status is changed.
  319. *
  320. * @param error Error that has been altered
  321. */
  322. protected void fireErrorStatusChanged(final ProgramError error) {
  323. for (ErrorListener listener : errorListeners.get(ErrorListener.class)) {
  324. listener.errorStatusChanged(error);
  325. }
  326. }
  327. }