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