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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.process.ProcessCallable;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    import java.io.IOException;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class LoggingProcessCallable implements ProcessCallable<String> {
028    
029            public LoggingProcessCallable(byte[] bytes) {
030                    this(bytes, false);
031            }
032    
033            public LoggingProcessCallable(byte[] bytes, boolean error) {
034                    _bytes = bytes;
035                    _error = error;
036            }
037    
038            @Override
039            public String call() {
040                    try {
041                            if (_error) {
042                                    System.err.write(_bytes);
043                            }
044                            else {
045                                    System.out.write(_bytes);
046                            }
047                    }
048                    catch (IOException ioe) {
049                            _log.error(
050                                    "Unable to output log message: " + new String(_bytes), ioe);
051                    }
052    
053                    return StringPool.BLANK;
054            }
055    
056            private static final long serialVersionUID = 1L;
057    
058            private static Log _log = LogFactoryUtil.getLog(
059                    LoggingProcessCallable.class);
060    
061            private final byte[] _bytes;
062            private final boolean _error;
063    
064    }