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            @Override
031            public Void processStdErr(InputStream stdErrInputStream)
032                    throws ProcessException {
033    
034                    _processOut(true, stdErrInputStream);
035    
036                    return null;
037            }
038    
039            @Override
040            public Void processStdOut(InputStream stdOutInputStream)
041                    throws ProcessException {
042    
043                    _processOut(false, stdOutInputStream);
044    
045                    return null;
046            }
047    
048            private void _processOut(boolean stdErr, InputStream inputStream)
049                    throws ProcessException {
050    
051                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
052                            new InputStreamReader(inputStream));
053    
054                    String line = null;
055    
056                    try {
057                            while ((line = unsyncBufferedReader.readLine()) != null) {
058                                    if (stdErr && _log.isErrorEnabled()) {
059                                            _log.error(line);
060                                    }
061                                    else if (!stdErr && _log.isInfoEnabled()) {
062                                            _log.info(line);
063                                    }
064                            }
065                    }
066                    catch (IOException ioe) {
067                            throw new ProcessException(ioe);
068                    }
069                    finally {
070                            try {
071                                    unsyncBufferedReader.close();
072                            }
073                            catch (IOException ioe) {
074                                    throw new ProcessException(ioe);
075                            }
076                    }
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(
080                    LoggingOutputProcessor.class);
081    
082    }