Skip to content

Commit 843ee74

Browse files
committed
NTA-89: Add a keyword filter
https://issues.jboss.org/browse/NTA-89
1 parent 04e2849 commit 843ee74

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed

Diff for: core/src/main/java/io/narayana/nta/Configuration.java

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package io.narayana.nta;
2424

2525
import io.narayana.nta.logparsing.as8.filters.PackageFilter;
26+
import io.narayana.nta.logparsing.as8.filters.KeywordFilter;
2627
import io.narayana.nta.logparsing.as8.handlers.*;
2728

2829
import java.io.File;
@@ -80,6 +81,7 @@ public final class Configuration {
8081

8182
public static final Class[] LOG_FILTERS = new Class[]{
8283
PackageFilter.class,
84+
KeywordFilter.class,
8385
};
8486

8587
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.narayana.nta.logparsing.as8.filters;
2+
3+
import org.apache.log4j.Logger;
4+
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Properties;
11+
12+
public class GetFilterKeywords {
13+
private List<String> filterKeywords;
14+
private static final Logger logger = Logger.getLogger(GetFilterKeywords.class.getName());
15+
16+
private static class GetFilterKeywordsHolder {
17+
private static final GetFilterKeywords INSTANCE = new GetFilterKeywords();
18+
}
19+
20+
public static final GetFilterKeywords getInstance() {
21+
return GetFilterKeywordsHolder.INSTANCE;
22+
}
23+
24+
private GetFilterKeywords() {
25+
InputStream inputStream = null;
26+
try {
27+
Properties prop = new Properties();
28+
filterKeywords = new ArrayList<>();
29+
30+
String propFileName = "filter.properties";
31+
logger.warn(getClass().getClassLoader().getResource());
32+
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
33+
if (inputStream != null) {
34+
prop.load(inputStream);
35+
} else {
36+
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
37+
}
38+
39+
for (String name : prop.stringPropertyNames()) {
40+
if (name.contains("keywords"))
41+
filterKeywords.add(prop.getProperty(name));
42+
}
43+
} catch (Exception e) {
44+
e.printStackTrace();
45+
} finally {
46+
try {
47+
inputStream.close();
48+
} catch (IOException ignored) {
49+
}
50+
}
51+
}
52+
53+
public List<String> getFilterKeywords() {
54+
return filterKeywords;
55+
}
56+
}
57+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright 2016, Red Hat, Inc., and individual contributors
4+
* as indicated by the @author tags. See the copyright.txt file in the
5+
* distribution for a full listing of individual contributors.
6+
*
7+
* This is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation; either version 2.1 of
10+
* the License, or (at your option) any later version.
11+
*
12+
* This software is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this software; if not, write to the Free
19+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21+
*/
22+
23+
package io.narayana.nta.logparsing.as8.filters;
24+
25+
import io.narayana.nta.logparsing.common.Filter;
26+
27+
import java.util.List;
28+
29+
/**
30+
* @author huyuan
31+
*/
32+
public class KeywordFilter implements Filter {
33+
34+
35+
@Override
36+
public boolean matches(String line) throws IndexOutOfBoundsException {
37+
38+
List<String> keywordList = GetFilterKeywords.getInstance().getFilterKeywords();
39+
40+
try {
41+
for (String s : keywordList) {
42+
if (line.contains(s)) { // if black list use !line.contains(s)
43+
return true;
44+
}
45+
}
46+
return false;
47+
} catch (IndexOutOfBoundsException e) {
48+
return false;
49+
}
50+
}
51+
}

Diff for: core/src/main/resources/filter.properties

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
keywords.1=TransactionImple
2+
keywords.2=Periodic Recovery
3+
keywords.3=TransactionSynchronizationRegistryImple
4+

Diff for: core/src/test/java/io/narayana/nta/test/HandlerServiceTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public static WebArchive createDeployment() {
7979
.addAsWebInfResource(new FileAsset(new File("src/test/resources/persistence.xml")),
8080
"classes/META-INF/persistence.xml")
8181
.addAsManifestResource(new FileAsset(new File("src/test/resources/nta-test-ds.xml")), "nta-test-ds.xml")
82+
.addAsManifestResource(new FileAsset(new File("src/test/resources/filter.properties")), "filter.properties")
8283
.addAsLibraries(libs)
8384
.setManifest(new StringAsset(ManifestMF));
8485
}

Diff for: core/src/test/resources/filter.properties

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
keywords.1=TransactionImple
2+
keywords.2=Periodic Recovery
3+
keywords.3=TransactionSynchronizationRegistryImple
4+

0 commit comments

Comments
 (0)