001    /**
002     * Copyright (c) 2000-2013 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.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[] bytes = toByteArray();
048    
049                            byte[] logData = new byte[_logPrefix.length + bytes.length];
050    
051                            System.arraycopy(_logPrefix, 0, logData, 0, _logPrefix.length);
052                            System.arraycopy(
053                                    bytes, 0, logData, _logPrefix.length, bytes.length);
054    
055                            LoggingProcessCallable loggingProcessCallable =
056                                    new LoggingProcessCallable(logData, _error);
057    
058                            writeProcessCallable(loggingProcessCallable);
059    
060                            reset();
061                    }
062            }
063    
064            public void setLogPrefix(byte[] logPrefix) {
065                    _logPrefix = logPrefix;
066            }
067    
068            public void writeProcessCallable(ProcessCallable<?> processCallable)
069                    throws IOException {
070    
071                    synchronized (_objectOutputStream) {
072                            _objectOutputStream.writeObject(processCallable);
073    
074                            _objectOutputStream.flush();
075                    }
076            }
077    
078            private final boolean _error;
079            private byte[] _logPrefix;
080            private final ObjectOutputStream _objectOutputStream;
081    
082    }