001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.ObjectOutputStream;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class ProcessOutputStream extends UnsyncByteArrayOutputStream {
027    
028            public ProcessOutputStream(ObjectOutputStream objectOutputStream) {
029                    this(objectOutputStream, false);
030            }
031    
032            public ProcessOutputStream(
033                    ObjectOutputStream objectOutputStream, boolean error) {
034    
035                    _objectOutputStream = objectOutputStream;
036                    _error = error;
037            }
038    
039            @Override
040            public void close() throws IOException {
041                    _objectOutputStream.close();
042            }
043    
044            @Override
045            public void flush() throws IOException {
046                    if (index > 0) {
047                            byte[] logData = toByteArray();
048    
049                            if (_logPrefix != null) {
050                                    byte[] bytes = logData;
051    
052                                    logData = new byte[_logPrefix.length + bytes.length];
053    
054                                    System.arraycopy(_logPrefix, 0, logData, 0, _logPrefix.length);
055                                    System.arraycopy(
056                                            bytes, 0, logData, _logPrefix.length, bytes.length);
057                            }
058    
059                            LoggingProcessCallable loggingProcessCallable =
060                                    new LoggingProcessCallable(logData, _error);
061    
062                            writeProcessCallable(loggingProcessCallable);
063    
064                            reset();
065                    }
066            }
067    
068            public void setLogPrefix(byte[] logPrefix) {
069                    _logPrefix = logPrefix;
070            }
071    
072            public void writeProcessCallable(ProcessCallable<?> processCallable)
073                    throws IOException {
074    
075                    synchronized (_objectOutputStream) {
076                            _objectOutputStream.writeObject(processCallable);
077    
078                            _objectOutputStream.flush();
079                    }
080            }
081    
082            private final boolean _error;
083            private byte[] _logPrefix;
084            private final ObjectOutputStream _objectOutputStream;
085    
086    }