Make Tomcat more tolerant of nulls when generating JMX names for Valves.
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 1 Nov 2010 17:22:33 +0000 (17:22 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 1 Nov 2010 17:22:33 +0000 (17:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1029755 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/mbeans/MBeanUtils.java
java/org/apache/catalina/valves/ValveBase.java
webapps/docs/changelog.xml

index c14a32c..9388506 100644 (file)
@@ -1690,7 +1690,7 @@ public class MBeanUtils {
         
         Container c = container;
         StringBuilder keyProperties = new StringBuilder();
-        int unknown = 0;
+        int containerCount = 0;
         
         // Work up container hierarchy, add a component to the name for
         // each container
@@ -1708,10 +1708,16 @@ public class MBeanUtils {
             } else if (c instanceof Host) {
                 keyProperties.append(",host=");
                 keyProperties.append(c.getName());
+            } else if (c == null) {
+                // May happen in unit testing and/or some embedding scenarios
+                keyProperties.append(",container");
+                keyProperties.append(containerCount++);
+                keyProperties.append("=null");
+                break;
             } else {
                 // Should never happen...
-                keyProperties.append(",unknown");
-                keyProperties.append(unknown++);
+                keyProperties.append(",container");
+                keyProperties.append(containerCount++);
                 keyProperties.append('=');
                 keyProperties.append(c.getName());
             }
index fb5ed1c..07ceb50 100644 (file)
@@ -27,6 +27,7 @@ import org.apache.catalina.Contained;
 import org.apache.catalina.Container;
 import org.apache.catalina.LifecycleException;
 import org.apache.catalina.LifecycleState;
+import org.apache.catalina.Pipeline;
 import org.apache.catalina.Valve;
 import org.apache.catalina.comet.CometEvent;
 import org.apache.catalina.connector.Request;
@@ -289,19 +290,24 @@ public abstract class ValveBase extends LifecycleMBeanBase
         name.append(MBeanUtils.getContainerKeyProperties(container));
         
         int seq = 0;
-        for (Valve valve : container.getPipeline().getValves()) {
-            // Skip null valves
-            if (valve == null) {
-                continue;
-            }
-            // Only compare valves in pipeline until we find this valve
-            if (valve == this) {
-                break;
-            }
-            if (valve.getClass() == this.getClass()) {
-                // Duplicate valve earlier in pipeline
-                // increment sequence number
-                seq ++;
+        
+        // Pipeline may not be present in unit testing
+        Pipeline p = container.getPipeline();
+        if (p != null) {
+            for (Valve valve : p.getValves()) {
+                // Skip null valves
+                if (valve == null) {
+                    continue;
+                }
+                // Only compare valves in pipeline until we find this valve
+                if (valve == this) {
+                    break;
+                }
+                if (valve.getClass() == this.getClass()) {
+                    // Duplicate valve earlier in pipeline
+                    // increment sequence number
+                    seq ++;
+                }
             }
         }
         
index c9acc60..69249a1 100644 (file)
         <bug>49180</bug>: Add option to disable log rotation in 
         juli FileHandler. credit: Pid (pidster at apache )
       </fix>
+      <fix>
+        Make Tomcat more tolerant of <code>null</code> when generating JMX names
+        for Valves. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">