/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/ package org.apache.catalina.manager;
/** * This servlet will dump JMX attributes in a simple format and implement proxy services for modeler. * * @author Costin Manolache
*/ publicclass JMXProxyServlet extends HttpServlet {
privatestaticfinallong serialVersionUID = 1L;
// Constant for "no parameters" when invoking a JMX operation // without any parameters. privatestaticfinal String[] NO_PARAMETERS = new String[0];
privatestaticfinal StringManager sm = StringManager.getManager(JMXProxyServlet.class);
// --------------------------------------------------------- Public Methods /** * Initialize this servlet.
*/
@Override publicvoid init() throws ServletException { // Retrieve the MBean server
registry = Registry.getRegistry(null, null);
mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
}
/** * Process a GET request for the specified resource. * * @param request The servlet request we are processing * @param response The servlet response we are creating * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet-specified error occurs
*/
@Override publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/plain;charset=" + Constants.CHARSET); // Stop older versions of IE thinking they know best. We set text/plain // in the line above for a reason. IE's behaviour is unwanted at best // and dangerous at worst.
response.setHeader("X-Content-Type-Options", "nosniff");
PrintWriter writer = response.getWriter();
if (mBeanServer == null) {
writer.println("Error - No mbean server"); return;
}
String qry = request.getParameter("set"); if (qry != null) {
String name = request.getParameter("att");
String val = request.getParameter("val");
/** * Determines if a type is supported by the {@link JMXProxyServlet}. * * @param type The type to check * * @return Always returns <code>true</code>
*/ publicboolean isSupported(String type) { returntrue;
}
/** * Parses parameter values from a parameter string. * * @param paramString The string containing comma-separated operation-invocation parameters, or <code>null</code> if * there are no parameters. * * @return An array of String parameters (empty array if <code>paramString</code> was <code>null</code>).
*/ private String[] getInvokeParameters(String paramString) { if (paramString == null) { return NO_PARAMETERS;
} else { return paramString.split(",");
}
}
/** * Sets an MBean attribute's value.
*/ privatevoid setAttributeInternal(String onameStr, String attributeName, String value) throws OperationsException, MBeanException, ReflectionException {
ObjectName oname = new ObjectName(onameStr);
String type = registry.getType(oname, attributeName);
Object valueObj = registry.convertValue(type, value);
mBeanServer.setAttribute(oname, new Attribute(attributeName, valueObj));
}
/** * Invokes an operation on an MBean. * * @param onameStr The name of the MBean. * @param operation The name of the operation to invoke. * @param parameters An array of Strings containing the parameters to the operation. They will be converted to the * appropriate types to call the requested operation. * * @return The value returned by the requested operation.
*/
@SuppressWarnings("null") // parameters can't be null if signature.length > 0 private Object invokeOperationInternal(String onameStr, String operation, String[] parameters) throws OperationsException, MBeanException, ReflectionException {
ObjectName oname = new ObjectName(onameStr); int paramCount = null == parameters ? 0 : parameters.length;
MBeanOperationInfo methodInfo = registry.getMethodInfo(oname, operation, paramCount); if (null == methodInfo) { // getMethodInfo returns null for both "object not found" and "operation not found"
MBeanInfo info = null; try {
info = registry.getMBeanServer().getMBeanInfo(oname);
} catch (InstanceNotFoundException infe) { throw infe;
} catch (Exception e) { thrownew IllegalArgumentException(sm.getString("jmxProxyServlet.noBeanFound", onameStr), e);
} thrownew IllegalArgumentException(sm.getString("jmxProxyServlet.noOperationOnBean", operation,
Integer.valueOf(paramCount), onameStr, info.getClassName()));
}
MBeanParameterInfo[] signature = methodInfo.getSignature();
String[] signatureTypes = new String[signature.length];
Object[] values = new Object[signature.length]; for (int i = 0; i < signature.length; i++) {
MBeanParameterInfo pi = signature[i];
signatureTypes[i] = pi.getType();
values[i] = registry.convertValue(pi.getType(), parameters[i]);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.