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.process.log;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.process.ProcessCallable;
019    
020    import java.io.IOException;
021    import java.io.NotSerializableException;
022    import java.io.ObjectOutputStream;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class ProcessOutputStream extends UnsyncByteArrayOutputStream {
028    
029            public ProcessOutputStream(ObjectOutputStream objectOutputStream) {
030                    this(objectOutputStream, false);
031            }
032    
033            public ProcessOutputStream(
034                    ObjectOutputStream objectOutputStream, boolean error) {
035    
036                    _objectOutputStream = objectOutputStream;
037                    _error = error;
038            }
039    
040            @Override
041            public void close() throws IOException {
042                    _objectOutputStream.close();
043            }
044    
045            @Override
046            public void flush() throws IOException {
047                    synchronized (System.out) {
048                            if (index > 0) {
049                                    byte[] bytes = toByteArray();
050    
051                                    reset();
052    
053                                    byte[] logData = new byte[_logPrefix.length + bytes.length];
054    
055                                    System.arraycopy(_logPrefix, 0, logData, 0, _logPrefix.length);
056                                    System.arraycopy(
057                                            bytes, 0, logData, _logPrefix.length, bytes.length);
058    
059                                    _objectOutputStream.writeObject(
060                                            new LoggingProcessCallable(logData, _error));
061                            }
062    
063                            _objectOutputStream.flush();
064    
065                            _objectOutputStream.reset();
066                    }
067            }
068    
069            public void setLogPrefix(byte[] logPrefix) {
070                    _logPrefix = logPrefix;
071            }
072    
073            public void writeProcessCallable(ProcessCallable<?> processCallable)
074                    throws IOException {
075    
076                    synchronized (System.out) {
077                            try {
078                                    _objectOutputStream.writeObject(processCallable);
079                            }
080                            catch (NotSerializableException nse) {
081                                    _objectOutputStream.reset();
082    
083                                    throw nse;
084                            }
085                            finally {
086                                    _objectOutputStream.flush();
087                            }
088                    }
089            }
090    
091            private final boolean _error;
092            private byte[] _logPrefix;
093            private final ObjectOutputStream _objectOutputStream;
094    
095    }