]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
AuditLogging improvements for easier unit testing 56/1356/2
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 8 Jan 2018 05:54:46 +0000 (07:54 +0200)
committerjsimomaa <jani.simomaa@gmail.com>
Mon, 8 Jan 2018 08:44:25 +0000 (10:44 +0200)
refs #7684

Change-Id: I6f4b10d2344b4588582f2558b9fda75f6d20145a

bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Client.scl
bundles/org.simantics.auditlogging/scl/Simantics/AuditLogging/Server.scl
bundles/org.simantics.auditlogging/src/org/simantics/audit/AuditLogging.java
bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingAPIClient.java
bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingClient.java
bundles/org.simantics.auditlogging/src/org/simantics/audit/server/AuditLoggingAPI.java

index f60474aa401de26dca0483fdfef5cc2eef96ac1e..4c8c9be6832adbef1411eed61f5413ac0a7304e0 100644 (file)
@@ -1,6 +1,13 @@
 import "Map" as Map
 
 importJava "org.simantics.audit.client.AuditLoggingClient" where
 import "Map" as Map
 
 importJava "org.simantics.audit.client.AuditLoggingClient" where
+
+    @JavaName fromProps
+    auditLoggingClient :: Map.T a a -> <Proc> b 
+
+    @JavaName getUUID
+    getAuditLoggingUUID :: <Proc> String
+
     @JavaName sendLog
     sendLogM :: Map.T String a -> <Proc> ()
     sendLog :: [a] -> <Proc> ()
     @JavaName sendLog
     sendLogM :: Map.T String a -> <Proc> ()
     sendLog :: [a] -> <Proc> ()
index 396f5f5619a222c8b0c07ca95246fadf84b49c88..9304294c094781e270f72cda58ab26f024acee23 100644 (file)
@@ -5,6 +5,7 @@ importJava "org.simantics.audit.AuditLogging" where
     log :: String -> Map.T String a -> <Proc, Exception> ()
     error :: String -> Map.T String a -> <Proc, Exception> ()
     trace :: String -> Map.T String a -> <Proc, Exception> ()
     log :: String -> Map.T String a -> <Proc, Exception> ()
     error :: String -> Map.T String a -> <Proc, Exception> ()
     trace :: String -> Map.T String a -> <Proc, Exception> ()
+    getLogEventsDays :: String -> String -> Integer -> <Proc, Exception> [String]
     getLogEvents :: String -> String -> String -> String -> <Proc, Exception> [String]
 
 importJava "org.simantics.audit.server.AuditLoggingServer" where
     getLogEvents :: String -> String -> String -> String -> <Proc, Exception> [String]
 
 importJava "org.simantics.audit.server.AuditLoggingServer" where
index cbbf1420984adbf81a6169c103c94d421501e85e..334e19c4762787ca579db78da236687ae6b53f79 100644 (file)
@@ -40,18 +40,45 @@ public class AuditLogging {
             throw new AuditLoggingException("Could not register service with id " + id, e);
         }
     }
             throw new AuditLoggingException("Could not register service with id " + id, e);
         }
     }
