1 package com.explosion.expf;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Font;
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.PrintWriter;
27 import java.util.Date;
28
29 import javax.swing.JOptionPane;
30
31 import org.apache.log4j.LogManager;
32 import org.apache.log4j.Logger;
33
34 import com.explosion.expf.preferences.SystemPreferences;
35 import com.explosion.utilities.GeneralConstants;
36 import com.explosion.utilities.GeneralUtils;
37 import com.explosion.utilities.dialog.GenericExceptionDisplayDialog;
38 import com.explosion.utilities.exception.ExceptionHandler;
39
40 /***
41 * @author Stephen Cowx
42 * Date created:@02-Feb-2003
43 */
44 public class ExpExceptionHandler implements ExceptionHandler
45 {
46
47 private static Logger log = LogManager
48 .getLogger(ExpExceptionHandler.class);
49 /***
50 * Report an exception
51 */
52 public void handleException(Throwable e, String message)
53 {
54 if (message == null)
55 message = e.getMessage();
56 else
57 message += "\n\n" + e.getMessage();
58
59 GenericExceptionDisplayDialog dialog = new GenericExceptionDisplayDialog(Application.getApplicationFrame(), true, e, message, ExpConstants.DEFAULT_EXCEPTION_ICON, (Font) SystemPreferences.getPreference(ExpConstants.WINDOW_FONT).getValue());
60 GeneralUtils.centreWindowInParent(dialog, Application.getApplicationFrame());
61 log.error("Error (see error file for details) : "+ e.getMessage(),e);
62 writeErrorFile(e);
63 }
64
65
66 /***
67 * This method writes an error file.
68 */
69 private void writeErrorFile(Throwable e)
70 {
71 try
72 {
73 boolean writeErrorFile = ((Boolean) SystemPreferences.getPreference(ExpConstants.LOGERRORS).getValue()).booleanValue();
74 if (writeErrorFile)
75 {
76 File errorFile = (File) SystemPreferences.getPreference(ExpConstants.ERROR_FILE).getValue();
77 if (errorFile == null || errorFile.getParentFile() == null || !errorFile.getParentFile().exists() || errorFile.isDirectory())
78 {
79 JOptionPane.showMessageDialog(Application.getApplicationFrame(), "Unable to write stack dump to file " + errorFile.getAbsolutePath() + " as it is not a valid location. Writing to default location " + (File) SystemPreferences.getPreference(ExpConstants.ERROR_FILE).getDefaultValue() + " instead. Please check your application properties to ensure you have specified a valid location.", "Error !", JOptionPane.ERROR_MESSAGE);
80 errorFile = (File) SystemPreferences.getPreference(ExpConstants.ERROR_FILE).getDefaultValue();
81 }
82
83 PrintWriter errorWriter = new PrintWriter(new FileOutputStream(errorFile, true));
84 errorWriter.write(GeneralConstants.LS + GeneralConstants.LS);
85 errorWriter.write(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " + GeneralConstants.LS);
86 errorWriter.write("Error on " + new Date() + ". Stack dump follows " + GeneralConstants.LS);
87 e.printStackTrace(errorWriter);
88 errorWriter.close();
89 }
90 }
91 catch (Exception ex)
92 {
93 e.printStackTrace();
94 ex.printStackTrace();
95 JOptionPane.showMessageDialog(Application.getApplicationFrame(), "Unable to write stack dump of error " + e.getMessage() + " to file error because of another error " + ex.getMessage(), "Error !", JOptionPane.ERROR_MESSAGE);
96 }
97 }
98
99 }