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    import java.util.Iterator;
024    import java.util.LinkedList;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class RunnableUtil {
030    
031            public static void runWithSwappedSystemOut(
032                    Runnable runnable, OutputStream outputStream) {
033    
034                    SwappedOutputStream swappedOutputStream = null;
035    
036                    synchronized (RunnableUtil.class) {
037                            swappedOutputStream = new SwappedOutputStream(
038                                    outputStream, System.out, Thread.currentThread());
039    
040                            _swappedOutputStreams.push(swappedOutputStream);
041    
042                            System.setOut(new PrintStream(swappedOutputStream));
043                    }
044    
045                    try {
046                            runnable.run();
047                    }
048                    finally {
049                            synchronized (RunnableUtil.class) {
050                                    swappedOutputStream._enabled = false;
051    
052                                    if (_swappedOutputStreams.peek() == swappedOutputStream) {
053                                            _swappedOutputStreams.pop();
054    
055                                            System.setOut(swappedOutputStream._fallbackPrintStream);
056    
057                                            Iterator<SwappedOutputStream> iterator =
058                                                    _swappedOutputStreams.iterator();
059    
060                                            while (iterator.hasNext()) {
061                                                    swappedOutputStream = iterator.next();
062    
063                                                    if (swappedOutputStream._enabled) {
064                                                            break;
065                                                    }
066    
067                                                    iterator.remove();
068    
069                                                    System.setOut(swappedOutputStream._fallbackPrintStream);
070                                            }
071                                    }
072                            }
073                    }
074            }
075    
076            private static final LinkedList<SwappedOutputStream> _swappedOutputStreams =
077                    new LinkedList<>();
078    
079            private static class SwappedOutputStream extends UnsyncFilterOutputStream {
080    
081                    @Override
082                    public void write(byte[] bytes, int offset, int length)
083                            throws IOException {
084    
085                            Thread thread = Thread.currentThread();
086    
087                            if ((thread == _thread) && _enabled) {
088                                    super.write(bytes, offset, length);
089                            }
090                            else {
091                                    _fallbackPrintStream.write(bytes, offset, length);
092                            }
093                    }
094    
095                    @Override
096                    public void write(int b) throws IOException {
097                            Thread thread = Thread.currentThread();
098    
099                            if ((thread == _thread) && _enabled) {
100                                    super.write(b);
101                            }
102                            else {
103                                    _fallbackPrintStream.write(b);
104                            }
105                    }
106    
107                    private SwappedOutputStream(
108                            OutputStream outputStream, PrintStream fallbackPrintStream,
109                            Thread thread) {
110    
111                            super(outputStream);
112    
113                            _fallbackPrintStream = fallbackPrintStream;
114                            _thread = thread;
115                    }
116    
117                    private volatile boolean _enabled = true;
118                    private final PrintStream _fallbackPrintStream;
119                    private final Thread _thread;
120    
121            }
122    
123    }