-    
+
+    /**
+     * Gets audit events for the last 5 days
+     * 
+     * @param uuid
+     * @param level
+     * @return
+     * @throws AuditLoggingException
+     */
+    public static List<String> getLogEventsDays(String uuid, String level, int days) throws AuditLoggingException {
+        LocalDate endDate = LocalDate.now().plusDays(1);
+        LocalDate startDate = endDate.minusDays(days);
+        return getLogEvents(uuid, level, startDate, endDate);
+    }
+
     public static List<String> getLogEvents(String uuid, String level, String startDate, String endDate) throws AuditLoggingException {
     public static List<String> getLogEvents(String uuid, String level, String startDate, String endDate) throws AuditLoggingException {
-        Path entryRoot = getEntryRoot(uuid);
         try {
             LocalDate localStartDate = LocalDate.parse(startDate);
             LocalDate localEndDate = LocalDate.parse(endDate).plusDays(1);
         try {
             LocalDate localStartDate = LocalDate.parse(startDate);
             LocalDate localEndDate = LocalDate.parse(endDate).plusDays(1);
+            return getLogEvents(uuid, level, localStartDate, localEndDate);
+        } catch (Exception e) {
+            throw new AuditLoggingException(e);
+        }
+    }
+
+    private static List<String> getLogEvents(String uuid, String level, LocalDate localStartDate, LocalDate localEndDate) throws AuditLoggingException {
+        Path entryRoot = getEntryRoot(uuid);
+        try {
             List<String> allLines = new ArrayList<>();
             while (localStartDate.isBefore(localEndDate)) {
                 String fileName = resolveLogFileName(uuid, Level.valueOf(level.toUpperCase()), localStartDate);
                 try {
             List<String> allLines = new ArrayList<>();
             while (localStartDate.isBefore(localEndDate)) {
                 String fileName = resolveLogFileName(uuid, Level.valueOf(level.toUpperCase()), localStartDate);
                 try {
-                    List<String> lines = Files.readAllLines(entryRoot.resolve(fileName));
-                    allLines.addAll(lines);
+                    Path fileToRead = entryRoot.resolve(fileName);
+                    if (Files.exists(fileToRead)) {
+                        List<String> lines = Files.readAllLines(fileToRead);
+                        allLines.addAll(lines);
+                    } else {
+                        LOGGER.info("No logging events for " + fileName);
+                    }
                 } catch (FileSystemException e) {
                     // presumably file not found but lets not throw this cause forward, log here
                     LOGGER.error("Could not read file {}", fileName, e);
                 } catch (FileSystemException e) {
                     // presumably file not found but lets not throw this cause forward, log here
                     LOGGER.error("Could not read file {}", fileName, e);
@@ -64,7 +91,6 @@ public class AuditLogging {
             throw new AuditLoggingException(e);
         }
     }
             throw new AuditLoggingException(e);
         }
     }
-    
 
     public static Path getEntryRoot(String uuid) {
         return Activator.getLogLocation().resolve(uuid);
 
     public static Path getEntryRoot(String uuid) {
         return Activator.getLogLocation().resolve(uuid);
index a7fc6022a921489f6bfabf4d67bcd1df8e750996..645a6065749911ff9bcbf222e9246e1b6430eb79 100644 (file)
@@ -47,6 +47,10 @@ public class AuditLoggingAPIClient {
         }
     }
 
         }
     }
 
+    public String getUuid() {
+        return uuid;
+    }
+
     private void register(String id) throws AuditLoggingException {
         try {
             Response response = base.path("register").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(Collections.singletonMap("id", id)));
     private void register(String id) throws AuditLoggingException {
         try {
             Response response = base.path("register").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(Collections.singletonMap("id", id)));
index e31a6ab214531a967fee0a06d3d41cd12fb7e573..63045fdcfe7a3f598751a7207b2d2d12c2a01950 100644 (file)
@@ -21,29 +21,36 @@ public class AuditLoggingClient {
 
     private AuditLoggingAPIClient apiClient;
 
 
     private AuditLoggingAPIClient apiClient;
 
-    private AuditLoggingClient() throws AuditLoggingException {
+    private AuditLoggingClient(String serverAddress) throws AuditLoggingException {
         // Read config from sysargs
         // Read config from sysargs
-        System.out.println("asd");
-        String serverAddress = System.getProperty(AUDIT_SERVER_ADDRESS);
-        if (serverAddress != null && !serverAddress.isEmpty()) {
-            apiClient = new AuditLoggingAPIClient("testlog", serverAddress);
-             
-        } else {
-            LOGGER.warn("No {} system property defined so client not configured", AUDIT_SERVER_ADDRESS);
-        }
+        apiClient = new AuditLoggingAPIClient("testlog", serverAddress);
+    }
+
+    private static AuditLoggingClient fromEnv() throws AuditLoggingException {
+        return fromProps(System.getProperties());
     }
 
     }
 
-    private static AuditLoggingClient instance() throws AuditLoggingException {
+    
+    public static AuditLoggingClient fromProps(Map<Object, Object> properties) throws AuditLoggingException {
         if (INSTANCE == null) {
             synchronized (AuditLoggingClient.class) {
                 if (INSTANCE == null) {
         if (INSTANCE == null) {
             synchronized (AuditLoggingClient.class) {
                 if (INSTANCE == null) {
-                    INSTANCE = new AuditLoggingClient();
+                    String serverAddress = (String) properties.get(AUDIT_SERVER_ADDRESS);
+                    if (serverAddress != null && !serverAddress.isEmpty()) {
+                        INSTANCE = new AuditLoggingClient(serverAddress);
+                    } else {
+                        LOGGER.warn("No {} system property defined so client not configured", AUDIT_SERVER_ADDRESS);
+                    }
                 }
             }
         }
         return INSTANCE;
     }
 
                 }
             }
         }
         return INSTANCE;
     }
 
+    public static String getUUID() throws AuditLoggingException {
+        return fromEnv().apiClient.getUuid();
+    }
+    
     public static void sendLog(List<Object> keyValues) throws AuditLoggingException {
         commit(Level.INFO, toMap(keyValues.toArray()));
     }
     public static void sendLog(List<Object> keyValues) throws AuditLoggingException {
         commit(Level.INFO, toMap(keyValues.toArray()));
     }
@@ -84,7 +91,7 @@ public class AuditLoggingClient {
 
     private static void commit(Level level, Map<String, Object> message) throws AuditLoggingException {
         try {
 
     private static void commit(Level level, Map<String, Object> message) throws AuditLoggingException {
         try {
-            AuditLoggingAPIClient client = instance().apiClient;
+            AuditLoggingAPIClient client = fromEnv().apiClient;
             if (client == null) {
                 // No can do - at least log to file
                 LOGGER.warn("Audit logging server not configured - printing event to log");
             if (client == null) {
                 // No can do - at least log to file
                 LOGGER.warn("Audit logging server not configured - printing event to log");
index 980ea4c982751608d8deeeb3283b50b601b37926..c5eccfad93e843ed941f3b2bc6e24051e9d4b541 100644 (file)
@@ -59,4 +59,17 @@ public class AuditLoggingAPI {
             return Response.serverError().entity(buildJSONResponse("message", e.getMessage())).build();
         }
     }
             return Response.serverError().entity(buildJSONResponse("message", e.getMessage())).build();
         }
     }
+
+    @Path("{uuid}/error")
+    @POST
+    public Response error(@PathParam("uuid") String uuid, Map<String, Object> payload) {
+        
+        try {
+            AuditLogging.error(uuid, payload);
+            return Response.ok().build();
+        } catch (AuditLoggingException e) {
+            LOGGER.error("Could not log error audit with id {}", uuid, e);
+            return Response.serverError().entity(buildJSONResponse("message", e.getMessage())).build();
+        }
+    }
 }
 }