1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     7104647
  27  * @summary Basic Test for HotSpotDiagnosticMXBean.getDiagnosticCommandInfo()
  28  * @author  Frederic Parain
  29  * @run main GetDiagnosticCommandInfo
  30  */
  31 
  32 import com.sun.management.HotSpotDiagnosticMXBean;
  33 import com.sun.management.DiagnosticCommandInfo;
  34 import com.sun.management.DiagnosticCommandArgumentInfo;
  35 import com.sun.management.VMOption;
  36 import java.lang.management.ManagementFactory;
  37 import java.util.List;
  38 import javax.management.MBeanServer;
  39 
  40 public class GetDiagnosticCommandInfo {
  41     private static String HOTSPOT_DIAGNOSTIC_MXBEAN_NAME =
  42         "com.sun.management:type=HotSpotDiagnostic";
  43 
  44     public static void main(String[] args) throws Exception {
  45         HotSpotDiagnosticMXBean mbean =
  46             ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
  47         checkDiagnosticCommandArguments(mbean);
  48 
  49         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  50         mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
  51                     HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
  52                     HotSpotDiagnosticMXBean.class);
  53         checkDiagnosticCommandArguments(mbean);
  54     }
  55 
  56     private static void checkDiagnosticCommandArguments(HotSpotDiagnosticMXBean mbean) {
  57         // check getDiagnosticCommandInfo()
  58         StringBuilder sb = new StringBuilder();
  59         List<DiagnosticCommandInfo> infoList = mbean.getDiagnosticCommandInfo();
  60         for(DiagnosticCommandInfo info : infoList) {
  61             printCommandInfo(info,sb);
  62         }
  63         // check getDiagnosticCommandInfo(List<String>)
  64         List<String> commands = mbean.getDiagnosticCommands();
  65         List<DiagnosticCommandInfo> list2 =
  66             mbean.getDiagnosticCommandInfo(commands);
  67         for(DiagnosticCommandInfo info : list2) {
  68             printCommandInfo(info,sb);
  69         }
  70         // check getDiagnosticCommandInfo(String)
  71         for(String cmd : commands) {
  72             DiagnosticCommandInfo info2 = mbean.getDiagnosticCommandInfo(cmd);
  73             printCommandInfo(info2,sb);
  74         }
  75         System.out.println(sb.toString());
  76     }
  77 
  78     private static void printCommandInfo(DiagnosticCommandInfo info,
  79                                          StringBuilder sb) {
  80         sb.append("\t").append(info.getName()).append(":\n");
  81         sb.append("\t\tDescription=").append(info.getDescription()).append("\n");
  82         sb.append("\t\tImpact=").append(info.getImpact()).append("\n");
  83         sb.append("\t\tStatus=");
  84         if (info.isEnabled()) {
  85             sb.append("Enabled\n");
  86         } else {
  87             sb.append("Disbled\n");
  88         }
  89         sb.append("\t\tArguments=");
  90         for(DiagnosticCommandArgumentInfo arg : info.getArgumentsInfo()) {
  91             printArgumentInfo(arg,sb);
  92         }
  93     }
  94 
  95     private static void printArgumentInfo(DiagnosticCommandArgumentInfo info,
  96                                           StringBuilder sb) {
  97         sb.append("\t\t\t").append(info.getName()).append(":\n");
  98         sb.append("\t\t\t\tType=").append(info.getType()).append("\n");
  99         sb.append("\t\t\t\tDescription=").append(info.getDescription()).append("\n");
 100         if(info.getDefault() != null) {
 101             sb.append("\t\t\t\tDefault=").append(info.getDefault()).append("\n");
 102         }
 103         if(info.isMandatory()) {
 104             sb.append("\t\t\t\tMandatory\n");
 105         } else {
 106             sb.append("\t\t\t\tOptional\n");
 107         }
 108         if(info.isOption()) {
 109             sb.append("\t\t\t\tIs an option\n");
 110         } else {
 111             sb.append("\t\t\t\tIs an argument expected at position");
 112             sb.append(info.getPosition());
 113             sb.append("\n");
 114         }
 115     }
 116 }