2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.filesearching;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.io.PrintWriter;
29 import java.io.UnsupportedEncodingException;
30 import java.util.NoSuchElementException;
31 import java.util.Scanner;
33 public class LogFileSearching {
35 private static PrintWriter writer;
37 public static void initFile(String filePath) {
40 // This is to reopen an existing file
41 writer = new PrintWriter(new FileOutputStream(filePath,true));
42 } catch ( IOException e) {
43 System.out.println("Exception caught when trying to open the file /tmp/mso-log-checker.log to dump the result");
49 public static void closeFile() {
57 public static boolean searchInFile(final String needle, final File file) throws FileNotFoundException {
58 Scanner logScanner = new Scanner(file);
60 writer.println("Searching pattern " + needle + " in " + file.getAbsolutePath());
61 //System.out.println("Searching pattern " + needle + " in " + file.getAbsolutePath());
63 String filedata = logScanner.useDelimiter("\\Z").next();
68 while (index < filedata.length() && (index = filedata.indexOf(needle, index)) >= 0) {
71 int separatorIndex = filedata.indexOf(System.getProperty("line.separator"), index);
72 if (separatorIndex >=0){
73 writer.println("FOUND:" + filedata.substring(index, separatorIndex));
74 //System.out.println("FOUND:"
75 // + filedata.substring(index, separatorIndex));
77 writer.println("FOUND:" + filedata.substring(index, filedata.length()-1));
78 //System.out.println("FOUND:"
79 // + filedata.substring(index, filedata.length()-1));
81 index += needle.length();
83 writer.println("TOTAL:" + occurrences + " FOUND");
84 //System.out.println("TOTAL:" + occurrences + " FOUND");
85 if (occurrences > 0) {
92 } catch (NoSuchElementException e) {
93 writer.println("TOTAL:0 FOUND");
94 //System.out.println("TOTAL:0 FOUND");
101 public static boolean searchInDirectory(final String needle, final File directory) throws FileNotFoundException {
104 String[] dirFiles = directory.list();
106 if (dirFiles != null) {
108 for (String dir : dirFiles) {
109 res = res || searchInDirectory(needle, new File(directory.getAbsolutePath() + "/" + dir));
113 return LogFileSearching.searchInFile(needle, directory);
118 public static boolean searchInDirectoryForCommonIssues(final String[] needles, final File directory) throws FileNotFoundException {
119 String[] defaultPatternsToUse = {"ClassNotFound","NullPointer","RuntimeException","IllegalStateException","FATAL"};
121 if (needles != null && needles.length>0) {
122 defaultPatternsToUse=needles;
125 boolean returnValue=false;
126 for (String needle:defaultPatternsToUse) {
127 returnValue |= LogFileSearching.searchInDirectory(needle,directory);