karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject [karaf-decanter] branch master updated: [KARAF-6318] Be able to persist AlertStore data
Date Thu, 26 Sep 2019 07:00:13 GMT
This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf-decanter.git


The following commit(s) were added to refs/heads/master by this push:
     new 392f612  [KARAF-6318] Be able to persist AlertStore data
     new b547159  Merge pull request #97 from fpapon/KARAF-6318
392f612 is described below

commit 392f61242b58db2cac6c060ca8bf9caaf8aa19cf
Author: Francois Papon <francois.papon@openobject.fr>
AuthorDate: Sun Sep 22 21:06:32 2019 +0200

    [KARAF-6318] Be able to persist AlertStore data
---
 alerting/checker/pom.xml                           |  8 +++
 .../decanter/alerting/checker/AlertStoreImpl.java  | 54 +++++++++++++-
 .../alerting/checker/AlertStoreImplTest.java       | 83 ++++++++++++++++++++++
 pom.xml                                            |  4 +-
 4 files changed, 144 insertions(+), 5 deletions(-)

diff --git a/alerting/checker/pom.xml b/alerting/checker/pom.xml
index f4b7638..f76e583 100644
--- a/alerting/checker/pom.xml
+++ b/alerting/checker/pom.xml
@@ -33,6 +33,14 @@
     <packaging>bundle</packaging>
     <name>Apache Karaf :: Decanter :: Alerting :: Checker</name>
 
+    <dependencies>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
     <build>
         <plugins>
             <plugin>
diff --git a/alerting/checker/src/main/java/org/apache/karaf/decanter/alerting/checker/AlertStoreImpl.java
b/alerting/checker/src/main/java/org/apache/karaf/decanter/alerting/checker/AlertStoreImpl.java
index 218d934..d1ebe81 100644
--- a/alerting/checker/src/main/java/org/apache/karaf/decanter/alerting/checker/AlertStoreImpl.java
+++ b/alerting/checker/src/main/java/org/apache/karaf/decanter/alerting/checker/AlertStoreImpl.java
@@ -16,24 +16,72 @@
  */
 package org.apache.karaf.decanter.alerting.checker;
 
-import org.osgi.service.component.annotations.Activate;
-import org.osgi.service.component.annotations.Component;
-
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.stream.Collectors;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @Component(
         immediate = true
 )
 public class AlertStoreImpl implements AlertStore {
 
+    private Logger logger = LoggerFactory.getLogger(AlertStoreImpl.class);
+
     private Set<String> errorAlerts;
     private Set<String> warnAlerts;
+    private File file;
 
     @Activate
     public void activate() {
         this.errorAlerts = new HashSet<>();
         this.warnAlerts = new HashSet<>();
+
+        // store the data file in $KARAF_DATA/decanter/alerter.db
+        file = new File(System.getProperty("karaf.data") + File.separator + "decanter" +
File.separator + "alerter.db");
+
+        if (file.exists()) {
+            try {
+                Files.lines(file.toPath())
+                        .forEach(line -> {
+                            if (line.startsWith(Level.error.name().concat(":"))) {
+                                this.errorAlerts.add(line.replaceFirst(Level.error.name().concat(":"),
""));
+                            } else if (line.startsWith(Level.warn.name().concat(":"))) {
+                                this.warnAlerts.add(line.replaceFirst(Level.warn.name().concat(":"),
""));
+                            } else {
+                                logger.error("Level unknow in line '{}'", line);
+                            }
+                        });
+            } catch (IOException exception) {
+                logger.error("Error while reading alerter store file!");
+            }
+        }
+    }
+
+    @Deactivate
+    public void deactivate() {
+        try {
+            // build data to write based on level for prefix
+            Set<String> data = new HashSet<>();
+            this.errorAlerts.stream().forEach(value -> data.add(Level.error.name().concat(":").concat(value)));
+            this.warnAlerts.stream().forEach(value -> data.add(Level.warn.name().concat(":").concat(value)));
+
+            // create directories if not exists
+            Files.createDirectories(file.getParentFile().toPath());
+
+            // write data
+            Files.write(file.toPath(), data.stream().collect(Collectors.toSet()), StandardOpenOption.CREATE);
+        } catch (IOException exception) {
+            logger.error("Error while writing alerter store file!");
+        }
     }
 
     public void add(String name, Level level) {
diff --git a/alerting/checker/src/test/java/org/apache/karaf/decanter/alerting/checker/AlertStoreImplTest.java
b/alerting/checker/src/test/java/org/apache/karaf/decanter/alerting/checker/AlertStoreImplTest.java
new file mode 100644
index 0000000..1c40a18
--- /dev/null
+++ b/alerting/checker/src/test/java/org/apache/karaf/decanter/alerting/checker/AlertStoreImplTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.alerting.checker;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class AlertStoreImplTest {
+
+    Logger logger = LoggerFactory.getLogger(AlertStoreImplTest.class);
+
+    @Before
+    public void setUp() {
+        System.setProperty("karaf.data", "target/data");
+    }
+
+    @Test
+    public void testWithoutInitFile() {
+        AlertStoreImpl alertStore = new AlertStoreImpl();
+        alertStore.activate();
+
+        alertStore.add("log service unavailable", AlertStore.Level.error);
+        Assert.assertTrue(alertStore.known("log service unavailable", AlertStore.Level.error));
+
+        alertStore.add("file service stopped", AlertStore.Level.warn);
+        Assert.assertTrue(alertStore.known("file service stopped", AlertStore.Level.warn));
+
+        alertStore.deactivate();
+
+        File file = new File(System.getProperty("karaf.data") + File.separator + "decanter"
+ File.separator + "alerter.db");
+        Assert.assertTrue(file.exists());
+
+        try {
+            Assert.assertEquals(2, Files.lines(file.toPath()).toArray().length);
+        } catch (IOException exception) {
+            logger.error("error while opening alerter db file!");
+        }
+    }
+
+    @Test
+    public void testWithInitFile() throws IOException {
+        File file = new File(System.getProperty("karaf.data") + File.separator + "decanter"
+ File.separator + "alerter.db");
+        Files.createDirectories(file.getParentFile().toPath());
+        Files.write(file.toPath(),
+                    Stream.of("error:log service unavailable", "warn:file service stopped").collect(Collectors.toSet()),
+                StandardOpenOption.CREATE);
+        Assert.assertTrue(file.exists());
+
+        AlertStoreImpl alertStore = new AlertStoreImpl();
+        alertStore.activate();
+
+        Assert.assertTrue(alertStore.known("log service unavailable", AlertStore.Level.error));
+        Assert.assertTrue(alertStore.known("file service stopped", AlertStore.Level.warn));
+
+        alertStore.deactivate();
+
+        Assert.assertEquals(2, Files.lines(file.toPath()).toArray().length);
+    }
+
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 8161c81..4754b3c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -222,8 +222,8 @@
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
-                    <source>1.7</source>
-                    <target>1.7</target>
+                    <source>1.8</source>
+                    <target>1.8</target>
                 </configuration>
             </plugin>
             <plugin>


Mime
View raw message