KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jmxdebug > web > velocity > DebugServlet


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.jmxdebug.web.velocity;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URLDecoder JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.geronimo.jmxdebug.web.beanlib.GBeanInfoHelper;
28 import org.apache.geronimo.jmxdebug.web.beanlib.KernelHelper;
29 import org.apache.velocity.VelocityContext;
30
31 /**
32  * Simple servlet for looking at mbeans
33  *
34  * @version $Rev: 106697 $ $Date: 2004-11-27 00:58:00 -0800 (Sat, 27 Nov 2004) $
35  */

36 public class DebugServlet extends BasicVelocityActionServlet {
37     public static String JavaDoc OBJECT_NAME_FILTER_KEY = "ObjectNameFilter";
38
39     protected String JavaDoc getActionVerb() {
40         return "action";
41     }
42
43     /**
44      * The only real action - just puts the mbean server helper in the
45      * context, and if there was a mbean specified for details, shoves
46      * a MBeanINfoHelper in the context
47      */

48     public void defaultAction(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
49             throws ServletException JavaDoc, IOException JavaDoc {
50
51         String JavaDoc beanName = req.getParameter("MBeanName");
52         String JavaDoc filterKey = req.getParameter(OBJECT_NAME_FILTER_KEY);
53
54         if (filterKey == null || "".equals(filterKey)) {
55             filterKey = "*:*";
56         }
57
58         VelocityContext vc = new VelocityContext();
59
60         KernelHelper kernelHelper = new KernelHelper();
61         vc.put("mbctx", kernelHelper);
62         vc.put("encoder", new KickSunInHead());
63         vc.put(OBJECT_NAME_FILTER_KEY, filterKey);
64
65         if (beanName == null) {
66             vc.put("template", "nobean.vm");
67         } else {
68             try {
69                 vc.put("beanInfo", new GBeanInfoHelper(kernelHelper, beanName));
70             } catch (Exception JavaDoc e) {
71                 e.printStackTrace();
72             }
73             vc.put("template", "mbeaninfo.vm");
74         }
75
76         renderTemplate(req, res, vc, "index.vm");
77     }
78
79     public void unknownAction(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
80             throws ServletException JavaDoc, IOException JavaDoc {
81         defaultAction(req, res);
82     }
83
84
85     /**
86      * Why oh why couldn't this be one class...
87      */

88     public class KickSunInHead {
89         public String JavaDoc decode(String JavaDoc string) {
90             return decode(string, "UTF-8");
91         }
92
93         public String JavaDoc decode(String JavaDoc string, String JavaDoc encoding) {
94             if (string != null) {
95                 try {
96                     return URLDecoder.decode(string, encoding);
97                 } catch (Exception JavaDoc e) {
98                     e.printStackTrace();
99                 }
100             }
101             return null;
102         }
103
104         public String JavaDoc encode(String JavaDoc string) {
105             return encode(string, "UTF-8");
106         }
107
108         public String JavaDoc encode(String JavaDoc string, String JavaDoc encoding) {
109             if (string != null) {
110                 try {
111                     return URLEncoder.encode(string, encoding);
112                 } catch (Exception JavaDoc e) {
113                     e.printStackTrace();
114                 }
115             }
116
117             return null;
118         }
119
120     }
121 }
122
Popular Tags