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 javax.management.MBeanServer;
020    import javax.management.MalformedObjectNameException;
021    import javax.management.ObjectName;
022    import javax.management.QueryExp;
023    import javax.servlet.ServletException;
024    import javax.servlet.http.HttpServlet;
025    import javax.servlet.http.HttpServletRequest;
026    
027    /**
028     * A useful base class for any JMS related servlet; there are various ways to
029     * map JMS operations to web requests so we put most of the common behaviour in
030     * a reusable base class.
031     *
032     * @version $Revision: 356269 $
033     */
034    public abstract class JMXServletSupport extends HttpServlet {
035    
036        protected static final String MANAGEMENT_CONTEXT_PROPERTY = "org.activemq.jmx.ManagementContext";
037    
038        private ManagementContext managementContext;
039    
040        public void init() throws ServletException {
041            if (managementContext == null) {
042                managementContext = (ManagementContext) getServletContext().getAttribute(MANAGEMENT_CONTEXT_PROPERTY);
043                if (managementContext == null) {
044                    managementContext = new ManagementContext();
045                }
046            }
047        }
048    
049        public MBeanServer getMBeanServer() {
050            return managementContext.getMBeanServer();
051        }
052    
053        public ManagementContext getManagementContext() {
054            return managementContext;
055        }
056    
057        public void setManagementContext(ManagementContext managementContext) {
058            this.managementContext = managementContext;
059        }
060    
061        protected QueryExp getQueryExp(HttpServletRequest request) throws ServletException {
062            QueryExp answer = null;
063            String value = request.getParameter("query");
064            if (value != null) {
065                try {
066                    answer = new ObjectName(value);
067                }
068                catch (MalformedObjectNameException e) {
069                    throw new ServletException(e);
070                }
071            }
072            return answer;
073        }
074    
075        protected ObjectName getObjectName(HttpServletRequest request) throws ServletException {
076            String value = request.getParameter("name");
077            ObjectName answer = null;
078            if (value != null) {
079                try {
080                    answer = new ObjectName(value);
081                }
082                catch (MalformedObjectNameException e) {
083                    throw new ServletException("Failed to parse object name: " + value + ". Reason: " + e, e);
084                }
085            }
086            return answer;
087        }
088    
089        /**
090         * Converts the value of the named parameter into a boolean
091         */
092        protected boolean asBoolean(HttpServletRequest request, String name) {
093            String param = request.getParameter(name);
094            return param != null && param.equalsIgnoreCase("true");
095        }
096    }