KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > web > jmx > JMXServlet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.servicemix.web.jmx;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import javax.management.JMException JavaDoc;
23 import javax.management.MBeanServer JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25 import javax.management.QueryExp JavaDoc;
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import java.io.IOException JavaDoc;
31 import java.util.Set JavaDoc;
32
33 /**
34  * Creates an XML response for one or more MBeans using an optional node in the
35  * JMX tree or query parameters.
36  *
37  * @version $Revision: 356269 $
38  */

39 public class JMXServlet extends JMXServletSupport {
40
41     private static final Log log = LogFactory.getLog(JMXServlet.class);
42     private static final long serialVersionUID = -5953322364144161756L;
43
44     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
45         try {
46             MBeanServer JavaDoc beanServer = getMBeanServer();
47             ObjectName JavaDoc name = getObjectName(request);
48             QueryExp JavaDoc query = getQueryExp(request);
49
50             JMXWriter writer = new JMXWriter(response.getWriter(), getManagementContext());
51
52             String JavaDoc style = request.getParameter("style");
53             String JavaDoc view = request.getParameter("view");
54             if (view == null) {
55                 view = "";
56             }
57
58             if (style != null && style.equals("html")) {
59                 Set JavaDoc names = beanServer.queryNames(name, query);
60
61                 if (log.isDebugEnabled()) {
62                     log.debug("ObjectName: " + name);
63                     log.debug("Query: " + query);
64                     log.debug("Matches ObjectNames: " + names);
65                 }
66
67                 if (view.equals("properties")) {
68                     writer.outputHtmlProperties(names);
69                 }
70                 else if (view.equals("attributes")) {
71                     writer.outputHtmlAttributes(names);
72                 }
73                 else if (view.equals("flat")) {
74                     writer.outputHtmlNames(names);
75                 }
76                 else {
77                     writer.outputHtmlNamesByDomain(names);
78                 }
79             }
80             else {
81                 writer.outputHeader();
82                 if (view.equals("bean")) {
83                     Set JavaDoc mbeans = beanServer.queryMBeans(name, query);
84                     writer.outputMBeans(mbeans);
85                 }
86                 else if (view.equals("detail")) {
87                     Set JavaDoc names = beanServer.queryNames(name, query);
88                     writer.outputDetail(names);
89                 }
90                 else {
91                     Set JavaDoc names = beanServer.queryNames(name, query);
92                     writer.outputNames(names);
93                 }
94                 writer.outputFooter();
95             }
96         }
97         catch (JMException JavaDoc e) {
98             throw new ServletException JavaDoc(e);
99         }
100     }
101 }
102
Popular Tags