karaf-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (KARAF-5888) Be able to define a line parser service in the file collector
Date Fri, 07 Sep 2018 07:01:00 GMT

    [ https://issues.apache.org/jira/browse/KARAF-5888?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16606759#comment-16606759
] 

ASF GitHub Bot commented on KARAF-5888:
---------------------------------------

jbonofre closed pull request #49: KARAF-5888 Be able to define a line parser service in the
file collector
URL: https://github.com/apache/karaf-decanter/pull/49
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/api/pom.xml b/api/pom.xml
index fcc7acf..1fdcb11 100644
--- a/api/pom.xml
+++ b/api/pom.xml
@@ -46,7 +46,8 @@
                     <instructions>
                         <Export-Package>
                             org.apache.karaf.decanter.api,
-                            org.apache.karaf.decanter.api.marshaller
+                            org.apache.karaf.decanter.api.marshaller,
+                            org.apache.karaf.decanter.api.parser
                         </Export-Package>
                     </instructions>
                 </configuration>
diff --git a/api/src/main/java/org/apache/karaf/decanter/api/parser/Parser.java b/api/src/main/java/org/apache/karaf/decanter/api/parser/Parser.java
new file mode 100644
index 0000000..c5567c3
--- /dev/null
+++ b/api/src/main/java/org/apache/karaf/decanter/api/parser/Parser.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.karaf.decanter.api.parser;
+
+import java.util.Map;
+
+public interface Parser {
+
+    String SERVICE_KEY_ID = "parserId";
+
+    Map<String, Object> parse(String line);
+}
diff --git a/api/src/main/java/org/apache/karaf/decanter/api/parser/packageinfo b/api/src/main/java/org/apache/karaf/decanter/api/parser/packageinfo
new file mode 100644
index 0000000..c72722a
--- /dev/null
+++ b/api/src/main/java/org/apache/karaf/decanter/api/parser/packageinfo
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+version 1.0.0
diff --git a/api/src/main/java/org/apache/karaf/decanter/impl/parser/IdentityParser.java b/api/src/main/java/org/apache/karaf/decanter/impl/parser/IdentityParser.java
new file mode 100644
index 0000000..f088d9a
--- /dev/null
+++ b/api/src/main/java/org/apache/karaf/decanter/impl/parser/IdentityParser.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.karaf.decanter.impl.parser;
+
+import org.apache.karaf.decanter.api.parser.Parser;
+import org.osgi.service.component.annotations.Component;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Component(immediate = true, property = Parser.SERVICE_KEY_ID + "=identify")
+public class IdentityParser implements Parser {
+
+    @Override
+    public Map<String, Object> parse(String line) {
+        Map<String, Object> data = new HashMap<>();
+
+        try {
+            data.put("line", Integer.parseInt(line));
+            return data;
+        } catch (Exception e) {
+            // nothing to do
+        }
+
+        try {
+            data.put("line", Long.parseLong(line));
+            return data;
+        } catch (Exception e) {
+            // nothing to do
+        }
+
+        data.put("line", line);
+        return data;
+    }
+}
diff --git a/api/src/test/java/org/apache/karaf/decanter/impl/parser/TestIdentityParser.java
b/api/src/test/java/org/apache/karaf/decanter/impl/parser/TestIdentityParser.java
new file mode 100644
index 0000000..fae39ed
--- /dev/null
+++ b/api/src/test/java/org/apache/karaf/decanter/impl/parser/TestIdentityParser.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.karaf.decanter.impl.parser;
+
+import org.apache.karaf.decanter.api.parser.Parser;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Map;
+
+public class TestIdentityParser {
+
+    @Test
+    public void testParse() {
+
+        Parser parser = new IdentityParser();
+        Map<String, Object> data;
+
+        // String type test
+        String lineString = "2018-09-07T08:40:41,768 | INFO  | FelixStartLevel  | core  
  | 14 - " +
+                "org.apache.aries.jmx.core - 1.1.8 | Unregistering MBean with ObjectName
" +
+                "[osgi.compendium:service=cm,version=1.3,framework=org.apache.felix.framework,"
+
+                "uuid=e75146c3-f73c-46e3-878b-1b88e58d76cf] for service with service.id [16]";
+
+        System.out.println("line String :: " + lineString);
+        data = parser.parse(lineString);
+        Assert.assertNotNull("parser result is null", data);
+        Assert.assertEquals("parser size result is incorrect",1, data.size());
+
+        Assert.assertTrue("parser value is not a string",data.get("line") instanceof String);
+
+        // Integer type test
+        String lineInteger = "512";
+
+        System.out.println("line Integer :: " + lineInteger);
+        data = parser.parse(lineInteger);
+        Assert.assertNotNull("parser result is null", data);
+        Assert.assertEquals("parser size result is incorrect", 1, data.size());
+
+        Assert.assertTrue("parser value is not an integer",data.get("line") instanceof Integer);
+
+        // Long type test
+        String lineLong = "9223372036854775806";
+
+        System.out.println("line Long :: " + lineLong);
+        data = parser.parse(lineLong);
+        Assert.assertNotNull("parser result is null", data);
+        Assert.assertEquals("parser size result is incorrect", 1, data.size());
+
+        Assert.assertTrue("parser value is not a long",data.get("line") instanceof Long);
+    }
+}
diff --git a/collector/file/pom.xml b/collector/file/pom.xml
index 37ab46a..6a253b9 100644
--- a/collector/file/pom.xml
+++ b/collector/file/pom.xml
@@ -43,6 +43,10 @@
             <groupId>org.apache.karaf.decanter.collector</groupId>
             <artifactId>org.apache.karaf.decanter.collector.utils</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.decanter</groupId>
