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;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.InputStreamReader;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class LoggingOutputProcessor implements OutputProcessor<Void, Void> {
029    
030            public Void processStdErr(InputStream stdErrInputStream)
031                    throws ProcessException {
032    
033                    _processOut(true, stdErrInputStream);
034    
035                    return null;
036            }
037    
038            public Void processStdOut(InputStream stdOutInputStream)
039                    throws ProcessException {
040    
041                    _processOut(false, stdOutInputStream);
042    
043                    return null;
044            }
045    
046            private void _processOut(boolean stdErr, InputStream inputStream)
047                    throws ProcessException {
048    
049                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
050                            new InputStreamReader(inputStream));
051    
052                    String line = null;
053    
054                    try {
055                            while ((line = unsyncBufferedReader.readLine()) != null) {
056                                    if (stdErr && _log.isErrorEnabled()) {
057                                            _log.error(line);
058                                    }
059                                    else if (!stdErr && _log.isInfoEnabled()) {
060                                            _log.info(line);
061                                    }
062                            }
063                    }
064                    catch (IOException ioe) {
065                            throw new ProcessException(ioe);
066                    }
067                    finally {
068                            try {
069                                    unsyncBufferedReader.close();
070                            }
071                            catch (IOException ioe) {
072                                    throw new ProcessException(ioe);
073                            }
074                    }
075            }
076    
077            private static Log _log = LogFactoryUtil.getLog(
078                    LoggingOutputProcessor.class);
079    
080    }