Example usage for java.lang System setOut

List of usage examples for java.lang System setOut

Introduction

In this page you can find the example usage for java.lang System setOut.

Prototype

public static void setOut(PrintStream out) 

Source Link

Document

Reassigns the "standard" output stream.

Usage

From source file:io.dockstore.client.cli.nested.AbstractEntryClient.java

private void launchWdl(String entry, final List<String> args) {
    boolean isLocalEntry = false;
    if (args.contains("--local-entry")) {
        isLocalEntry = true;/*from   www . ja  v a2s  .c  o  m*/
    }
    final String json = reqVal(args, "--json");

    Main main = new Main();
    File parameterFile = new File(json);

    final SourceFile wdlFromServer;
    try {
        // Grab WDL from server and store to file
        final File tempDir = Files.createTempDir();
        File tmp;
        if (!isLocalEntry) {
            wdlFromServer = getDescriptorFromServer(entry, "wdl");
            File tempDescriptor = File.createTempFile("temp", ".wdl", tempDir);
            Files.write(wdlFromServer.getContent(), tempDescriptor, StandardCharsets.UTF_8);
            downloadDescriptors(entry, "wdl", tempDir);

            tmp = resolveImportsForDescriptor(tempDir, tempDescriptor);
        } else {
            tmp = new File(entry);
        }

        // Get list of input files
        Bridge bridge = new Bridge();
        Map<String, String> wdlInputs = bridge.getInputFiles(tmp);

        // Convert parameter JSON to a map
        WDLFileProvisioning wdlFileProvisioning = new WDLFileProvisioning(this.getConfigFile());
        Gson gson = new Gson();
        String jsonString = FileUtils.readFileToString(parameterFile, StandardCharsets.UTF_8);
        Map<String, Object> inputJson = gson.fromJson(jsonString, HashMap.class);

        // Download files and change to local location
        // Make a new map of the inputs with updated locations
        final String workingDir = Paths.get(".").toAbsolutePath().normalize().toString();
        System.out.println("Creating directories for run of Dockstore launcher in current working directory: "
                + workingDir);
        Map<String, Object> fileMap = wdlFileProvisioning.pullFiles(inputJson, wdlInputs);

        // Make new json file
        String newJsonPath = wdlFileProvisioning.createUpdatedInputsJson(inputJson, fileMap);

        final List<String> wdlRun = Lists.newArrayList(tmp.getAbsolutePath(), newJsonPath);
        final scala.collection.immutable.List<String> wdlRunList = scala.collection.JavaConversions
                .asScalaBuffer(wdlRun).toList();

        // run a workflow
        System.out.println("Calling out to Cromwell to run your workflow");

        // save the output stream
        PrintStream savedOut = System.out;
        PrintStream savedErr = System.err;

        // capture system.out and system.err
        ByteArrayOutputStream stdoutCapture = new ByteArrayOutputStream();
        System.setOut(new PrintStream(stdoutCapture, true, StandardCharsets.UTF_8.toString()));
        ByteArrayOutputStream stderrCapture = new ByteArrayOutputStream();
        System.setErr(new PrintStream(stderrCapture, true, StandardCharsets.UTF_8.toString()));

        final int run = main.run(wdlRunList);

        System.out.flush();
        System.err.flush();
        String stdout = stdoutCapture.toString(StandardCharsets.UTF_8);
        String stderr = stderrCapture.toString(StandardCharsets.UTF_8);

        System.setOut(savedOut);
        System.setErr(savedErr);
        System.out.println("Cromwell exit code: " + run);

        LauncherCWL.outputIntegrationOutput(workingDir, ImmutablePair.of(stdout, stderr),
                stdout.replaceAll("\n", "\t"), stderr.replaceAll("\n", "\t"), "Cromwell");

        // capture the output and provision it
        final String wdlOutputTarget = optVal(args, "--wdl-output-target", null);
        if (wdlOutputTarget != null) {
            // grab values from output JSON
            Map<String, String> outputJson = gson.fromJson(stdout, HashMap.class);
            System.out.println("Provisioning your output files to their final destinations");
            final List<String> outputFiles = bridge.getOutputFiles(tmp);
            for (String outFile : outputFiles) {
                // find file path from output
                final File resultFile = new File(outputJson.get(outFile));
                FileProvisioning.FileInfo new1 = new FileProvisioning.FileInfo();

                new1.setUrl(wdlOutputTarget + "/" + resultFile.getParentFile().getName() + "/"
                        + resultFile.getName());
                new1.setLocalPath(resultFile.getAbsolutePath());
                System.out.println("Uploading: " + outFile + " from " + resultFile + " to : " + new1.getUrl());
                FileProvisioning fileProvisioning = new FileProvisioning(this.getConfigFile());
                fileProvisioning.provisionOutputFile(new1, resultFile.getAbsolutePath());
            }
        } else {
            System.out.println("Output files left in place");
        }
    } catch (ApiException ex) {
        exceptionMessage(ex, "", API_ERROR);
    } catch (IOException ex) {
        exceptionMessage(ex, "", IO_ERROR);
    }
}

