tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1231762 - in /tomcat/trunk: java/org/apache/catalina/manager/JMXProxyServlet.java webapps/docs/manager-howto.xml
Date Sun, 15 Jan 2012 21:37:19 GMT
Author: markt
Date: Sun Jan 15 21:37:18 2012
New Revision: 1231762

URL: http://svn.apache.org/viewvc?rev=1231762&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=38216
Add the ability to invoke MBean operations through the JMXProxy
Based on a patch by Christopher Hlubek
Also improved error reporting to help debugging.

Modified:
    tomcat/trunk/java/org/apache/catalina/manager/JMXProxyServlet.java
    tomcat/trunk/webapps/docs/manager-howto.xml

Modified: tomcat/trunk/java/org/apache/catalina/manager/JMXProxyServlet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/manager/JMXProxyServlet.java?rev=1231762&r1=1231761&r2=1231762&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/manager/JMXProxyServlet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/manager/JMXProxyServlet.java Sun Jan 15 21:37:18
2012
@@ -14,16 +14,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-
 package org.apache.catalina.manager;
 
-
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Set;
 
 import javax.management.Attribute;
+import javax.management.MBeanOperationInfo;
+import javax.management.MBeanParameterInfo;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 import javax.servlet.ServletException;
@@ -101,6 +100,19 @@ public class JMXProxyServlet extends Htt
             getAttribute( writer, qry, name );
             return;
         }
+        qry = request.getParameter("invoke");
+        if(qry != null) {
+            String opName=request.getParameter("op");
+            String ps = request.getParameter("ps");
+            String[] valuesStr;
+            if (ps == null) {
+                valuesStr = new String[0];
+            } else {
+                valuesStr = request.getParameter("ps").split(",");
+            }
+            invokeOperation( writer, qry, opName,valuesStr );
+            return;
+        }
         qry=request.getParameter("qry");
         if( qry == null ) {
             qry = "*:*";
@@ -117,7 +129,8 @@ public class JMXProxyServlet extends Htt
             writer.println("OK - Attribute get '" + onameStr + "' - " + att
                     + "= " + MBeanDumper.escape(value.toString()));
         } catch (Exception ex) {
-            writer.println("Error - " + ex.toString());
+            writer.println("Error");
+            ex.printStackTrace(writer);
         }
     }
 
@@ -131,7 +144,8 @@ public class JMXProxyServlet extends Htt
             mBeanServer.setAttribute( oname, new Attribute(att, valueObj));
             writer.println("OK - Attribute set");
         } catch( Exception ex ) {
-            writer.println("Error - " + ex.toString());
+            writer.println("Error");
+            ex.printStackTrace(writer);
         }
     }
 
@@ -143,8 +157,9 @@ public class JMXProxyServlet extends Htt
             names=mBeanServer.queryNames(new ObjectName(qry), null);
             writer.println("OK - Number of results: " + names.size());
             writer.println();
-        } catch (Exception e) {
-            writer.println("Error - " + e.toString());
+        } catch (Exception ex) {
+            writer.println("Error");
+            ex.printStackTrace(writer);
             return;
         }
 
@@ -161,4 +176,38 @@ public class JMXProxyServlet extends Htt
     public boolean isSupported(String type) {
         return true;
     }
+
+
+    private void invokeOperation(PrintWriter writer, String onameStr, String op,
+            String[] valuesStr) {
+        try {
+            ObjectName oname=new ObjectName( onameStr );
+            MBeanOperationInfo methodInfo = registry.getMethodInfo(oname,op);
+            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(), valuesStr[i] );
+           }
+
+            Object retVal = mBeanServer.invoke(oname,op,values,signatureTypes);
+            writer.println("OK - Operation " + op + " returned:");
+            output("", writer, retVal);
+        } catch( Exception ex ) {
+            writer.println("Error");
+            ex.printStackTrace(writer);
+        }
+    }
+
+    private void output(String indent, PrintWriter writer, Object result) {
+        if (result instanceof Object[]) {
+            for (Object obj : (Object[]) result) {
+                output("  " + indent, writer, obj);
+            }
+        } else {
+            writer.println(indent + result.toString());
+        }
+    }
 }

Modified: tomcat/trunk/webapps/docs/manager-howto.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/manager-howto.xml?rev=1231762&r1=1231761&r2=1231762&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/manager-howto.xml (original)
+++ tomcat/trunk/webapps/docs/manager-howto.xml Sun Jan 15 21:37:18 2012
@@ -1301,6 +1301,18 @@ Error: java.lang.NumberFormatException: 
 </source>
   </subsection>
 
+  <subsection name="JMX Invoke command">
+    <p>The <code>invoke</code> command enables methods to be called on
MBeans. The
+    general form of the command is:</p>
+<source>
+http://webserver/manager/jmxproxy/?invoke=BEANNAME&amp;op=METHODNAME&amp;ps=COMMASEPARATEDPARAMETERS
+</source>
+    <p>For exmaple, to call the <code>findConnectors()</code> method of
the
+    <strong>Service</strong> use:</p>
+<source>
+http://localhost:8080/manager/jmxproxy/?invoke=Catalina%3Atype%3DService&amp;op=findConnectors&amp;ps=
+</source>
+  </subsection>
 
 </section>
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message