001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.util;
016    
017    import org.apache.oozie.util.XLogStreamer;
018    
019    import java.util.ArrayList;
020    import java.io.Writer;
021    import java.io.IOException;
022    import java.io.BufferedReader;
023    import java.io.InputStreamReader;
024    import java.io.InputStream;
025    
026    /**
027     * Reads the input stream(log file) and applies the filters and writes it to output stream. The filtering will also
028     * consider the log messages spilling over multiline.
029     */
030    public class XLogReader {
031        private BufferedReader logReader;
032        private Writer logWriter;
033        private boolean noFilter = false;
034        private XLogStreamer.Filter logFilter;
035    
036        public XLogReader(InputStream logFileIS, XLogStreamer.Filter filter, Writer logWriter) {
037            logReader = new BufferedReader(new InputStreamReader(logFileIS));
038            logFilter = filter;
039            this.logWriter = logWriter;
040        }
041    
042        /**
043         * Processes the Given Log and writes the output after applying the filters.
044         *
045         * @throws IOException
046         */
047        public void processLog() throws IOException {
048            String line = logReader.readLine();
049            boolean patternMatched = false;
050            int lcnt = 0;
051            if (logFilter == null || !logFilter.isFilterPresent()) {
052                noFilter = true;
053            }
054            else {
055                logFilter.constructPattern();
056            }
057            while (line != null) {
058                if (noFilter) {
059                    logWriter.write(line + "\n");
060                }
061                else {
062                    ArrayList<String> logParts = logFilter.splitLogMessage(line);
063                    if (logParts != null) {
064                        patternMatched = logFilter.matches(logParts);
065                    }
066                    if (patternMatched) {
067                        logWriter.write(line + "\n");
068                    }
069                }
070                lcnt++;
071                if (lcnt % 20 == 0) {
072                    logWriter.flush();
073                }
074                line = logReader.readLine();
075            }
076            logWriter.flush();
077        }
078    }