--- /dev/null
+package org.simantics.audit.client;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.simantics.audit.AuditLogging;
+import org.simantics.audit.AuditLogging.Level;
+import org.simantics.audit.AuditLoggingException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class AuditLoggingClient {
+
+ private static final String AUDIT_SERVER_ADDRESS = "org.simantics.audit.serverAddress";
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(AuditLoggingClient.class);
+
+ private static AuditLoggingClient INSTANCE;
+
+ private AuditLoggingAPIClient apiClient;
+
+ private AuditLoggingClient() throws AuditLoggingException {
+ // 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);
+ }
+ }
+
+ private static AuditLoggingClient instance() throws AuditLoggingException {
+ if (INSTANCE == null) {
+ synchronized (AuditLoggingClient.class) {
+ if (INSTANCE == null) {
+ INSTANCE = new AuditLoggingClient();
+ }
+ }
+ }
+ return INSTANCE;
+ }
+
+ public static void sendLog(List<Object> keyValues) throws AuditLoggingException {
+ commit(Level.INFO, toMap(keyValues.toArray()));
+ }
+
+ private static Map<String, Object> toMap(Object... keyValues) {
+ if ((keyValues.length % 2) != 0)
+ throw new IllegalArgumentException("Invalid amount of arguments! " + Arrays.toString(keyValues));
+ Map<String, Object> results = new HashMap<>(keyValues.length / 2);
+ for (int i = 0; i < keyValues.length; i += 2) {
+ Object key = keyValues[i];
+ Object value = keyValues[i + 1];
+ if (!(key instanceof String))
+ throw new IllegalArgumentException("Key with index " + i + " is not String");
+ results.put((String) key, value);
+ }
+ return results;
+ }
+
+ public static void sendLog(Map<String, Object> event) throws AuditLoggingException {
+ commit(Level.INFO, event);
+ }
+
+ public static void sendError(Map<String, Object> event) throws AuditLoggingException {
+ commit(Level.ERROR, event);
+ }
+
+ public static void sendError(List<Object> keyValues) throws AuditLoggingException {
+ commit(Level.ERROR, toMap(keyValues.toArray()));
+ }
+
+ public static void sendTrace(Map<String, Object> event) throws AuditLoggingException {
+ commit(Level.TRACE, event);
+ }
+
+ public static void sendTrace(List<Object> keyValues) throws AuditLoggingException {
+ commit(Level.TRACE, toMap(keyValues.toArray()));
+ }
+
+ private static void commit(Level level, Map<String, Object> message) throws AuditLoggingException {
+ try {
+ AuditLoggingAPIClient client = instance().apiClient;
+ if (client == null) {
+ // No can do - at least log to file
+ LOGGER.warn("Audit logging server not configured - printing event to log");
+ LOGGER.info(message.toString());
+ } else {
+ switch (level) {
+ case INFO:
+ client.log(message);
+ break;
+ case ERROR:
+ client.error(message);
+ break;
+ case TRACE:
+ client.trace(message);
+ break;
+ default:
+ break;
+ }
+ }
+ } catch (AuditLoggingException e) {
+ // Just for debugging purposes
+ LOGGER.error("Could not send audit event {} with level {}", message, level, e);
+ // log this locally to a file just in case
+ AuditLogging.log("local", message);
+ throw e;
+ }
+ }
+}