tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rj...@apache.org
Subject svn commit: r1229549 - in /tomcat/trunk: java/org/apache/catalina/manager/JMXProxyServlet.java java/org/apache/catalina/mbeans/MBeanDumper.java test/org/apache/catalina/mbeans/MBeanDumper.java test/org/apache/catalina/mbeans/TestRegistration.java
Date Tue, 10 Jan 2012 14:14:34 GMT
Author: rjung
Date: Tue Jan 10 14:14:34 2012
New Revision: 1229549

URL: http://svn.apache.org/viewvc?rev=1229549&view=rev
Log:
Reduce code duplication introduced in r1229536:
Use common MBeanDumper class for JMXProxyServlet
and Mean unit tests.

Added:
    tomcat/trunk/java/org/apache/catalina/mbeans/MBeanDumper.java
      - copied, changed from r1229536, tomcat/trunk/test/org/apache/catalina/mbeans/MBeanDumper.java
Removed:
    tomcat/trunk/test/org/apache/catalina/mbeans/MBeanDumper.java
Modified:
    tomcat/trunk/java/org/apache/catalina/manager/JMXProxyServlet.java
    tomcat/trunk/test/org/apache/catalina/mbeans/TestRegistration.java

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=1229549&r1=1229548&r2=1229549&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/manager/JMXProxyServlet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/manager/JMXProxyServlet.java Tue Jan 10 14:14:34
2012
@@ -21,13 +21,9 @@ package org.apache.catalina.manager;
 
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.lang.reflect.Array;
-import java.util.Iterator;
 import java.util.Set;
 
 import javax.management.Attribute;