+            <artifactId>org.apache.karaf.decanter.api</artifactId>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/collector/file/src/main/java/org/apache/karaf/decanter/collector/file/DecanterTailerListener.java
b/collector/file/src/main/java/org/apache/karaf/decanter/collector/file/DecanterTailerListener.java
index 0894b81..040953d 100644
--- a/collector/file/src/main/java/org/apache/karaf/decanter/collector/file/DecanterTailerListener.java
+++ b/collector/file/src/main/java/org/apache/karaf/decanter/collector/file/DecanterTailerListener.java
@@ -25,6 +25,7 @@
 
 import org.apache.commons.io.input.Tailer;
 import org.apache.commons.io.input.TailerListenerAdapter;
+import org.apache.karaf.decanter.api.parser.Parser;
 import org.apache.karaf.decanter.collector.utils.PropertiesPreparator;
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.component.ComponentContext;
@@ -48,6 +49,9 @@
     @Reference
     public EventAdmin dispatcher;
 
+    @Reference
+    public Parser parser;
+
     private final static Logger LOGGER = LoggerFactory.getLogger(DecanterTailerListener.class);
 
     private String type;
@@ -95,17 +99,16 @@ public void handle(String line) {
         data.put("path", path);
         data.put("regex", regex);
 
-        // TODO: try some line parsing
         if (regex != null) {
             Pattern pattern = Pattern.compile(regex);
             Matcher matcher = pattern.matcher(line);
             if (matcher.matches()) {
-                data.put("line", line);
+                data.putAll(this.parser.parse(line));
             } else {
                 return;
             }
         } else {
-            data.put("line", line);
+            data.putAll(this.parser.parse(line));
         }
 
         try {
diff --git a/manual/src/main/asciidoc/user-guide/collectors.adoc b/manual/src/main/asciidoc/user-guide/collectors.adoc
index 3b03541..81304ba 100644
--- a/manual/src/main/asciidoc/user-guide/collectors.adoc
+++ b/manual/src/main/asciidoc/user-guide/collectors.adoc
@@ -149,6 +149,16 @@ my=stuff
 
 The file collector will tail on `karaf.log` file, and send any new line matching the `regex`
in this log file as collected data.
 
+By default, the collector use the `org.apache.karaf.decanter.impl.parser.IdentityParser`
parser to parse the line into
+a typed Object (Long, Integer or String) before send it to the EventDispatcher data map.
+
+You can write your own parser by implementing the `org.apache.karaf.decanter.api.parser.Parser`
interface and declare
+it into the file collector configuration file:
+
+----
+parser.target=(parserId=myParser)
+----
+
 ==== EventAdmin
 
 The Decanter EventAdmin Collector is an event-driven collector, listening for all internal
events happening in


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Be able to define a line parser service in the file collector
> -------------------------------------------------------------
>
>                 Key: KARAF-5888
>                 URL: https://issues.apache.org/jira/browse/KARAF-5888
>             Project: Karaf
>          Issue Type: Improvement
>          Components: decanter
>            Reporter: Jean-Baptiste Onofré
>            Assignee: Francois Papon
>            Priority: Major
>             Fix For: decanter-2.1.0
>
>
> Now the file collector uses a tail that takes any new lines appended in a file and send
the "raw" line to the dispatcher. It would be great to be able to use a line parser in the
collector that send the extracted property from the line.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message