From 4b9ea954fed4d63756f0f41eafc74f351ff18a01 Mon Sep 17 00:00:00 2001 From: Yuan Hu Date: Fri, 29 Apr 2016 15:34:18 +0800 Subject: [PATCH] NTA-89: Add a keyword filter https://issues.jboss.org/browse/NTA-89 --- .../java/io/narayana/nta/Configuration.java | 2 + .../as8/filters/GetFilterKeywords.java | 39 ++++++++++++++ .../logparsing/as8/filters/KeywordFilter.java | 51 +++++++++++++++++++ core/src/main/resources/filter.properties | 1 + .../narayana/nta/test/HandlerServiceTest.java | 1 + core/src/test/resources/filter.properties | 1 + 6 files changed, 95 insertions(+) create mode 100644 core/src/main/java/io/narayana/nta/logparsing/as8/filters/GetFilterKeywords.java create mode 100644 core/src/main/java/io/narayana/nta/logparsing/as8/filters/KeywordFilter.java create mode 100644 core/src/main/resources/filter.properties create mode 100644 core/src/test/resources/filter.properties diff --git a/core/src/main/java/io/narayana/nta/Configuration.java b/core/src/main/java/io/narayana/nta/Configuration.java index 8d75a4e..89158d9 100644 --- a/core/src/main/java/io/narayana/nta/Configuration.java +++ b/core/src/main/java/io/narayana/nta/Configuration.java @@ -22,6 +22,7 @@ package io.narayana.nta; +import io.narayana.nta.logparsing.as8.filters.KeywordFilter; import io.narayana.nta.logparsing.as8.filters.PackageFilter; import io.narayana.nta.logparsing.as8.handlers.*; @@ -80,6 +81,7 @@ public final class Configuration { public static final Class[] LOG_FILTERS = new Class[]{ PackageFilter.class, + KeywordFilter.class, }; /** diff --git a/core/src/main/java/io/narayana/nta/logparsing/as8/filters/GetFilterKeywords.java b/core/src/main/java/io/narayana/nta/logparsing/as8/filters/GetFilterKeywords.java new file mode 100644 index 0000000..db675d5 --- /dev/null +++ b/core/src/main/java/io/narayana/nta/logparsing/as8/filters/GetFilterKeywords.java @@ -0,0 +1,39 @@ +package io.narayana.nta.logparsing.as8.filters; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class GetFilterKeywords { + + InputStream inputStream; + String filterKeywords; + + public String getPropValues() { + + try { + Properties prop = new Properties(); + + String propFileName = "filter.properties"; + inputStream = getClass().getClassLoader().getResourceAsStream(propFileName); + + if (inputStream != null) { + prop.load(inputStream); + } else { + throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath"); + } + + filterKeywords = prop.getProperty("keywords"); + + } catch (Exception e) { + System.out.println("Exception: " + e); + } finally { + try { + inputStream.close(); + } catch (IOException e){ + } + } + return filterKeywords; + } +} \ No newline at end of file diff --git a/core/src/main/java/io/narayana/nta/logparsing/as8/filters/KeywordFilter.java b/core/src/main/java/io/narayana/nta/logparsing/as8/filters/KeywordFilter.java new file mode 100644 index 0000000..f1abe31 --- /dev/null +++ b/core/src/main/java/io/narayana/nta/logparsing/as8/filters/KeywordFilter.java @@ -0,0 +1,51 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package io.narayana.nta.logparsing.as8.filters; + +import io.narayana.nta.logparsing.common.Filter; + +/** + * @author huyuan + * + */ +public class KeywordFilter implements Filter{ + + GetFilterKeywords filterKeywords = new GetFilterKeywords(); + + @Override + public boolean matches(String line) throws IndexOutOfBoundsException{ + + String keywordList = filterKeywords.getPropValues(); + String[] keywords = keywordList.split(","); + + try { + for (int i = 0; i < keywords.length; i++){ + if(line.indexOf(keywords[i]) != -1) + return true; + } + return false; + } catch (IndexOutOfBoundsException e) { + return false; + } + } +} \ No newline at end of file diff --git a/core/src/main/resources/filter.properties b/core/src/main/resources/filter.properties new file mode 100644 index 0000000..ad7f333 --- /dev/null +++ b/core/src/main/resources/filter.properties @@ -0,0 +1 @@ +keywords=TransactionImple,Periodic Recovery,TransactionSynchronizationRegistryImple diff --git a/core/src/test/java/io/narayana/nta/test/HandlerServiceTest.java b/core/src/test/java/io/narayana/nta/test/HandlerServiceTest.java index 0c0c583..86ad6a1 100644 --- a/core/src/test/java/io/narayana/nta/test/HandlerServiceTest.java +++ b/core/src/test/java/io/narayana/nta/test/HandlerServiceTest.java @@ -79,6 +79,7 @@ public static WebArchive createDeployment() { .addAsWebInfResource(new FileAsset(new File("src/test/resources/persistence.xml")), "classes/META-INF/persistence.xml") .addAsManifestResource(new FileAsset(new File("src/test/resources/nta-test-ds.xml")), "nta-test-ds.xml") + .addAsManifestResource(new FileAsset(new File("src/test/resources/filter.properties")), "filter.properties") .addAsLibraries(libs) .setManifest(new StringAsset(ManifestMF)); } diff --git a/core/src/test/resources/filter.properties b/core/src/test/resources/filter.properties new file mode 100644 index 0000000..ad7f333 --- /dev/null +++ b/core/src/test/resources/filter.properties @@ -0,0 +1 @@ +keywords=TransactionImple,Periodic Recovery,TransactionSynchronizationRegistryImple