-import javax.management.MBeanAttributeInfo;
-import javax.management.MBeanInfo;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 import javax.servlet.ServletException;
@@ -35,7 +31,7 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.catalina.mbeans.MBeanDumper;
 import org.apache.tomcat.util.modeler.Registry;
 
 /**
@@ -119,7 +115,7 @@ public class JMXProxyServlet extends Htt
             ObjectName oname = new ObjectName(onameStr);
             Object value = mBeanServer.getAttribute(oname, att);
             writer.println("OK - Attribute get '" + onameStr + "' - " + att
-                    + "= " + escape(value.toString()));
+                    + "= " + MBeanDumper.escape(value.toString()));
         } catch (Exception ex) {
             writer.println("Error - " + ex.toString());
         }
@@ -152,121 +148,8 @@ public class JMXProxyServlet extends Htt
             return;
         }
 
-        Iterator<ObjectName> it=names.iterator();
-        while( it.hasNext()) {
-            ObjectName oname=it.next();
-            writer.println( "Name: " + oname.toString());
-
-            try {
-                MBeanInfo minfo=mBeanServer.getMBeanInfo(oname);
-                // can't be null - I think
-                String code=minfo.getClassName();
-                if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
-                    code=(String)mBeanServer.getAttribute(oname, "modelerType");
-                }
-                writer.println("modelerType: " + code);
-
-                MBeanAttributeInfo attrs[]=minfo.getAttributes();
-                Object value=null;
-
-                for( int i=0; i< attrs.length; i++ ) {
-                    if( ! attrs[i].isReadable() ) continue;
-                    if( ! isSupported( attrs[i].getType() )) continue;
-                    String attName=attrs[i].getName();
-                    if( "modelerType".equals( attName)) continue;
-                    if( attName.indexOf( "=") >=0 ||
-                            attName.indexOf( ":") >=0 ||
-                            attName.indexOf( " ") >=0 ) {
-                        continue;
-                    }
-
-                    try {
-                        value=mBeanServer.getAttribute(oname, attName);
-                    } catch( Throwable t) {
-                        log("Error getting attribute " + oname +
-                            " " + attName + " " + t.toString());
-                        continue;
-                    }
-                    if( value==null ) continue;
-                    String valueString;
-                    try {
-                        Class<?> c = value.getClass();
-                        if (c.isArray()) {
-                            int len = Array.getLength(value);
-                            StringBuilder sb = new StringBuilder("Array[" +
-                                    c.getComponentType().getName() + "] of length " + len);
-                            if (len > 0) {
-                                sb.append("\r\n");
-                            }
-                            for (int j = 0; j < len; j++) {
-                                sb.append("\t");
-                                Object item = Array.get(value, j);
-                                if (item == null) {
-                                    sb.append("NULL VALUE");
-                                } else {
-                                    try {
-                                        sb.append(escape(item.toString()));
-                                    }
-                                    catch (Throwable t) {
-                                        ExceptionUtils.handleThrowable(t);
-                                        sb.append("NON-STRINGABLE VALUE");
-                                    }
-                                }
-                                if (j < len - 1) {
-                                    sb.append("\r\n");
-                                }
-                            }
-                            valueString = sb.toString();
-                        }
-                        else {
-                            valueString = escape(value.toString());
-                        }
-                        writer.println( attName + ": " + valueString);
-                    }
-                    catch (Throwable t) {
-                        ExceptionUtils.handleThrowable(t);
-                    }
-                }
-            } catch (Throwable t) {
-                ExceptionUtils.handleThrowable(t);
-            }
-            writer.println();
-        }
-
-    }
-
-    public String escape(String value) {
-        // The only invalid char is \n
-        // We also need to keep the string short and split it with \nSPACE
-        // XXX TODO
-        int idx=value.indexOf( "\n" );
-        if( idx < 0 ) return value;
-
-        int prev=0;
-        StringBuilder sb=new StringBuilder();
-        while( idx >= 0 ) {
-            appendHead(sb, value, prev, idx);
-
-            sb.append( "\\n\n ");
-            prev=idx+1;
-            if( idx==value.length() -1 ) break;
-            idx=value.indexOf('\n', idx+1);
-        }
-        if( prev < value.length() )
-            appendHead( sb, value, prev, value.length());
-        return sb.toString();
-    }
-
-    private void appendHead( StringBuilder sb, String value, int start, int end) {
-        if (end < 1) return;
-
-        int pos=start;
-        while( end-pos > 78 ) {
-            sb.append( value.substring(pos, pos+78));
-            sb.append( "\n ");
-            pos=pos+78;
-        }
-        sb.append( value.substring(pos,end));
+        String dump = MBeanDumper.dumpBeans(mBeanServer, names);
+        writer.print(dump);
     }
 
     /**

Copied: tomcat/trunk/java/org/apache/catalina/mbeans/MBeanDumper.java (from r1229536, tomcat/trunk/test/org/apache/catalina/mbeans/MBeanDumper.java)
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/mbeans/MBeanDumper.java?p2=tomcat/trunk/java/org/apache/catalina/mbeans/MBeanDumper.java&p1=tomcat/trunk/test/org/apache/catalina/mbeans/MBeanDumper.java&r1=1229536&r2=1229549&rev=1229549&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/mbeans/MBeanDumper.java (original)
+++ tomcat/trunk/java/org/apache/catalina/mbeans/MBeanDumper.java Tue Jan 10 14:14:34 2012
@@ -35,19 +35,23 @@ import org.apache.tomcat.util.ExceptionU
  */
 public class MBeanDumper {
 
-    private static Log log = LogFactory.getLog(MBeanDumper.class);
+    private static final Log log = LogFactory.getLog(MBeanDumper.class);
+
+    private static final String CRLF = "\r\n";
 
     /**
      * The following code to dump MBeans has been copied from JMXProxyServlet.
      *
      */
-    public static void listBeans(MBeanServer mbeanServer, Set<ObjectName> names)
+    public static String dumpBeans(MBeanServer mbeanServer, Set<ObjectName> names)
     {
-
+        StringBuilder buf = new StringBuilder();
         Iterator<ObjectName> it=names.iterator();
         while( it.hasNext()) {
             ObjectName oname=it.next();
-            log.info( "Name: " + oname.toString());
+            buf.append("Name: ");
+            buf.append(oname.toString());
+            buf.append(CRLF);
 
             try {
                 MBeanInfo minfo=mbeanServer.getMBeanInfo(oname);
@@ -56,29 +60,31 @@ public class MBeanDumper {
                 if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                     code=(String)mbeanServer.getAttribute(oname, "modelerType");
                 }
-                log.info("modelerType: " + code);
+                buf.append("modelerType: ");
+                buf.append(code);
+                buf.append(CRLF);
 
                 MBeanAttributeInfo attrs[]=minfo.getAttributes();
                 Object value=null;
 
-                for( int i=0; i< attrs.length; i++ ) {
-                    if( ! attrs[i].isReadable() ) continue;
+                for (int i=0; i< attrs.length; i++) {
+                    if (! attrs[i].isReadable()) continue;
                     String attName=attrs[i].getName();
-                    if( "modelerType".equals( attName)) continue;
-                    if( attName.indexOf( "=") >=0 ||
-                            attName.indexOf( ":") >=0 ||
-                            attName.indexOf( " ") >=0 ) {
+                    if ("modelerType".equals(attName)) continue;
+                    if (attName.indexOf("=") >=0 ||
+                            attName.indexOf(":") >=0 ||
+                            attName.indexOf(" ") >=0 ) {
                         continue;
                     }
 
                     try {
                         value=mbeanServer.getAttribute(oname, attName);
-                    } catch( Throwable t) {
+                    } catch (Throwable t) {
                         log.error("Error getting attribute " + oname +
-                            " " + attName + " " + t.toString(), t);
+                            " " + attName + " " + t.toString());
                         continue;
                     }
-                    if( value==null ) continue;
+                    if (value==null) continue;
                     String valueString;
                     try {
                         Class<?> c = value.getClass();
@@ -87,7 +93,7 @@ public class MBeanDumper {
                             StringBuilder sb = new StringBuilder("Array[" +
                                     c.getComponentType().getName() + "] of length " + len);
                             if (len > 0) {
-                                sb.append("\r\n");
+                                sb.append(CRLF);
                             }
                             for (int j = 0; j < len; j++) {
                                 sb.append("\t");
@@ -104,7 +110,7 @@ public class MBeanDumper {
                                     }
                                 }
                                 if (j < len - 1) {
-                                    sb.append("\r\n");
+                                    sb.append(CRLF);
                                 }
                             }
                             valueString = sb.toString();
@@ -112,7 +118,10 @@ public class MBeanDumper {
                         else {
                             valueString = escape(value.toString());
                         }
-                        log.info( attName + ": " + valueString);
+                        buf.append(attName);
+                        buf.append(": ");
+                        buf.append(valueString);
+                        buf.append(CRLF);
                     }
                     catch (Throwable t) {
                         ExceptionUtils.handleThrowable(t);
@@ -121,8 +130,9 @@ public class MBeanDumper {
             } catch (Throwable t) {
                 ExceptionUtils.handleThrowable(t);
             }
-            log.info("");
+            buf.append(CRLF);
         }
+        return buf.toString();
 
     }
 

Modified: tomcat/trunk/test/org/apache/catalina/mbeans/TestRegistration.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/mbeans/TestRegistration.java?rev=1229549&r1=1229548&r2=1229549&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/mbeans/TestRegistration.java (original)
+++ tomcat/trunk/test/org/apache/catalina/mbeans/TestRegistration.java Tue Jan 10 14:14:34
2012
@@ -133,10 +133,10 @@ public class TestRegistration extends To
         final MBeanServer mbeanServer = Registry.getRegistry(null, null).getMBeanServer();
         // Verify there are no Catalina or Tomcat MBeans
         Set<ObjectName> onames = mbeanServer.queryNames(new ObjectName("Catalina:*"),
null);
-        MBeanDumper.listBeans(mbeanServer, onames);
+        log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
         assertEquals("Unexpected: " + onames, 0, onames.size());
         onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
-        MBeanDumper.listBeans(mbeanServer, onames);
+        log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
         assertEquals("Unexpected: " + onames, 0, onames.size());
 
         final Tomcat tomcat = getTomcatInstance();
@@ -150,7 +150,7 @@ public class TestRegistration extends To
 
         // Verify there are no Catalina MBeans
         onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null);
-        MBeanDumper.listBeans(mbeanServer, onames);
+        log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
         assertEquals("Found: " + onames, 0, onames.size());
 
         // Verify there are the correct Tomcat MBeans
@@ -210,10 +210,10 @@ public class TestRegistration extends To
 
         // There should be no Catalina MBeans and no Tomcat MBeans
         onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null);
-        MBeanDumper.listBeans(mbeanServer, onames);
+        log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
         assertEquals("Remaining: " + onames, 0, onames.size());
         onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
-        MBeanDumper.listBeans(mbeanServer, onames);
+        log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
         assertEquals("Remaining: " + onames, 0, onames.size());
     }
 



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


Mime
View raw message