1 /*
   2  * Copyright (c) 2012, 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     7150256
  27  * @summary Basic Test for the DiagnosticCommandMBean
  28  * @author  Frederic Parain
  29  *
  30  * @run main/othervm -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=8123 DcmdMBeanInvocationTest
  31  */
  32 
  33 
  34 import java.io.IOException;
  35 import java.lang.management.ManagementFactory;
  36 import java.util.logging.Level;
  37 import java.util.logging.Logger;
  38 import javax.management.Descriptor;
  39 import javax.management.InstanceNotFoundException;
  40 import javax.management.IntrospectionException;
  41 import javax.management.MBeanInfo;
  42 import javax.management.MBeanOperationInfo;
  43 import javax.management.MBeanServer;
  44 import javax.management.MalformedObjectNameException;
  45 import javax.management.ObjectName;
  46 import javax.management.ReflectionException;
  47 import javax.management.*;
  48 import javax.management.remote.*;
  49 
  50 public class DcmdMBeanInvocationTest {
  51 
  52     private static String HOTSPOT_DIAGNOSTIC_MXBEAN_NAME =
  53         "com.sun.management:type=DiagnosticCommand";
  54 
  55     public static void main(String[] args) {
  56         MBeanServerConnection mbs = null;
  57         try {
  58             JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8123/jmxrmi");
  59             JMXConnector connector = JMXConnectorFactory.connect(url);
  60             mbs = connector.getMBeanServerConnection();
  61         } catch(Throwable t) {
  62             t.printStackTrace();
  63         }
  64         ObjectName name;
  65         try {
  66             name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
  67             MBeanInfo info = mbs.getMBeanInfo(name);
  68             String[] helpArgs = {"-all"};
  69             Object[] dcmdArgs = {helpArgs};
  70             String[] signature = {"[Ljava/lang/String;"};
  71             String result = (String) mbs.invoke(name, "help", dcmdArgs, signature);
  72             System.out.println(result);
  73         } catch (InstanceNotFoundException|IntrospectionException|ReflectionException
  74                  |MalformedObjectNameException|MBeanException|IOException ex) {
  75             Logger.getLogger(DcmdMBeanTest.class.getName()).log(Level.SEVERE, null, ex);
  76         }
  77     }       
  78 }
  79