001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.web.jmx;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    
022    import javax.management.JMException;
023    import javax.management.MBeanServer;
024    import javax.management.ObjectName;
025    import javax.management.QueryExp;
026    import javax.servlet.ServletException;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import java.io.IOException;
031    import java.util.Set;
032    
033    /**
034     * Creates an XML response for one or more MBeans using an optional node in the
035     * JMX tree or query parameters.
036     * 
037     * @version $Revision: 356269 $
038     */
039    public class JMXServlet extends JMXServletSupport {
040    
041        private static final Log log = LogFactory.getLog(JMXServlet.class);
042        private static final long serialVersionUID = -5953322364144161756L;
043    
044        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
045            try {
046                MBeanServer beanServer = getMBeanServer();
047                ObjectName name = getObjectName(request);
048                QueryExp query = getQueryExp(request);
049    
050                JMXWriter writer = new JMXWriter(response.getWriter(), getManagementContext());
051    
052                String style = request.getParameter("style");
053                String view = request.getParameter("view");
054                if (view == null) {
055                    view = "";
056                }
057    
058                if (style != null && style.equals("html")) {
059                    Set names = beanServer.queryNames(name, query);
060    
061                    if (log.isDebugEnabled()) {
062                        log.debug("ObjectName: " + name);
063                        log.debug("Query: " + query);
064                        log.debug("Matches ObjectNames: " + names);
065                    }
066    
067                    if (view.equals("properties")) {
068                        writer.outputHtmlProperties(names);
069                    }
070                    else if (view.equals("attributes")) {
071                        writer.outputHtmlAttributes(names);
072                    }
073                    else if (view.equals("flat")) {
074                        writer.outputHtmlNames(names);
075                    }
076                    else {
077                        writer.outputHtmlNamesByDomain(names);
078                    }
079                }
080                else {
081                    writer.outputHeader();
082                    if (view.equals("bean")) {
083                        Set mbeans = beanServer.queryMBeans(name, query);
084                        writer.outputMBeans(mbeans);
085                    }
086                    else if (view.equals("detail")) {
087                        Set names = beanServer.queryNames(name, query);
088                        writer.outputDetail(names);
089                    }
090                    else {
091                        Set names = beanServer.queryNames(name, query);
092                        writer.outputNames(names);
093                    }
094                    writer.outputFooter();
095                }
096            }
097            catch (JMException e) {
098                throw new ServletException(e);
099            }
100        }
101    }