jdk/test/java/security/testlibrary/Proc.java
author jjg
Mon, 25 Nov 2013 17:42:28 -0800
changeset 21894 3535c1819067
parent 18536 092411ced388
child 36511 9d0388c6b336
permissions -rw-r--r--
8028318: [doclint] doclint will reject existing user-written doc comments using custom tags that follow the recommended rules Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18536
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
     1
/*
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
     4
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
     8
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    13
 * accompanied this code).
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    14
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    18
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    21
 * questions.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    22
 */
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    23
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    24
import java.io.BufferedReader;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    25
import java.io.File;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    26
import java.io.IOException;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    27
import java.io.InputStreamReader;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    28
import java.net.URL;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    29
import java.net.URLClassLoader;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    30
import java.nio.file.Files;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    31
import java.nio.file.Path;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    32
import java.nio.file.Paths;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    33
import java.security.Permission;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    34
import java.util.ArrayList;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    35
import java.util.Base64;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    36
import java.util.HashMap;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    37
import java.util.List;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    38
import java.util.Map;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    39
import java.util.Map.Entry;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    40
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    41
/**
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    42
 * This is a test library that makes writing a Java test that spawns multiple
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    43
 * Java processes easily.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    44
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    45
 * Usage:
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    46
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    47
 *    Proc.create("Clazz")      // The class to launch
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    48
 *        .args("x")            // with args
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    49
 *        .env("env", "value")  // and an environment variable
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    50
 *        .prop("key","value")  // and a system property
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    51
 *        .perm(perm)           // with granted permissions
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    52
 *        .start();             // and start
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    53
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    54
 * create/start must be called, args/env/prop/perm can be called zero or
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    55
 * multiple times between create and start.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    56
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    57
 * The controller can call inheritIO to share its I/O to the process.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    58
 * Otherwise, it can send data into a proc's stdin with write/println, and
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    59
 * read its stdout with readLine. stderr is always redirected to DFILE
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    60
 * unless nodump() is called. A protocol is designed to make
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    61
 * data exchange among the controller and the processes super easy, in which
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    62
 * useful data are always printed with a special prefix ("PROCISFUN:").
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    63
 * If the data is binary, make it BASE64.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    64
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    65
 * For example:
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    66
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    67
 * - A producer Proc calls Proc.binOut() or Proc.textOut() to send out data.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    68
 *   This method would prints to the stdout something like
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    69
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    70
 *      PROCISFUN:[raw text or base64 binary]
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    71
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    72
 * - The controller calls producer.readData() to get the content. This method
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    73
 *   ignores all other output and only reads lines starting with "PROCISFUN:".
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    74
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    75
 * - The controller does not care if the context is text or base64, it simply
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    76
 *   feeds the data to a consumer Proc by calling consumer.println(data).
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    77
 *   This will be printed into System.in of the consumer process.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    78
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    79
 * - The consumer Proc calls Proc.binIn() or Proc.textIn() to read the data.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    80
 *   The first method de-base64 the input and return a byte[] block.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    81
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    82
 * Please note only plain ASCII is supported in raw text at the moment.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    83
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    84
 * As the Proc objects are hidden so deeply, two static methods, d(String) and
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    85
 * d(Throwable) are provided to output info into stderr, where they will
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    86
 * normally be appended messages to DFILE (unless nodump() is called).
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    87
 * Developers can view the messages in real time by calling
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    88
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    89
 *    tail -f proc.debug
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    90
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    91
 * TODO:
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    92
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    93
 * . launch java tools, say, keytool
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    94
 * . launch another version of java
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    95
 * . start in another directory
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    96
 * . start and finish using one method
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    97
 *
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    98
 * This is not a test, but is the core of
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
    99
 * JDK-8009977: A test library to launch multiple Java processes
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   100
 */
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   101
public class Proc {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   102
    private Process p;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   103
    private BufferedReader br;      // the stdout of a process
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   104
    private String launcher;        // Optional: the java program
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   105
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   106
    private List<Permission> perms = new ArrayList<>();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   107
    private List<String> args = new ArrayList<>();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   108
    private Map<String,String> env = new HashMap<>();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   109
    private Map<String,String> prop = new HashMap();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   110
    private boolean inheritIO = false;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   111
    private boolean noDump = false;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   112
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   113
    private String clazz;           // Class to launch
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   114
    private String debug;           // debug flag, controller will show data
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   115
                                    // transfer between procs
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   116
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   117
    final private static String PREFIX = "PROCISFUN:";
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   118
    final private static String DFILE = "proc.debug";
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   119
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   120
    // The following methods are called by controllers
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   121
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   122
    // Creates a Proc by the Java class name, launcher is an optional
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   123
    // argument to specify the java program
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   124
    public static Proc create(String clazz, String... launcher) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   125
        Proc pc = new Proc();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   126
        pc.clazz = clazz;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   127
        if (launcher.length > 0) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   128
            pc.launcher = launcher[0];
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   129
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   130
        return pc;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   131
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   132
    // Sets inheritIO flag to proc. If set, proc will same I/O channels as
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   133
    // teh controller. Otherwise, its stdin/stdout is untouched, and its
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   134
    // stderr is redirected to DFILE.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   135
    public Proc inheritIO() {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   136
        inheritIO = true;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   137
        return this;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   138
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   139
    // When called, stderr inherits parent stderr, otherwise, append to a file
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   140
    public Proc nodump() {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   141
        noDump = true;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   142
        return this;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   143
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   144
    // Specifies some args. Can be called multiple times.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   145
    public Proc args(String... args) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   146
        for (String c: args) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   147
            this.args.add(c);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   148
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   149
        return this;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   150
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   151
    // Returns debug prefix
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   152
    public String debug() {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   153
        return debug;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   154
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   155
    // Enables debug with prefix
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   156
    public Proc debug(String title) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   157
        debug = title;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   158
        return this;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   159
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   160
    // Specifies an env var. Can be called multiple times.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   161
    public Proc env(String a, String b) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   162
        env.put(a, b);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   163
        return this;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   164
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   165
    // Specifies a Java system property. Can be called multiple times.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   166
    public Proc prop(String a, String b) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   167
        prop.put(a, b);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   168
        return this;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   169
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   170
    // Adds a perm to policy. Can be called multiple times. In order to make it
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   171
    // effective, please also call prop("java.security.manager", "").
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   172
    public Proc perm(Permission p) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   173
        perms.add(p);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   174
        return this;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   175
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   176
    // Starts the proc
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   177
    public Proc start() throws IOException {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   178
        List<String> cmd = new ArrayList<>();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   179
        if (launcher != null) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   180
            cmd.add(launcher);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   181
        } else {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   182
            cmd.add(new File(new File(System.getProperty("java.home"), "bin"),
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   183
                        "java").getPath());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   184
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   185
        cmd.add("-cp");
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   186
        StringBuilder cp = new StringBuilder();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   187
        for (URL url: ((URLClassLoader)Proc.class.getClassLoader()).getURLs()) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   188
            if (cp.length() != 0) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   189
                cp.append(File.pathSeparatorChar);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   190
            }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   191
            cp.append(url.getFile());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   192
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   193
        cmd.add(cp.toString());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   194
        for (Entry<String,String> e: prop.entrySet()) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   195
            cmd.add("-D" + e.getKey() + "=" + e.getValue());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   196
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   197
        if (!perms.isEmpty()) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   198
            Path p = Files.createTempFile(
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   199
                    Paths.get(".").toAbsolutePath(), "policy", null);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   200
            StringBuilder sb = new StringBuilder();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   201
            sb.append("grant {\n");
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   202
            for (Permission perm: perms) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   203
                // Sometimes a permission has no name or actions.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   204
                // but it's safe to use an empty string.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   205
                String s = String.format("%s \"%s\", \"%s\"",
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   206
                        perm.getClass().getCanonicalName(),
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   207
                        perm.getName()
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   208
                                .replace("\\", "\\\\").replace("\"", "\\\""),
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   209
                        perm.getActions());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   210
                sb.append("    permission ").append(s).append(";\n");
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   211
            }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   212
            sb.append("};\n");
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   213
            Files.write(p, sb.toString().getBytes());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   214
            cmd.add("-Djava.security.policy=" + p.toString());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   215
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   216
        cmd.add(clazz);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   217
        for (String s: args) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   218
            cmd.add(s);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   219
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   220
        if (debug != null) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   221
            System.out.println("PROC: " + debug + " cmdline: " + cmd);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   222
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   223
        ProcessBuilder pb = new ProcessBuilder(cmd);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   224
        for (Entry<String,String> e: env.entrySet()) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   225
            pb.environment().put(e.getKey(), e.getValue());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   226
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   227
        if (inheritIO) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   228
            pb.inheritIO();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   229
        } else if (noDump) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   230
            pb.redirectError(ProcessBuilder.Redirect.INHERIT);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   231
        } else {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   232
            pb.redirectError(ProcessBuilder.Redirect.appendTo(new File(DFILE)));
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   233
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   234
        p = pb.start();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   235
        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   236
        return this;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   237
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   238
    // Reads a line from stdout of proc
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   239
    public String readLine() throws IOException {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   240
        String s = br.readLine();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   241
        if (debug != null) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   242
            System.out.println("PROC: " + debug + " readline: " +
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   243
                    (s == null ? "<EOF>" : s));
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   244
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   245
        return s;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   246
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   247
    // Reads a special line from stdout of proc
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   248
    public String readData() throws Exception {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   249
        while (true) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   250
            String s = readLine();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   251
            if (s == null) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   252
                if (p.waitFor() != 0) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   253
                    throw new Exception("Proc abnormal end");
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   254
                } else {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   255
                    return s;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   256
                }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   257
            }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   258
            if (s.startsWith(PREFIX)) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   259
                return s.substring(PREFIX.length());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   260
            }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   261
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   262
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   263
    // Writes text into stdin of proc
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   264
    public void println(String s) throws IOException {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   265
        if (debug != null) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   266
            System.out.println("PROC: " + debug + " println: " + s);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   267
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   268
        write((s + "\n").getBytes());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   269
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   270
    // Writes data into stdin of proc
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   271
    public void write(byte[] b) throws IOException {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   272
        p.getOutputStream().write(b);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   273
        p.getOutputStream().flush();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   274
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   275
    // Reads all output and wait for process end
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   276
    public int waitFor() throws Exception {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   277
        while (true) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   278
            String s = readLine();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   279
            if (s == null) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   280
                break;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   281
            }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   282
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   283
        return p.waitFor();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   284
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   285
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   286
    // The following methods are used inside a proc
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   287
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   288
    // Writes out a BASE64 binary with a prefix
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   289
    public static void binOut(byte[] data) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   290
        System.out.println(PREFIX + Base64.getEncoder().encodeToString(data));
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   291
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   292
    // Reads in a line of BASE64 binary
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   293
    public static byte[] binIn() throws Exception {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   294
        return Base64.getDecoder().decode(textIn());
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   295
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   296
    // Writes out a text with a prefix
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   297
    public static void textOut(String data) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   298
        System.out.println(PREFIX + data);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   299
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   300
    // Reads in a line of text
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   301
    public static String textIn() throws Exception {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   302
        StringBuilder sb = new StringBuilder();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   303
        boolean isEmpty = true;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   304
        while (true) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   305
            int i = System.in.read();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   306
            if (i == -1) break;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   307
            isEmpty = false;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   308
            if (i == '\n') break;
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   309
            if (i != 13) {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   310
                // Force it to a char, so only simple ASCII works.
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   311
                sb.append((char)i);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   312
            }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   313
        }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   314
        return isEmpty ? null : sb.toString();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   315
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   316
    // Sends string to stderr. If inheritIO is not called, they will
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   317
    // be collected into DFILE
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   318
    public static void d(String s) throws IOException {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   319
        System.err.println(s);
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   320
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   321
    // Sends an exception to stderr
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   322
    public static void d(Throwable e) throws IOException {
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   323
        e.printStackTrace();
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   324
    }
092411ced388 8001326: Improve Kerberos caching
weijun
parents:
diff changeset
   325
}