hotspot/test/testlibrary/com/oracle/java/testlibrary/dcmd/JMXExecutor.java
author miauno
Fri, 30 Jan 2015 20:00:57 +0100
changeset 28821 f7820f311663
permissions -rw-r--r--
8071908: Port internal Diagnostic Command tests and test framework to jtreg Reviewed-by: jbachorik, egahlin, ykantser, mtobiass
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28821
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
     1
/*
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
     4
 *
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
     8
 *
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    13
 * accompanied this code).
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    14
 *
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    18
 *
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    21
 * questions.
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    22
 */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    23
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    24
package com.oracle.java.testlibrary.dcmd;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    25
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    26
import com.oracle.java.testlibrary.OutputAnalyzer;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    27
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    28
import javax.management.*;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    29
import javax.management.remote.JMXConnector;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    30
import javax.management.remote.JMXConnectorFactory;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    31
import javax.management.remote.JMXServiceURL;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    32
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    33
import java.io.IOException;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    34
import java.io.PrintWriter;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    35
import java.io.StringWriter;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    36
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    37
import java.lang.management.ManagementFactory;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    38
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    39
import java.util.HashMap;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    40
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    41
/**
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    42
 * Executes Diagnostic Commands on the target VM (specified by a host/port combination or a full JMX Service URL) using
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    43
 * the JMX interface. If the target is not the current VM, the JMX Remote interface must be enabled beforehand.
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    44
 */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    45
public class JMXExecutor extends CommandExecutor {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    46
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    47
    private final MBeanServerConnection mbs;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    48
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    49
    /**
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    50
     * Instantiates a new JMXExecutor targeting the current VM
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    51
     */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    52
    public JMXExecutor() {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    53
        super();
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    54
        mbs = ManagementFactory.getPlatformMBeanServer();
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    55
    }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    56
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    57
    /**
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    58
     * Instantiates a new JMXExecutor targeting the VM indicated by the given host/port combination or a full JMX
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    59
     * Service URL
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    60
     *
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    61
     * @param target a host/port combination on the format "host:port" or a full JMX Service URL of the target VM
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    62
     */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    63
    public JMXExecutor(String target) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    64
        String urlStr;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    65
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    66
        if (target.matches("^\\w[\\w\\-]*(\\.[\\w\\-]+)*:\\d+$")) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    67
            /* Matches "hostname:port" */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    68
            urlStr = String.format("service:jmx:rmi:///jndi/rmi://%s/jmxrmi", target);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    69
        } else if (target.startsWith("service:")) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    70
            urlStr = target;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    71
        } else {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    72
            throw new IllegalArgumentException("Could not recognize target string: " + target);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    73
        }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    74
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    75
        try {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    76
            JMXServiceURL url = new JMXServiceURL(urlStr);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    77
            JMXConnector c = JMXConnectorFactory.connect(url, new HashMap<>());
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    78
            mbs = c.getMBeanServerConnection();
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    79
        } catch (IOException e) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    80
            throw new CommandExecutorException("Could not initiate connection to target: " + target, e);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    81
        }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    82
    }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    83
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    84
    protected OutputAnalyzer executeImpl(String cmd) throws CommandExecutorException {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    85
        String stdout = "";
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    86
        String stderr = "";
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    87
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    88
        String[] cmdParts = cmd.split(" ", 2);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    89
        String operation = commandToMethodName(cmdParts[0]);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    90
        Object[] dcmdArgs = produceArguments(cmdParts);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    91
        String[] signature = {String[].class.getName()};
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    92
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    93
        ObjectName beanName = getMBeanName();
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    94
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    95
        try {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    96
            stdout = (String) mbs.invoke(beanName, operation, dcmdArgs, signature);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    97
        }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    98
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
    99
        /* Failures on the "local" side, the one invoking the command. */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   100
        catch (ReflectionException e) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   101
            Throwable cause = e.getCause();
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   102
            if (cause instanceof NoSuchMethodException) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   103
                /* We want JMXExecutor to match the behavior of the other CommandExecutors */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   104
                String message = "Unknown diagnostic command: " + operation;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   105
                stderr = exceptionTraceAsString(new IllegalArgumentException(message, e));
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   106
            } else {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   107
                rethrowExecutorException(operation, dcmdArgs, e);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   108
            }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   109
        }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   110
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   111
        /* Failures on the "local" side, the one invoking the command. */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   112
        catch (InstanceNotFoundException | IOException e) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   113
            rethrowExecutorException(operation, dcmdArgs, e);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   114
        }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   115
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   116
        /* Failures on the remote side, the one executing the invoked command. */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   117
        catch (MBeanException e) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   118
            stdout = exceptionTraceAsString(e);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   119
        }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   120
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   121
        return new OutputAnalyzer(stdout, stderr);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   122
    }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   123
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   124
    private void rethrowExecutorException(String operation, Object[] dcmdArgs,
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   125
                                          Exception e) throws CommandExecutorException {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   126
        String message = String.format("Could not invoke: %s %s", operation,
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   127
                String.join(" ", (String[]) dcmdArgs[0]));
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   128
        throw new CommandExecutorException(message, e);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   129
    }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   130
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   131
    private ObjectName getMBeanName() throws CommandExecutorException {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   132
        String MBeanName = "com.sun.management:type=DiagnosticCommand";
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   133
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   134
        try {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   135
            return new ObjectName(MBeanName);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   136
        } catch (MalformedObjectNameException e) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   137
            String message = "MBean not found: " + MBeanName;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   138
            throw new CommandExecutorException(message, e);
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   139
        }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   140
    }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   141
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   142
    private Object[] produceArguments(String[] cmdParts) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   143
        Object[] dcmdArgs = {new String[0]}; /* Default: No arguments */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   144
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   145
        if (cmdParts.length == 2) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   146
            dcmdArgs[0] = cmdParts[1].split(" ");
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   147
        }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   148
        return dcmdArgs;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   149
    }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   150
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   151
    /**
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   152
     * Convert from diagnostic command to MBean method name
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   153
     *
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   154
     * Examples:
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   155
     * help            --> help
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   156
     * VM.version      --> vmVersion
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   157
     * VM.command_line --> vmCommandLine
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   158
     */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   159
    private static String commandToMethodName(String cmd) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   160
        String operation = "";
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   161
        boolean up = false; /* First letter is to be lower case */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   162
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   163
        /*
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   164
         * If a '.' or '_' is encountered it is not copied,
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   165
         * instead the next character will be converted to upper case
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   166
         */
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   167
        for (char c : cmd.toCharArray()) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   168
            if (('.' == c) || ('_' == c)) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   169
                up = true;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   170
            } else if (up) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   171
                operation = operation.concat(Character.toString(c).toUpperCase());
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   172
                up = false;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   173
            } else {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   174
                operation = operation.concat(Character.toString(c).toLowerCase());
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   175
            }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   176
        }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   177
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   178
        return operation;
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   179
    }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   180
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   181
    private static String exceptionTraceAsString(Throwable cause) {
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   182
        StringWriter sw = new StringWriter();
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   183
        cause.printStackTrace(new PrintWriter(sw));
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   184
        return sw.toString();
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   185
    }
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   186
f7820f311663 8071908: Port internal Diagnostic Command tests and test framework to jtreg
miauno
parents:
diff changeset
   187
}