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.Arrays; |
19 | |
import java.util.Collections; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
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.collections.comparators.ComparatorChain; |
33 | |
import net.admin4j.deps.commons.collections.comparators.ReverseComparator; |
34 | |
import net.admin4j.deps.commons.lang3.StringUtils; |
35 | |
import net.admin4j.entity.ExecutionPoint; |
36 | |
import net.admin4j.hotspot.ExecutionTracker; |
37 | |
import net.admin4j.hotspot.HotSpotUtils; |
38 | |
import net.admin4j.util.ServletUtils; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 6 | public class HotSpotDisplayServlet extends AdminDisplayServlet { |
50 | |
|
51 | |
private static final String DISPLAY_OPTION_TOP_50 = "Top 50"; |
52 | |
private static final String DISPLAY_OPTION_TOP_25 = "Top 25"; |
53 | |
private static final String DISPLAY_OPTION_TOP_10 = "Top 10"; |
54 | |
private static final String DISPLAY_OPTION_ALL = "All"; |
55 | |
private static final long serialVersionUID = 2757696314660986019L; |
56 | |
|
57 | 6 | public static final Integer DEFAULT_SLEEP_INTERGER_MILLIS=30000; |
58 | 6 | private Integer sleepIntervalInMillis = DEFAULT_SLEEP_INTERGER_MILLIS; |
59 | |
|
60 | |
public static final String PUBLIC_HANDLE="hotSpot"; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
@Override |
66 | |
public void init(ServletConfig config) throws ServletException { |
67 | 3 | super.init(config); |
68 | |
|
69 | |
|
70 | 3 | String sleepIntervalMillisStr = ServletUtils.getConfigurationSetting( |
71 | |
new String[]{PUBLIC_HANDLE + ".sleep.interval.millis", |
72 | |
"sleep.interval.millis"}, config); |
73 | |
|
74 | 0 | if ( !StringUtils.isEmpty(sleepIntervalMillisStr) ) { |
75 | |
try { |
76 | 0 | sleepIntervalInMillis = Integer.parseInt(sleepIntervalMillisStr); |
77 | 0 | } catch (NumberFormatException e) { |
78 | 0 | this.logger.error("Invalid Sleep Interval provided. Default used. value="+sleepIntervalMillisStr, e); |
79 | 0 | sleepIntervalInMillis = DEFAULT_SLEEP_INTERGER_MILLIS; |
80 | 0 | } |
81 | |
|
82 | |
} |
83 | 3 | else if ( Admin4JConfiguration.getHotSpotSleepIntervalMillis() != null ) { |
84 | 0 | sleepIntervalInMillis = Admin4JConfiguration.getHotSpotSleepIntervalMillis(); |
85 | |
} |
86 | |
|
87 | 3 | ExecutionTracker.startTracking(sleepIntervalInMillis); |
88 | 3 | } |
89 | |
|
90 | |
@SuppressWarnings("unchecked") |
91 | |
@Override |
92 | |
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
93 | |
|
94 | 0 | if ( !StringUtils.isEmpty(request.getParameter("Reset"))) { |
95 | 0 | ExecutionTracker.reset(); |
96 | |
} |
97 | |
|
98 | 0 | String selectedDisplayOption = DISPLAY_OPTION_ALL; |
99 | 0 | if ( !StringUtils.isEmpty(request.getParameter("displayOption"))) { |
100 | 0 | selectedDisplayOption = request.getParameter("displayOption"); |
101 | |
} |
102 | |
|
103 | 0 | Map<StackTraceElement, ExecutionPoint> fullExecutionMap = ExecutionTracker.getExecutionMap(); |
104 | 0 | Map<StackTraceElement, ExecutionPoint> executionMap = ExecutionTracker.getHotSpotMap(); |
105 | 0 | Map<StackTraceElement, ExecutionPoint> blockedExecutionMap = ExecutionTracker.getBlockedExecutionMap(); |
106 | |
|
107 | 0 | displayHotSpotPage(request, response, selectedDisplayOption, |
108 | |
fullExecutionMap, executionMap, blockedExecutionMap); |
109 | 0 | } |
110 | |
|
111 | |
protected void displayHotSpotPage(HttpServletRequest request, |
112 | |
HttpServletResponse response, String selectedDisplayOption, |
113 | |
Map<StackTraceElement, ExecutionPoint> fullExecutionMap, |
114 | |
Map<StackTraceElement, ExecutionPoint> executionMap, |
115 | |
Map<StackTraceElement, ExecutionPoint> blockedExecutionMap) |
116 | |
throws IOException { |
117 | 3 | ExecutionPoint displayedExecutionPoint = null; |
118 | 0 | if ( !StringUtils.isEmpty(request.getParameter("DisplayClass"))) { |
119 | 0 | StringTokenizer epStrTok = new StringTokenizer(request.getParameter("DisplayClass"), ":"); |
120 | 0 | String className = epStrTok.nextToken(); |
121 | 0 | String methodName = epStrTok.nextToken(); |
122 | 0 | String fileName = epStrTok.nextToken(); |
123 | |
|
124 | 0 | displayedExecutionPoint = fullExecutionMap.get(new StackTraceElement(className, methodName, fileName, 0)); |
125 | |
} |
126 | |
|
127 | 3 | List<String> displayOptionList = new ArrayList<String>(); |
128 | |
|
129 | 3 | displayOptionList.add(DISPLAY_OPTION_ALL); |
130 | 3 | displayOptionList.add(DISPLAY_OPTION_TOP_10); |
131 | 3 | displayOptionList.add(DISPLAY_OPTION_TOP_25); |
132 | 3 | displayOptionList.add(DISPLAY_OPTION_TOP_50); |
133 | |
|
134 | 3 | List<ExecutionPoint> pointList = new ArrayList<ExecutionPoint>(); |
135 | 3 | pointList.addAll(executionMap.values()); |
136 | |
ComparatorChain comparator = new ComparatorChain(); |
137 | |
comparator.addComparator(new ReverseComparator(new BeanComparator("nbrExecutions"))); |
138 | |
comparator.addComparator(new BeanComparator("stackTraceElement.className")); |
139 | |
comparator.addComparator(new BeanComparator("stackTraceElement.methodName")); |
140 | |
comparator.addComparator(new BeanComparator("stackTraceElement.lineNumber")); |
141 | 3 | Collections.sort(pointList, comparator); |
142 | |
|
143 | 3 | if ( !selectedDisplayOption.equals(DISPLAY_OPTION_ALL)) { |
144 | 3 | int top = 50; |
145 | 3 | if (selectedDisplayOption.equals(DISPLAY_OPTION_TOP_25)) { |
146 | 0 | top = 25; |
147 | |
} |
148 | 3 | if (selectedDisplayOption.equals(DISPLAY_OPTION_TOP_10)) { |
149 | 0 | top = 10; |
150 | |
} |
151 | |
|
152 | 3 | while (pointList.size() > top) { |
153 | 0 | pointList.remove(top); |
154 | |
} |
155 | |
} |
156 | |
|
157 | 3 | List<ExecutionPoint> blockedPointList = new ArrayList<ExecutionPoint>(); |
158 | 3 | blockedPointList.addAll(blockedExecutionMap.values()); |
159 | |
comparator = new ComparatorChain(); |
160 | |
comparator.addComparator(new ReverseComparator(new BeanComparator("nbrBlockedExecutions"))); |
161 | |
comparator.addComparator(new BeanComparator("stackTraceElement.className")); |
162 | |
comparator.addComparator(new BeanComparator("stackTraceElement.methodName")); |
163 | |
comparator.addComparator(new BeanComparator("stackTraceElement.lineNumber")); |
164 | 3 | Collections.sort(blockedPointList, comparator); |
165 | |
|
166 | 3 | Map<String,Object> variableMap = new HashMap<String,Object>(); |
167 | 3 | variableMap.put("displayOptionList", displayOptionList); |
168 | 3 | variableMap.put("executionPointList", pointList); |
169 | 3 | variableMap.put("blockedExecutionPointList", blockedPointList); |
170 | 3 | if (displayedExecutionPoint != null) { |
171 | 0 | variableMap.put("displayedExecutionPointList", Arrays.asList(displayedExecutionPoint)); |
172 | |
} |
173 | 3 | variableMap.put("selectedDisplayOption", selectedDisplayOption); |
174 | 3 | variableMap.put("hotSpotUtils", new HotSpotUtils(fullExecutionMap)); |
175 | |
|
176 | 3 | displayFreeMarkerPage(request, response, "hotSpotServletDisplay.ftl", variableMap); |
177 | 3 | } |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
@Override |
183 | |
public String getServletLabel() { |
184 | 3 | return "HotSpot Display"; |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
protected Integer getSleepIntervalInMillis() { |
192 | 0 | return sleepIntervalInMillis; |
193 | |
} |
194 | |
|
195 | |
} |