jdk/test/sun/jvmstat/testlibrary/JavaProcess.java
author duke
Wed, 05 Jul 2017 17:14:19 +0200
changeset 5570 bfed772dd989
parent 5506 202f599c92aa
child 21652 e935e26bd5ec
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class JavaProcess {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    protected Process process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private String classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private StringBuilder classArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private StringBuilder javaOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static String java = System.getProperty("java.home")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                                 + File.separator + "bin"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                                 + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public JavaProcess(String classname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        this(classname, "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public JavaProcess(String classname, String classArgs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        this(classname, "", classArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public JavaProcess(String classname, String javaOptions, String classArgs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.classname = classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.javaOptions = new StringBuilder(javaOptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.classArgs = new StringBuilder(classArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * add java options to the java command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public void addOptions(String[] opts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (javaOptions != null && javaOptions.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            javaOptions.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        for (int i = 0; i < opts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                javaOptions.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            javaOptions.append(opts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * add arguments to the class arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void addArguments(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (classArgs != null && classArgs.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            classArgs.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        for (int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                classArgs.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            classArgs.append(args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * start the java process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void start() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        String javaCommand = java + " " + javaOptions + " "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                             + classname + " " + classArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.out.println("exec'ing: " + javaCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        process = Runtime.getRuntime().exec(javaCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * destroy the java process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            process.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public int exitValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return process.exitValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        throw new RuntimeException("exitValue called with process == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public InputStream getErrorStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return process.getErrorStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                "getErrorStream() called with process == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public InputStream getInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return process.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                "getInputStream() called with process == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public OutputStream getOutputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return process.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                "getOutputStream() called with process == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public int waitFor() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return process.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        throw new RuntimeException("waitFor() called with process == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}