From source file:org.kchine.rpf.PoolUtils.java

public static void redirectIO() {
    final JTextArea area = new JTextArea();
    JFrame f = new JFrame("out/err");
    f.add(new JScrollPane(area), BorderLayout.CENTER);
    f.pack();//from   w w  w .  j av  a 2 s.co m
    f.setVisible(true);
    f.setSize(500, 500);
    f.setLocation(100, 100);

    PrintStream ps = new PrintStream(new OutputStream() {
        public void write(final int b) throws IOException {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setText(area.getText() + new String(new byte[] { (byte) b }));
                }
            });
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setCaretPosition(area.getText().length());
                    area.repaint();
                }
            });
        }

        public void write(final byte[] b) throws IOException {

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setText(area.getText() + new String(b));
                }
            });
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setCaretPosition(area.getText().length());
                    area.repaint();
                }
            });
        }

        public void write(byte[] b, int off, int len) throws IOException {
            final byte[] r = new byte[len];
            for (int i = 0; i < len; ++i)
                r[i] = b[off + i];

            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setText(area.getText() + new String(r));
                }
            });
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    area.setCaretPosition(area.getText().length());
                    area.repaint();
                }
            });
        }
    });
    System.setOut(ps);
    System.setErr(ps);
}

From source file:com.ibm.bi.dml.test.integration.AutomatedTestBase.java

/**
 * Enables expection of a line in standard output stream.
 * /*from   www.  j  a  v  a  2  s.  c o  m*/
 * @param expected
 */
public void setExpectedStdOut(String expectedLine) {
    this.expectedStdOut = expectedLine;
    originalPrintStreamStd = System.out;
    iExpectedStdOutState = 1;
    System.setOut(new PrintStream(new ExpectedOutputStream()));
}

From source file:com.adito.agent.client.Agent.java

protected static Agent initAgent(AgentArgs agentArgs) throws Throwable {
    Agent agent = new Agent(agentArgs.getAgentConfiguration());

    // Setup the output stream
    PrintStream consolePrintStream = new PrintStream(agent.getGUI().getConsole());
    System.setErr(consolePrintStream);
    System.setOut(consolePrintStream);

    System.out.println("Java version " + System.getProperty("java.version"));
    System.out.println("OS version " + System.getProperty("os.name"));

    // #ifdef DEBUG
    if (agentArgs.getLogProperties() != null) {
        File f = new File(agentArgs.getLogProperties());
        InputStream in = new FileInputStream(f);
        try {//w  w w.j  ava2 s.com
            Properties props = new Properties();
            props.load(in);
            File logfile = new File(f.getParent(), "agent.log"); //$NON-NLS-1$
            props.put("log4j.appender.logfile.File", logfile.getAbsolutePath()); //$NON-NLS-1$
            org.apache.log4j.PropertyConfigurator.configure(props);
            log = org.apache.commons.logging.LogFactory.getLog(Agent.class);
            log.info("Configured logging"); //$NON-NLS-1$
        } finally {
            in.close();
        }
    }

    Properties systemProperties = System.getProperties();
    String key;
    log.info("System properties:");
    for (Enumeration e = systemProperties.keys(); e.hasMoreElements();) {
        key = (String) e.nextElement();
        log.info("   " + key + ": " + systemProperties.getProperty(key));
    }
    // #endif

    agent.setupProxy(agentArgs.getLocalProxyURL(), agentArgs.getUserAgent(), agentArgs.getPluginProxyURL(),
            agentArgs.getReverseProxyURL());

    if (agentArgs.getBrowserCommand() != null && !agentArgs.getBrowserCommand().equals("")) { //$NON-NLS-1$

        // #ifdef DEBUG
        log.info("Setting browser to " + agentArgs.getBrowserCommand()); //$NON-NLS-1$
        // #endif
        BrowserLauncher.setBrowserCommand(agentArgs.getBrowserCommand());
    }

    return agent;
}

From source file:org.apache.hadoop.hdfs.TestDFSShell.java

private static String runLsr(final FsShell shell, String root, int returnvalue) throws Exception {
    System.out.println("root=" + root + ", returnvalue=" + returnvalue);
    final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    final PrintStream out = new PrintStream(bytes);
    final PrintStream oldOut = System.out;
    final PrintStream oldErr = System.err;
    System.setOut(out);
    System.setErr(out);//www.j  a v  a 2  s .com
    final String results;
    try {
        assertEquals(returnvalue, shell.run(new String[] { "-lsr", root }));
        results = bytes.toString();
    } finally {
        IOUtils.closeStream(out);
        System.setOut(oldOut);
        System.setErr(oldErr);
    }
    System.out.println("results:\n" + results);
    return results;
}

