001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jmx.adaptor.html;
023:
024: import java.io.IOException;
025: import java.util.ArrayList;
026: import java.util.Enumeration;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import javax.management.AttributeList;
030: import javax.management.JMException;
031: import javax.servlet.RequestDispatcher;
032: import javax.servlet.ServletConfig;
033: import javax.servlet.ServletException;
034: import javax.servlet.http.HttpServlet;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037: import javax.servlet.http.HttpSession;
038:
039: import org.jboss.logging.Logger;
040: import org.jboss.jmx.adaptor.control.OpResultInfo;
041: import org.jboss.jmx.adaptor.control.Server;
042: import org.jboss.jmx.adaptor.model.MBeanData;
043:
044: /** The HTML adaptor controller servlet.
045: *
046: * @author Scott.Stark@jboss.org
047: * @version $Revision: 57210 $
048: */
049: public class HtmlAdaptorServlet extends HttpServlet {
050: private static Logger log = Logger
051: .getLogger(HtmlAdaptorServlet.class);
052: private static final String ACTION_PARAM = "action";
053: private static final String FILTER_PARAM = "filter";
054: private static final String DISPLAY_MBEANS_ACTION = "displayMBeans";
055: private static final String INSPECT_MBEAN_ACTION = "inspectMBean";
056: private static final String UPDATE_ATTRIBUTES_ACTION = "updateAttributes";
057: private static final String INVOKE_OP_ACTION = "invokeOp";
058: private static final String INVOKE_OP_BY_NAME_ACTION = "invokeOpByName";
059:
060: /** Creates a new instance of HtmlAdaptor */
061: public HtmlAdaptorServlet() {
062: }
063:
064: public void init(ServletConfig config) throws ServletException {
065: super .init(config);
066: }
067:
068: public void destroy() {
069: }
070:
071: protected void doGet(HttpServletRequest request,
072: HttpServletResponse response) throws ServletException,
073: IOException {
074: processRequest(request, response);
075: }
076:
077: protected void doPost(HttpServletRequest request,
078: HttpServletResponse response) throws ServletException,
079: IOException {
080: processRequest(request, response);
081: }
082:
083: protected void processRequest(HttpServletRequest request,
084: HttpServletResponse response) throws ServletException,
085: IOException {
086: String action = request.getParameter(ACTION_PARAM);
087:
088: if (action == null)
089: action = DISPLAY_MBEANS_ACTION;
090:
091: if (action.equals(DISPLAY_MBEANS_ACTION))
092: displayMBeans(request, response);
093: else if (action.equals(INSPECT_MBEAN_ACTION))
094: inspectMBean(request, response);
095: else if (action.equals(UPDATE_ATTRIBUTES_ACTION))
096: updateAttributes(request, response);
097: else if (action.equals(INVOKE_OP_ACTION))
098: invokeOp(request, response);
099: else if (action.equals(INVOKE_OP_BY_NAME_ACTION))
100: invokeOpByName(request, response);
101: }
102:
103: /** Display all mbeans categorized by domain
104: */
105: private void displayMBeans(HttpServletRequest request,
106: HttpServletResponse response) throws ServletException,
107: IOException {
108: // get ObjectName filter from request or session context
109: HttpSession session = request.getSession(false);
110: String filter = request.getParameter(FILTER_PARAM);
111:
112: if (filter == null && session != null) {
113: // try using previously provided filter from session context
114: filter = (String) session.getAttribute(FILTER_PARAM);
115: }
116:
117: if (filter != null && filter.length() > 0) {
118: // Strip any enclosing quotes
119: if (filter.charAt(0) == '"')
120: filter = filter.substring(1);
121: if (filter.charAt(filter.length() - 1) == '"')
122: filter = filter.substring(0, filter.length() - 2);
123:
124: // be a litte it tolerant to user input
125: String domain = "*";
126: String props = "*,*";
127:
128: int separator = filter.indexOf(':');
129: int assignment = filter.indexOf('=');
130:
131: if (separator == -1 && assignment != -1) {
132: // assume properties only
133: props = filter.trim();
134: } else if (separator == -1 && assignment == -1) {
135: // assume domain name only
136: domain = filter.trim();
137: } else {
138: // domain and properties
139: domain = filter.substring(0, separator).trim();
140: props = filter.substring(separator + 1).trim();
141: }
142:
143: if (domain.equals(""))
144: domain = "*";
145:
146: if (props.equals(""))
147: props = "*,*";
148: if (props.endsWith(","))
149: props += "*";
150: if (!props.endsWith(",*"))
151: props += ",*";
152: if (props.equals("*,*"))
153: props = "*";
154:
155: filter = domain + ":" + props;
156: ;
157:
158: if (filter.equals("*:*"))
159: filter = "";
160: } else {
161: filter = "";
162: }
163:
164: // update request filter and store filter in session context,
165: // so it can be used when no filter has been submitted in
166: // current request
167: request.setAttribute(FILTER_PARAM, filter);
168:
169: if (session != null) {
170: session.setAttribute(FILTER_PARAM, filter);
171: }
172:
173: try {
174: Iterator mbeans = Server.getDomainData(filter);
175: request.setAttribute("mbeans", mbeans);
176: RequestDispatcher rd = this .getServletContext()
177: .getRequestDispatcher("/displayMBeans.jsp");
178: rd.forward(request, response);
179: } catch (JMException e) {
180: throw new ServletException("Failed to get MBeans", e);
181: }
182: }
183:
184: /** Display an mbeans attributes and operations
185: */
186: private void inspectMBean(HttpServletRequest request,
187: HttpServletResponse response) throws ServletException,
188: IOException {
189: String name = request.getParameter("name");
190: log.trace("inspectMBean, name=" + name);
191: try {
192: MBeanData data = Server.getMBeanData(name);
193: request.setAttribute("mbeanData", data);
194: RequestDispatcher rd = this .getServletContext()
195: .getRequestDispatcher("/inspectMBean.jsp");
196: rd.forward(request, response);
197: } catch (JMException e) {
198: throw new ServletException("Failed to get MBean data", e);
199: }
200: }
201:
202: /** Update the writable attributes of an mbean
203: */
204: private void updateAttributes(HttpServletRequest request,
205: HttpServletResponse response) throws ServletException,
206: IOException {
207: String name = request.getParameter("name");
208: log.trace("updateAttributes, name=" + name);
209: Enumeration paramNames = request.getParameterNames();
210: HashMap attributes = new HashMap();
211: while (paramNames.hasMoreElements()) {
212: String param = (String) paramNames.nextElement();
213: if (param.equals("name") || param.equals("action"))
214: continue;
215: String value = request.getParameter(param);
216: log.trace("name=" + param + ", value='" + value + "'");
217: // Ignore null values, these are empty write-only fields
218: if (value == null || value.length() == 0)
219: continue;
220: attributes.put(param, value);
221: }
222:
223: try {
224: AttributeList newAttributes = Server.setAttributes(name,
225: attributes);
226: MBeanData data = Server.getMBeanData(name);
227: request.setAttribute("mbeanData", data);
228: RequestDispatcher rd = this .getServletContext()
229: .getRequestDispatcher("/inspectMBean.jsp");
230: rd.forward(request, response);
231: } catch (JMException e) {
232: throw new ServletException("Failed to update attributes", e);
233: }
234: }
235:
236: /** Invoke an mbean operation given the index into the MBeanOperationInfo{}
237: array of the mbean.
238: */
239: private void invokeOp(HttpServletRequest request,
240: HttpServletResponse response) throws ServletException,
241: IOException {
242: String name = request.getParameter("name");
243: log.trace("invokeOp, name=" + name);
244: String[] args = getArgs(request);
245: String methodIndex = request.getParameter("methodIndex");
246: if (methodIndex == null || methodIndex.length() == 0)
247: throw new ServletException(
248: "No methodIndex given in invokeOp form");
249: int index = Integer.parseInt(methodIndex);
250: try {
251: OpResultInfo opResult = Server.invokeOp(name, index, args);
252: request.setAttribute("opResultInfo", opResult);
253: RequestDispatcher rd = this .getServletContext()
254: .getRequestDispatcher("/displayOpResult.jsp");
255: rd.forward(request, response);
256: } catch (JMException e) {
257: throw new ServletException("Failed to invoke operation", e);
258: }
259: }
260:
261: /** Invoke an mbean operation given the method name and its signature.
262: */
263: private void invokeOpByName(HttpServletRequest request,
264: HttpServletResponse response) throws ServletException,
265: IOException {
266: String name = request.getParameter("name");
267: log.trace("invokeOpByName, name=" + name);
268: String[] argTypes = request.getParameterValues("argType");
269: String[] args = getArgs(request);
270: String methodName = request.getParameter("methodName");
271: if (methodName == null)
272: throw new ServletException(
273: "No methodName given in invokeOpByName form");
274: try {
275: OpResultInfo opResult = Server.invokeOpByName(name,
276: methodName, argTypes, args);
277: request.setAttribute("opResultInfo", opResult);
278: RequestDispatcher rd = this .getServletContext()
279: .getRequestDispatcher("/displayOpResult.jsp");
280: rd.forward(request, response);
281: } catch (JMException e) {
282: throw new ServletException("Failed to invoke operation", e);
283: }
284: }
285:
286: /** Extract the argN values from the request into a String[]
287: */
288: private String[] getArgs(HttpServletRequest request) {
289: ArrayList argList = new ArrayList();
290: for (int i = 0; true; i++) {
291: String name = "arg" + i;
292: String value = request.getParameter(name);
293: if (value == null)
294: break;
295: argList.add(value);
296: log.trace(name + "=" + value);
297: }
298: String[] args = new String[argList.size()];
299: argList.toArray(args);
300: return args;
301: }
302: }
|