1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.ui.servlets; |
15 | |
|
16 | |
import java.io.IOException; |
17 | |
import java.util.ArrayList; |
18 | |
import java.util.Collections; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
import java.util.Set; |
23 | |
import java.util.StringTokenizer; |
24 | |
|
25 | |
import javax.servlet.ServletConfig; |
26 | |
import javax.servlet.ServletException; |
27 | |
import javax.servlet.http.HttpServletRequest; |
28 | |
import javax.servlet.http.HttpServletResponse; |
29 | |
|
30 | |
import net.admin4j.config.Admin4JConfiguration; |
31 | |
import net.admin4j.deps.commons.beanutils.BeanComparator; |
32 | |
import net.admin4j.deps.commons.lang3.StringUtils; |
33 | |
import net.admin4j.entity.ExceptionInfo; |
34 | |
import net.admin4j.exception.ExceptionTracker; |
35 | |
import net.admin4j.log.LogManager; |
36 | |
import net.admin4j.log.LogManagerRegistry; |
37 | |
import net.admin4j.util.Admin4jRuntimeException; |
38 | |
import net.admin4j.util.FreemarkerUtils; |
39 | |
import net.admin4j.util.GuiUtils; |
40 | |
import net.admin4j.util.HostUtils; |
41 | |
import net.admin4j.util.ServletUtils; |
42 | |
import net.admin4j.vo.ExceptionStatisticVO; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 6 | public class ExceptionDisplayServlet extends AdminDisplayServlet { |
62 | |
|
63 | |
private static final long serialVersionUID = -2262926722396897223L; |
64 | |
|
65 | |
public static final String PUBLIC_HANDLE="exception"; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
@Override |
71 | |
public void init(ServletConfig config) throws ServletException { |
72 | 6 | super.init(config); |
73 | 6 | for (LogManager logManager: LogManagerRegistry.getAvailableLogManagerSet()) { |
74 | |
try { |
75 | 18 | logManager.installExceptionTrackingLogAppender(); |
76 | 18 | logger.info("Exception tracking for {} installed.", logManager.getLoggerSoftwareProductName()); |
77 | |
} |
78 | 0 | catch (Throwable t) { |
79 | 0 | logger.error("Exception tracking for {} installed.", |
80 | |
new Admin4jRuntimeException(t).addContextValue("log manager", logManager.getLoggerSoftwareProductName())); |
81 | 36 | } |
82 | |
} |
83 | |
|
84 | 6 | String trackingTimeStr = ServletUtils.getConfigurationSetting( |
85 | |
new String[]{PUBLIC_HANDLE + ".tracking.time.in.days", |
86 | |
"tracking.time.in.days"}, config); |
87 | |
Integer trackingTimeInDays; |
88 | 0 | if ( !StringUtils.isEmpty(trackingTimeStr)) { |
89 | |
try { |
90 | 3 | trackingTimeInDays = new Integer(trackingTimeStr.trim()); |
91 | 3 | ExceptionTracker.setPurgeThresholdInDays(trackingTimeInDays); |
92 | |
} |
93 | 0 | catch (Throwable t) { |
94 | 0 | this.logger.error("Invalid tracking.time.in.millis parameter for ExceptionDisplayServlet. Default used. tracking.time.in.millis="+ trackingTimeStr, |
95 | |
t); |
96 | 3 | } |
97 | |
} |
98 | 3 | else if (Admin4JConfiguration.getExceptionTimeTrackingInDays() != null) { |
99 | 0 | ExceptionTracker.setPurgeThresholdInDays(Admin4JConfiguration.getExceptionTimeTrackingInDays()); |
100 | |
} |
101 | |
|
102 | 6 | String exemptedExceptionStr = ServletUtils.getConfigurationSetting( |
103 | |
new String[]{PUBLIC_HANDLE + ".exempted.exception.types", |
104 | |
"exempted.exception.types"}, config); |
105 | 0 | if ( !StringUtils.isEmpty(exemptedExceptionStr)) { |
106 | 0 | registerExceptedExceptionTypes(exemptedExceptionStr); |
107 | |
} |
108 | 0 | else if ( !StringUtils.isEmpty(Admin4JConfiguration.getExceptionExemptedExceptionTypes() )) { |
109 | 0 | registerExceptedExceptionTypes(Admin4JConfiguration.getExceptionExemptedExceptionTypes()); |
110 | |
} |
111 | 6 | } |
112 | |
|
113 | |
private void registerExceptedExceptionTypes(String exemptedExceptionStr) { |
114 | |
try { |
115 | 0 | StringTokenizer tok = new StringTokenizer(exemptedExceptionStr, ","); |
116 | |
String exemptedClassName; |
117 | 0 | while (tok.hasMoreTokens()) { |
118 | 0 | exemptedClassName = tok.nextToken(); |
119 | 0 | if (!StringUtils.isEmpty(exemptedClassName)) { |
120 | 0 | ExceptionTracker.addExemptedExceptionClassName(exemptedClassName); |
121 | 0 | this.logger.info("Exceptions of type {} will not be tracked.", exemptedClassName); |
122 | |
} |
123 | |
} |
124 | |
} |
125 | 0 | catch (Throwable t) { |
126 | 0 | this.logger.error("Invalid exempted.exception.types parameter for ExceptionDisplayServlet. parm=" + exemptedExceptionStr, |
127 | |
t); |
128 | 0 | } |
129 | 0 | } |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
@SuppressWarnings("unchecked") |
135 | |
@Override |
136 | |
protected void service(HttpServletRequest request, HttpServletResponse response) |
137 | |
throws ServletException, IOException { |
138 | |
|
139 | 3 | String actionStr = request.getParameter("action"); |
140 | 0 | if ( !StringUtils.isEmpty(actionStr)) { |
141 | |
|
142 | |
|
143 | |
|
144 | 0 | if ("delete".equalsIgnoreCase(actionStr)) { |
145 | 0 | ExceptionTracker.deleteException(request.getParameter("exceptionId")); |
146 | |
} |
147 | 0 | if ("text".equalsIgnoreCase(actionStr)) { |
148 | 0 | ExceptionInfo eInfo = ExceptionTracker.lookupException(request.getParameter("exceptionId")); |
149 | |
|
150 | 0 | response.setContentType("application/octet-stream"); |
151 | 0 | response.setHeader("Content-disposition", "attachment; filename=\"exception.txt\""); |
152 | |
|
153 | 0 | Map<String,Object> variableMap = new HashMap<String,Object>(); |
154 | 0 | variableMap.put("exception", eInfo); |
155 | 0 | variableMap.put("host", HostUtils.getHostName()); |
156 | 0 | variableMap.put("GuiUtils", new GuiUtils()); |
157 | 0 | FreemarkerUtils.addConfiguration(variableMap); |
158 | 0 | this.displayFreeMarkerResponse(request, response, "exceptionDownload.ftl", variableMap); |
159 | |
|
160 | 0 | return; |
161 | |
} |
162 | |
} |
163 | |
|
164 | 3 | Set<ExceptionStatisticVO> exceptionSet = ExceptionTracker.getExceptionStatisticSet(); |
165 | |
|
166 | 3 | List<ExceptionStatisticVO> exceptionList = new ArrayList<ExceptionStatisticVO>(exceptionSet); |
167 | |
Collections.sort(exceptionList, new BeanComparator("totalNbrExceptions")); |
168 | 3 | Collections.reverse(exceptionList); |
169 | |
|
170 | 3 | Map<String,Object> variableMap = new HashMap<String,Object>(); |
171 | 3 | variableMap.put("exceptionList", exceptionList); |
172 | 3 | variableMap.put("GuiUtils", new GuiUtils()); |
173 | 3 | FreemarkerUtils.addConfiguration(variableMap); |
174 | |
|
175 | 3 | displayFreeMarkerPage(request, response, "exceptionDisplayServletDisplay.ftl", variableMap); |
176 | 3 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
@Override |
182 | |
public String getServletLabel() { |
183 | 3 | return "Exception Summary"; |
184 | |
} |
185 | |
|
186 | |
|
187 | |
} |