001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncFilterOutputStream;
018    
019    import java.io.IOException;
020    import java.io.OutputStream;
021    import java.io.PrintStream;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class RunnableUtil {
027    
028            public static void runWithSwappedSystemOut(
029                    Runnable runnable, OutputStream outputStream) {
030    
031                    PrintStream printStream = null;
032    
033                    synchronized (RunnableUtil.class) {
034                            printStream = System.out;
035    
036                            System.setOut(
037                                    new PrintStream(
038                                            new SwappedOutputStream(
039                                                    outputStream, printStream, Thread.currentThread())));
040                    }
041    
042                    try {
043                            runnable.run();
044                    }
045                    finally {
046                            synchronized (RunnableUtil.class) {
047                                    System.setOut(printStream);
048                            }
049                    }
050            }
051    
052            private static class SwappedOutputStream extends UnsyncFilterOutputStream {
053    
054                    @Override
055                    public void write(byte[] bytes, int offset, int length)
056                            throws IOException {
057    
058                            Thread thread = Thread.currentThread();
059    
060                            if (thread == _thread) {
061                                    super.write(bytes, offset, length);
062                            }
063                            else {
064                                    _fallbackOutputStream.write(bytes, offset, length);
065                            }
066                    }
067    
068                    @Override
069                    public void write(int b) throws IOException {
070                            Thread thread = Thread.currentThread();
071    
072                            if (thread == _thread) {
073                                    super.write(b);
074                            }
075                            else {
076                                    _fallbackOutputStream.write(b);
077                            }
078                    }
079    
080                    private SwappedOutputStream(
081                            OutputStream outputStream, OutputStream fallbackOutputStream,
082                            Thread thread) {
083    
084                            super(outputStream);
085    
086                            _fallbackOutputStream = fallbackOutputStream;
087                            _thread = thread;
088                    }
089    
090                    private final OutputStream _fallbackOutputStream;
091                    private final Thread _thread;
092    
093            }
094    
095    }