]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.auditlogging/src/org/simantics/audit/client/AuditLoggingClient.java
e9043187d76db1c28b5325292b4ce38a7848412c
[simantics/platform.git] / bundles / org.simantics.auditlogging / src / org / simantics / audit / client / AuditLoggingClient.java
1 package org.simantics.audit.client;
2
3 import java.util.Arrays;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.simantics.audit.AuditLogging;
9 import org.simantics.audit.AuditLogging.Level;
10 import org.simantics.audit.AuditLoggingException;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 public class AuditLoggingClient {
15
16     private static final String AUDIT_SERVER_ADDRESS = "org.simantics.audit.serverAddress";
17     
18     private static final Logger LOGGER = LoggerFactory.getLogger(AuditLoggingClient.class);
19     
20     private static AuditLoggingClient INSTANCE;
21
22     private AuditLoggingAPIClient apiClient;
23
24     private AuditLoggingClient(String serverAddress) throws AuditLoggingException {
25         // Read config from sysargs
26         apiClient = new AuditLoggingAPIClient("testlog", serverAddress);
27     }
28
29     private static AuditLoggingClient fromEnv() throws AuditLoggingException {
30         return fromProps(System.getProperties());
31     }
32
33     
34     public static AuditLoggingClient fromProps(Map<Object, Object> properties) throws AuditLoggingException {
35         if (INSTANCE == null) {
36             synchronized (AuditLoggingClient.class) {
37                 if (INSTANCE == null) {
38                     String serverAddress = (String) properties.get(AUDIT_SERVER_ADDRESS);
39                     if (serverAddress != null && !serverAddress.isEmpty()) {
40                         INSTANCE = new AuditLoggingClient(serverAddress);
41                     } else {
42                         LOGGER.warn("No {} system property defined so client not configured", AUDIT_SERVER_ADDRESS);
43                     }
44                 }
45             }
46         }
47         return INSTANCE;
48     }
49
50     public static String getUUID() throws AuditLoggingException {
51         return fromEnv().apiClient.getUuid();
52     }
53     
54     public static void sendLog(List<Object> keyValues) throws AuditLoggingException {
55         commit(Level.INFO, toMap(keyValues.toArray()));
56     }
57
58     private static Map<String, Object> toMap(Object... keyValues) {
59         if ((keyValues.length % 2) != 0)
60             throw new IllegalArgumentException("Invalid amount of arguments! " + Arrays.toString(keyValues));
61         Map<String, Object> results = new HashMap<>(keyValues.length / 2);
62         for (int i = 0; i < keyValues.length; i += 2) {
63             Object key = keyValues[i];
64             Object value = keyValues[i + 1];
65             if (!(key instanceof String))
66                 throw new IllegalArgumentException("Key with index " + i + " is not String");
67             results.put((String) key, value);
68         }
69         return results;
70     }
71     
72     public static void sendLog(Map<String, Object> event) throws AuditLoggingException {
73         commit(Level.INFO, event);
74     }
75
76     public static void sendError(Map<String, Object> event) throws AuditLoggingException {
77         commit(Level.ERROR, event);
78     }
79
80     public static void sendError(List<Object> keyValues) throws AuditLoggingException {
81         commit(Level.ERROR, toMap(keyValues.toArray()));
82     }
83
84     public static void sendTrace(Map<String, Object> event) throws AuditLoggingException {
85         commit(Level.TRACE, event);
86     }
87
88     public static void sendTrace(List<Object> keyValues) throws AuditLoggingException {
89         commit(Level.TRACE, toMap(keyValues.toArray()));
90     }
91
92     private static void commit(Level level, Map<String, Object> message) throws AuditLoggingException {
93         try {
94             AuditLoggingClient client = fromEnv();
95             if (client == null || client.apiClient == null) {
96                 // No can do - at least log to file
97                 LOGGER.warn("Audit logging server not configured - printing event to log");
98                 LOGGER.info(message.toString());
99             } else {
100                 AuditLoggingAPIClient apiClient = client.apiClient;
101                 switch (level) {
102                 case INFO:
103                     apiClient.log(message);
104                     break;
105                 case ERROR:
106                     apiClient.error(message);
107                     break;
108                 case TRACE:
109                     apiClient.trace(message);
110                     break;
111                 default:
112                     break;
113                 }
114             }
115         } catch (AuditLoggingException e) {
116             // Just for debugging purposes
117             LOGGER.error("Could not send audit event {} with level {}", message, level, e);
118             // log this locally to a file just in case
119             AuditLogging.log("local", message);
120             throw e;
121         }
122     }
123 }