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