From source file:homenetapp.HomeNetAppGui.java

private void redirectSystemStreams() {

    javax.swing.text.Style style = consoleTextPane.addStyle("Red", null);
    javax.swing.text.StyleConstants.setForeground(style, java.awt.Color.red);
    final javax.swing.text.Style redStyle = style;

    style = consoleTextPane.addStyle("White", null);
    javax.swing.text.StyleConstants.setForeground(style, java.awt.Color.white);
    final javax.swing.text.Style whiteStyle = style;

    OutputStream out = new OutputStream() {

        @Override//from  w ww  . j  a  va2 s .c  o  m
        public void write(final int b) throws IOException {
            updateTextPane(String.valueOf((char) b), whiteStyle);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            updateTextPane(new String(b, off, len), whiteStyle);
        }

        @Override
        public void write(byte[] b) throws IOException {
            write(b, 0, b.length);
        }
    };

    OutputStream err = new OutputStream() {

        @Override
        public void write(final int b) throws IOException {
            updateTextPane(String.valueOf((char) b), redStyle);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            updateTextPane(new String(b, off, len), redStyle);
        }

        @Override
        public void write(byte[] b) throws IOException {
            write(b, 0, b.length);
        }
    };

    System.setOut(new PrintStream(out, true));
    System.setErr(new PrintStream(err, true));
}

From source file:com.netscape.admin.certsrv.Console.java

public static void tee(String f) throws IOException {

    // Create/Open logfile.
    logfile = new PrintStream(new BufferedOutputStream(new FileOutputStream(f)), /*autoFlush=*/true);

    // Start redirecting the output.
    System.setOut(new TeeStream(System.out));
    System.setErr(new TeeStream(System.err));
}

From source file:org.apache.hadoop.hdfs.TestDFSShell.java

@Test(timeout = 30000)
public void testSetXAttrCaseSensitivity() throws Exception {
    UserGroupInformation user = UserGroupInformation.createUserForTesting("user", new String[] { "mygroup" });
    MiniDFSCluster cluster = null;//from  w ww  .  ja  v  a2s.  co m
    PrintStream bak = null;
    try {
        final Configuration conf = new HdfsConfiguration();
        cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
        cluster.waitActive();

        FileSystem fs = cluster.getFileSystem();
        Path p = new Path("/mydir");
        fs.mkdirs(p);
        bak = System.err;

        final FsShell fshell = new FsShell(conf);
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));

        doSetXattr(out, fshell, new String[] { "-setfattr", "-n", "User.Foo", "/mydir" },
                new String[] { "-getfattr", "-d", "/mydir" }, new String[] { "user.Foo" }, new String[] {});

        doSetXattr(out, fshell, new String[] { "-setfattr", "-n", "user.FOO", "/mydir" },
                new String[] { "-getfattr", "-d", "/mydir" }, new String[] { "user.Foo", "user.FOO" },
                new String[] {});

        doSetXattr(out, fshell, new String[] { "-setfattr", "-n", "USER.foo", "/mydir" },
                new String[] { "-getfattr", "-d", "/mydir" },
                new String[] { "user.Foo", "user.FOO", "user.foo" }, new String[] {});

        doSetXattr(out, fshell, new String[] { "-setfattr", "-n", "USER.fOo", "-v", "myval", "/mydir" },
                new String[] { "-getfattr", "-d", "/mydir" },
                new String[] { "user.Foo", "user.FOO", "user.foo", "user.fOo=\"myval\"" },
                new String[] { "user.Foo=", "user.FOO=", "user.foo=" });

        doSetXattr(out, fshell, new String[] { "-setfattr", "-x", "useR.foo", "/mydir" },
                new String[] { "-getfattr", "-d", "/mydir" }, new String[] { "user.Foo", "user.FOO" },
                new String[] { "foo" });

        doSetXattr(out, fshell, new String[] { "-setfattr", "-x", "USER.FOO", "/mydir" },
                new String[] { "-getfattr", "-d", "/mydir" }, new String[] { "user.Foo" },
                new String[] { "FOO" });

        doSetXattr(out, fshell, new String[] { "-setfattr", "-x", "useR.Foo", "/mydir" },
                new String[] { "-getfattr", "-n", "User.Foo", "/mydir" }, new String[] {},
                new String[] { "Foo" });

    } finally {
        if (bak != null) {
            System.setOut(bak);
        }
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}