This is an automated email from the ASF dual-hosted git repository. ewencp pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/trunk by this push: new 938580f KAFKA-7813: JmxTool throws NPE when --object-name is omitted 938580f is described below commit 938580ff6c5c27b1d7a3baf9cc09029ef3c2eb68 Author: huxihx <huxi...@hotmail.com> AuthorDate: Sun Mar 17 18:50:55 2019 -0700 KAFKA-7813: JmxTool throws NPE when --object-name is omitted https://issues.apache.org/jira/browse/KAFKA-7813 Running the JMX tool without --object-name parameter, results in a NullPointerException. *More detailed description of your change, if necessary. The PR title and PR message become the squashed commit message, so use a separate comment to ping reviewers.* *Summary of testing strategy (including rationale) for the feature or bug fix. Unit and/or integration tests are expected for any behaviour change and system tests should be considered for larger changes.* Author: huxihx <huxi...@hotmail.com> Reviewers: Ewen Cheslack-Postava <e...@confluent.io> Closes #6139 from huxihx/KAFKA-7813 --- core/src/main/scala/kafka/tools/JmxTool.scala | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/core/src/main/scala/kafka/tools/JmxTool.scala b/core/src/main/scala/kafka/tools/JmxTool.scala index c5303a9..9451cc2 100644 --- a/core/src/main/scala/kafka/tools/JmxTool.scala +++ b/core/src/main/scala/kafka/tools/JmxTool.scala @@ -18,7 +18,7 @@ */ package kafka.tools -import java.util.Date +import java.util.{Date, Objects} import java.text.SimpleDateFormat import javax.management._ import javax.management.remote._ @@ -28,7 +28,7 @@ import joptsimple.OptionParser import scala.collection.JavaConverters._ import scala.collection.mutable import scala.math._ -import kafka.utils.{CommandLineUtils , Exit, Logging} +import kafka.utils.{CommandLineUtils, Exit, Logging} /** @@ -140,7 +140,7 @@ object JmxTool extends Logging { else List(null) - val hasPatternQueries = queries.exists((name: ObjectName) => name.isPattern) + val hasPatternQueries = queries.filterNot(Objects.isNull).exists((name: ObjectName) => name.isPattern) var names: Iterable[ObjectName] = null def namesSet = Option(names).toSet.flatten @@ -165,12 +165,20 @@ object JmxTool extends Logging { } val numExpectedAttributes: Map[ObjectName, Int] = - if (attributesWhitelistExists) - queries.map((_, attributesWhitelist.get.length)).toMap - else { - names.map{(name: ObjectName) => + if (!attributesWhitelistExists) + names.map{name: ObjectName => val mbean = mbsc.getMBeanInfo(name) (name, mbsc.getAttributes(name, mbean.getAttributes.map(_.getName)).size)}.toMap + else { + if (!hasPatternQueries) + names.map{name: ObjectName => + val mbean = mbsc.getMBeanInfo(name) + val attributes = mbsc.getAttributes(name, mbean.getAttributes.map(_.getName)) + val expectedAttributes = attributes.asScala.asInstanceOf[mutable.Buffer[Attribute]] + .filter(attr => attributesWhitelist.get.contains(attr.getName)) + (name, expectedAttributes.size)}.toMap.filter(_._2 > 0) + else + queries.map((_, attributesWhitelist.get.length)).toMap } if(numExpectedAttributes.isEmpty) {