jdk/test/java/rmi/testlibrary/JavaVM.java
author weijun
Fri, 20 Jun 2008 12:05:02 +0800
changeset 793 8183c2b0985f
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6716534: Krb5LoginModule has not cleaned temp info between authentication attempts Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * RMI regression test utility class that uses Runtime.exec to spawn a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * java process that will run a named java class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class JavaVM {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    // need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    protected Process vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private String classname = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private String args = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String options = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private OutputStream outputStream = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private OutputStream errorStream = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private String policyFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static void mesg(Object mesg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        System.err.println("JAVAVM: " + mesg.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** string name of the program execd by JavaVM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static String javaProgram = "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            javaProgram = TestLibrary.getProperty("java.home", "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                File.separator + "bin" + File.separator + javaProgram;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public JavaVM(String classname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.classname = classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public JavaVM(String classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                  String options, String args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.classname = classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.args = args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public JavaVM(String classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                  String options, String args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                  OutputStream out, OutputStream err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this(classname, options, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.outputStream = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.errorStream = err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void addOptions(String[] opts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        String newOpts = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        for (int i = 0 ; i < opts.length ; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            newOpts += " " + opts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        newOpts += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        options = newOpts + options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void addArguments(String[] arguments) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        String newArgs = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (int i = 0 ; i < arguments.length ; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            newArgs += " " + arguments[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        newArgs += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        args = newArgs + args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void setPolicyFile(String policyFileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.policyFileName = policyFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * This method is used for setting VM options on spawned VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * It returns the extra command line options required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * to turn on jcov code coverage analysis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    protected static String getCodeCoverageOptions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return TestLibrary.getExtraProperty("jcov.options","");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Exec the VM as specified in this object's constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void start() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (vm != null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * If specified, add option for policy file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (policyFileName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            String option = "-Djava.security.policy=" + policyFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            addOptions(new String[] { option });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        addOptions(new String[] { getCodeCoverageOptions() });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        StringTokenizer optionsTokenizer = new StringTokenizer(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        StringTokenizer argsTokenizer = new StringTokenizer(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int optionsCount = optionsTokenizer.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int argsCount = argsTokenizer.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        String javaCommand[] = new String[optionsCount + argsCount + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        javaCommand[count++] = JavaVM.javaProgram;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        while (optionsTokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            javaCommand[count++] = optionsTokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        javaCommand[count++] = classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        while (argsTokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            javaCommand[count++] = argsTokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        mesg("command = " + Arrays.asList(javaCommand).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        System.err.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        vm = Runtime.getRuntime().exec(javaCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        /* output from the execed process may optionally be captured. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        StreamPipe.plugTogether(vm.getInputStream(), this.outputStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        StreamPipe.plugTogether(vm.getErrorStream(), this.errorStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            Thread.sleep(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } catch (Exception ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        mesg("finished starting vm.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (vm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            vm.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected Process getVM() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}