jdk/test/java/rmi/testlibrary/StreamPipe.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 13256 5886d7607acd
child 14919 cd751b98ef69
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 12692
diff changeset
     2
 * Copyright (c) 1998, 2012, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Pipe output of one stream into input of another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class StreamPipe extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private String preamble;
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    38
    private JavaVM javaVM;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    42
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    43
    /* StreamPipe constructor : should only be called by plugTogether() method !!
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    44
     * If passed vm is not null :
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    45
     * -  This is StreamPipe usage when streams to pipe come from a given
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    46
     *    vm (JavaVM) process (the vm process must be started with a prefixed
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    47
     *    "-showversion" option to be able to determine as soon as possible when
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    48
     *    the vm process is started through the redirection of the streams).
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    49
     *    There must be a close connection between the StreamPipe instance and
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    50
     *    the JavaVM object on which a start() call has been done.
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    51
     *    run() method will flag distant JavaVM as started.
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    52
     * If passed vm is null :
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    53
     * -  We don't have control on the process which we want to redirect the passed
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    54
     *    streams.
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    55
     *    run() method will ignore distant process.
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    56
     */
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    57
    private StreamPipe(JavaVM vm, InputStream in, OutputStream out, String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.in  = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.preamble = "# ";
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    62
        this.javaVM = vm;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    65
    // Install redirection of passed InputStream and OutputStream from passed JavaVM
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    66
    // to this vm standard output and input streams.
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    67
    public static void plugTogether(JavaVM vm, InputStream in, OutputStream out) {
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    68
        String name = null;
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    69
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    70
        synchronized (lock) {
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    71
            name = "TestLibrary: StreamPipe-" + (count ++ );
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    72
        }
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    73
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    74
        Thread pipe = new StreamPipe(vm, in, out, name);
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    75
        pipe.setDaemon(true);
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    76
        pipe.start();
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    77
    }
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    78
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    79
    /* Redirects the InputStream and OutputStream passed by caller to this
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    80
     * vm standard output and input streams.
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    81
     * (we just have to use fully parametered plugTogether() call with a null
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    82
     *  JavaVM input to do this).
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    83
     */
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    84
    public static void plugTogether(InputStream in, OutputStream out) {
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    85
        plugTogether(null, in, out);
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    86
    }
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    87
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    88
    // Starts redirection of streams.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        BufferedReader r = new BufferedReader(new InputStreamReader(in), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(out));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        byte[] buf = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            String line;
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    96
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    97
            /* This is to check that the distant vm has started,
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    98
             * if such a vm has been provided at construction :
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
    99
             * - As soon as we can read something from r BufferedReader,
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   100
             *   that means the distant vm is already started.
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   101
             * Thus we signal associated JavaVM object that it is now started.
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   102
             */
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   103
            if (((line = r.readLine()) != null) &&
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   104
                (javaVM != null)) {
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   105
                javaVM.setStarted();
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   106
            }
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   107
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   108
            // Redirects r on w.
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   109
            while (line != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                w.write(preamble);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                w.write(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                w.newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                w.flush();
12692
72f0847dd477 7144861: speed up RMI activation tests
olagneau
parents: 5506
diff changeset
   114
                line = r.readLine();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
13256
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 12692
diff changeset
   116
        } catch (InterruptedIOException iioe) {
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 12692
diff changeset
   117
            // Thread interrupted during IO operation. Terminate StreamPipe.
5886d7607acd 7142596: RMI JPRT tests are failing
dmocek
parents: 12692
diff changeset
   118
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.err.println("*** IOException in StreamPipe.run:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}