1 package org.andromda.core.common;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.PrintWriter;
6 import java.io.RandomAccessFile;
7 import java.text.SimpleDateFormat;
8 import java.util.Date;
9 import java.util.Random;
10 import org.apache.commons.lang.StringUtils;
11 import org.apache.commons.lang.exception.ExceptionUtils;
12 import org.apache.log4j.Logger;
13
14
15
16
17
18
19
20
21 public final class ExceptionRecorder
22 {
23 private static final Logger logger = Logger.getLogger(ExceptionRecorder.class);
24
25
26
27
28 static final String FILE_HEADER = "------- AndroMDA Exception Recording -------";
29
30
31
32
33 static final String RUN_SYSTEM = "Run System .....: ";
34
35
36
37
38 static final String RUN_JDK = "Run JDK ........: ";
39
40
41
42
43 static final String INFORMATION_UNAVAILABLE = " unavailable";
44
45
46
47
48 private static final String exceptionDirectoryName = ".";
49
50
51
52
53 private static File exceptionDirectory = null;
54 private static final SimpleDateFormat cvDateFormat = new SimpleDateFormat("yyMMddHHmmss");
55 private static final Random random = new Random();
56
57
58
59
60 private static final ExceptionRecorder instance = new ExceptionRecorder();
61
62
63
64
65 private ExceptionRecorder()
66 {
67
68 }
69
70
71
72
73
74
75 public static ExceptionRecorder instance()
76 {
77 return instance;
78 }
79
80
81
82
83
84
85
86
87
88
89 public String record(Throwable throwable)
90 {
91 return record("", throwable, "S");
92 }
93
94
95
96
97
98
99
100
101
102
103
104 public String record(
105 String errorMessage,
106 Throwable throwable)
107 {
108 return record(errorMessage, throwable, "S");
109 }
110
111
112
113
114 private static final String DEFAULT_PREFIX = "andromda";
115
116
117
118
119
120
121
122
123
124
125
126
127 public String record(
128 String message,
129 Throwable throwable,
130 String prefix)
131 {
132 String result = null;
133 if (StringUtils.isEmpty(prefix))
134 {
135 prefix = DEFAULT_PREFIX;
136 }
137 try
138 {
139 final BuildInformation buildInformation = BuildInformation.instance();
140 final String uniqueName = getUniqueName(prefix);
141 final File exceptionFile = new File(exceptionDirectory, uniqueName);
142 result = exceptionFile.getCanonicalPath();
143 final PrintWriter writer = new PrintWriter(new FileWriter(exceptionFile));
144 writer.println(FILE_HEADER);
145 writer.println("Version ........: " + buildInformation.getBuildVersion());
146 writer.println("Error ..........: " + message);
147 writer.println("Build ..........: " + buildInformation.getBuildDate());
148 writer.println("Build System ...: " + buildInformation.getBuildSystem());
149 writer.println("Build JDK ......: " + buildInformation.getBuildJdk());
150 writer.println("Build Builder ..: " + buildInformation.getBuildBuilder());
151
152
153 try
154 {
155 writer.println(RUN_SYSTEM + System.getProperty("os.name") + System.getProperty("os.version"));
156 writer.println(RUN_JDK + System.getProperty("java.vm.vendor") + System.getProperty("java.vm.version"));
157 }
158 catch (Exception ex)
159 {
160
161 writer.println(RUN_SYSTEM + INFORMATION_UNAVAILABLE);
162 writer.println(RUN_JDK + INFORMATION_UNAVAILABLE);
163 }
164 writer.println("Main Exception .: " + throwable.getMessage());
165 Throwable cause = ExceptionUtils.getRootCause(throwable);
166 if (cause == null)
167 {
168 cause = throwable;
169 }
170 writer.println("Root Exception .: " + cause);
171 cause.printStackTrace(writer);
172 writer.close();
173 AndroMDALogger.error("Exception recorded in --> '" + result + '\'');
174 }
175 catch (Throwable th)
176 {
177 final String errorMessage = "ExceptionRecorder.record error recording exception --> '" + throwable + '\'';
178 logger.error(errorMessage, th);
179 }
180 return result;
181 }
182
183
184
185
186 private static final String SUFFIX = ".exc";
187
188
189
190
191
192
193
194 protected synchronized String getUniqueName(String prefix)
195 {
196 String uniqueName = prefix + cvDateFormat.format(new Date()) + SUFFIX;
197 int suffix = 0;
198 File exceptionFile = new File(exceptionDirectory, uniqueName);
199 while (exceptionFile.exists())
200 {
201 uniqueName = prefix + cvDateFormat.format(new Date()) + '_' + suffix++ + SUFFIX;
202 exceptionFile = new File(exceptionDirectory, uniqueName);
203
204
205
206
207 try
208 {
209 Thread.sleep(Math.abs(random.nextInt() % 100));
210 }
211 catch (InterruptedException e1)
212 {
213
214 }
215 }
216
217
218
219
220 try
221 {
222 RandomAccessFile file;
223 file = new RandomAccessFile(exceptionFile, "rw");
224 file.writeChar('t');
225 file.close();
226 }
227 catch (Exception ex)
228 {
229
230 }
231 return uniqueName;
232 }
233
234 static
235 {
236
237 try
238 {
239 exceptionDirectory = new File(exceptionDirectoryName);
240 if (!exceptionDirectory.exists())
241 {
242 exceptionDirectory.mkdir();
243 }
244 }
245 catch (Throwable th)
246 {
247
248 }
249 finally
250 {
251 if (exceptionDirectory == null)
252 {
253 exceptionDirectory = new File(".");
254 }
255 }
256 }
257
258
259
260
261
262
263 public File getExceptionDirectory()
264 {
265 return exceptionDirectory;
266 }
267 }