KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.MBeanServer JavaDoc;
20 import javax.management.MalformedObjectNameException JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22 import javax.management.QueryExp JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServlet JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26
27 /**
28  * A useful base class for any JMS related servlet; there are various ways to
29  * map JMS operations to web requests so we put most of the common behaviour in
30  * a reusable base class.
31  *
32  * @version $Revision: 356269 $
33  */

34 public abstract class JMXServletSupport extends HttpServlet JavaDoc {
35
36     protected static final String JavaDoc MANAGEMENT_CONTEXT_PROPERTY = "org.activemq.jmx.ManagementContext";
37
38     private ManagementContext managementContext;
39
40     public void init() throws ServletException JavaDoc {
41         if (managementContext == null) {
42             managementContext = (ManagementContext) getServletContext().getAttribute(MANAGEMENT_CONTEXT_PROPERTY);
43             if (managementContext == null) {
44                 managementContext = new ManagementContext();
45             }
46         }
47     }
48
49     public MBeanServer JavaDoc getMBeanServer() {
50         return managementContext.getMBeanServer();
51     }
52
53     public ManagementContext getManagementContext() {
54         return managementContext;
55     }
56
57     public void setManagementContext(ManagementContext managementContext) {
58         this.managementContext = managementContext;
59     }
60
61     protected QueryExp JavaDoc getQueryExp(HttpServletRequest JavaDoc request) throws ServletException JavaDoc {
62         QueryExp JavaDoc answer = null;
63         String JavaDoc value = request.getParameter("query");
64         if (value != null) {
65             try {
66                 answer = new ObjectName JavaDoc(value);
67             }
68             catch (MalformedObjectNameException JavaDoc e) {
69                 throw new ServletException JavaDoc(e);
70             }
71         }
72         return answer;
73     }
74
75     protected ObjectName JavaDoc getObjectName(HttpServletRequest JavaDoc request) throws ServletException JavaDoc {
76         String JavaDoc value = request.getParameter("name");
77         ObjectName JavaDoc answer = null;
78         if (value != null) {
79             try {
80                 answer = new ObjectName JavaDoc(value);
81             }
82             catch (MalformedObjectNameException JavaDoc e) {
83                 throw new ServletException JavaDoc("Failed to parse object name: " + value + ". Reason: " + e, e);
84             }
85         }
86         return answer;
87     }
88
89     /**
90      * Converts the value of the named parameter into a boolean
91      */

92     protected boolean asBoolean(HttpServletRequest JavaDoc request, String JavaDoc name) {
93         String JavaDoc param = request.getParameter(name);
94         return param != null && param.equalsIgnoreCase("true");
95     }
96 }
97
Popular Tags