1
2 package net.sf.voruta.stat;
3
4 import java.io.*;
5 import java.net.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8 import java.text.*;
9
10 import javax.servlet.*;
11 import javax.servlet.http.*;
12
13 import net.sf.voruta.*;
14 /***
15 * TODO: Collect activity statistics
16 * @author baliuka
17 * @version
18 */
19 public class ActivityServlet extends HttpServlet {
20
21 private static final String CLASS_ATTRIBUTE = "voruta.activity.class";
22 private static final String METHOD_ATTRIBUTE = "voruta.activity.method";
23
24
25 /*** Initializes the servlet.
26 */
27 public void init(ServletConfig config) throws ServletException {
28 super.init(config);
29
30 }
31
32 /*** Destroys the servlet.
33 */
34 public void destroy() {
35
36 }
37
38 /*** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
39 * @param request servlet request
40 * @param response servlet response
41 */
42 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
43 throws ServletException, IOException {
44 response.setContentType("text/html");
45 PrintWriter out = response.getWriter();
46
47
48 out.println("<html>");
49 out.println("<head>");
50 out.println("<title>Voruta Activity</title>");
51 out.println("</head>");
52 out.println("<body>");
53
54 try{
55 //ProcedureUtilsTest.main(null);
56 body(request,response);
57
58 }catch(Exception e){
59 out.println("<pre>");
60 e.printStackTrace(out);
61 out.println("</pre>");
62 }finally{
63 Db.close();
64 }
65
66
67 out.println("</body>");
68 out.println("</html>");
69
70 out.close();
71 }
72
73 Set keywords = new HashSet(
74
75 Arrays.asList(
76 new Object[] {
77
78 "select","update","delete","insert","from",
79 "where","group","having",
80 "values","set"
81
82 }
83 )
84
85 );
86
87 private void printKeywords(PrintWriter out, String str){
88
89
90 for( StringTokenizer parser = new StringTokenizer(str,"\n\r\t "); parser.hasMoreTokens(); ){
91 String token = parser.nextToken();
92 if(keywords.contains(token.toLowerCase())){
93
94 out.print("<br> ");
95 out.print("<b>");
96 out.print(token);
97 out.print("</b>");
98
99 }else{
100
101 out.print(" ");
102 out.print(token);
103 }
104
105 }
106
107
108 }
109
110 Set filter = new HashSet(
111
112 Arrays.asList(
113 new Object[] {
114 "query","update","useCache","flushCache","handler"
115 }
116 )
117
118 );
119
120
121 private void print(PrintWriter out,
122 ProcedureDescriptor pd,
123 Method method,
124 boolean explain)throws Exception{
125
126 Properties p = pd.getTags();
127
128
129 for( Iterator i = p.entrySet().iterator(); i.hasNext(); ){
130
131 Map.Entry e = (Map.Entry)i.next();
132 if(filter.contains(e.getKey())){
133 out.print("<b>");
134 out.print(e.getKey());
135 out.print("</b>");
136 out.print(" ");
137 out.print("<span style=color:navy>");
138 printKeywords(out,(String)e.getValue());
139 out.print("</span>");
140 out.print("<br>");
141
142 }
143
144 }
145
146 format(
147 out,
148 "<a href=./?method={0}#{0} >EXPLAIN</a>",
149 new Object[]{methodId(method)}
150 );
151
152 if(explain){
153 out.print("<pre>");
154 pd.getHelper().explain(null,out);
155 out.print("</pre>");
156 }
157
158
159 }
160
161 private String methodId(Method method){
162 return method.getName() + "$" + method.getParameterTypes().length;
163 }
164
165 private void format(PrintWriter out,String str, Object[] args){
166
167 out.print(
168 MessageFormat.format( str, args )
169 );
170
171 }
172
173 private void print(PrintWriter out, Method method){
174
175 format(
176 out,
177 "<a name={0}><b>{1}</b>" ,
178 new Object[]{methodId(method),method.getName()}
179
180 );
181
182 out.print("(");
183 Class types[] = method.getParameterTypes();
184 for( int i = 0; i < types.length; i++ ){
185 format(
186 out,
187 "{0} <span style=color:navy>${1}</span>",
188 new Object[]{
189 types[i].getName(),
190 new Integer(i + 1)
191 }
192 );
193
194 if( i != types.length - 1 ){
195 out.print(", ");
196 }
197 }
198 out.print(")");
199 }
200
201
202 protected void body(HttpServletRequest request, HttpServletResponse response)
203 throws Exception {
204
205 PrintWriter out = response.getWriter();
206 Map classes = Db.registredClasses();
207 String currentClass = (String)request.getSession().getAttribute(CLASS_ATTRIBUTE);
208 String currentMethod = (String)request.getSession().getAttribute(METHOD_ATTRIBUTE);
209
210 for(Iterator i = classes.entrySet().iterator(); i.hasNext();){
211
212 Map.Entry e = (Map.Entry)i.next();
213 Class cls = (Class)e.getKey();
214
215 format(
216 out,
217 "<a name={0}><a href=./?class={0}#{0} ><b>class {0}</b></a>'{",
218 new Object[]{ cls.getName() }
219 );
220
221 if( cls.getName().equals(currentClass) ){
222
223 out.print("<br>");
224 Map descriptors = (Map)e.getValue();
225 for(Iterator j = descriptors.entrySet().iterator(); j.hasNext();){
226 e = (Map.Entry)j.next();
227 Method method = (Method)e.getKey();
228 print( out, method );
229 out.print("{<br>");
230 print(
231 out,
232 (ProcedureDescriptor)e.getValue(),
233 method,
234 methodId(method).equals(currentMethod)
235 );
236 out.print("<br>}<br>");
237 out.print("<hr width=100%>");
238
239 }
240 } else {
241 out.print("...");
242 }
243
244 out.println("}");
245 }
246
247 }
248 /*** Handles the HTTP <code>GET</code> method.
249 * @param request servlet request
250 * @param response servlet response
251 */
252 protected void doGet(HttpServletRequest request, HttpServletResponse response)
253 throws ServletException, IOException {
254
255 String cls = request.getParameter("class");
256 if(cls != null){
257 request.getSession().setAttribute( CLASS_ATTRIBUTE , cls);
258 }
259
260 String m = request.getParameter("method");
261 if(m != null){
262 request.getSession().setAttribute( METHOD_ATTRIBUTE , m);
263 }
264 processRequest(request, response);
265 }
266
267 /*** Handles the HTTP <code>POST</code> method.
268 * @param request servlet request
269 * @param response servlet response
270 */
271 protected void doPost(HttpServletRequest request, HttpServletResponse response)
272 throws ServletException, IOException {
273 processRequest(request, response);
274 }
275
276 /*** Returns a short description of the servlet.
277 */
278 public String getServletInfo() {
279 return "Voruta activity servlet";
280 }
281
282 }
This page was automatically generated by Maven