1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender.db.nosql.couchdb;
18
19 import java.util.Map;
20
21 import org.apache.logging.log4j.core.appender.AppenderLoggingException;
22 import org.apache.logging.log4j.core.appender.db.nosql.NoSQLConnection;
23 import org.apache.logging.log4j.core.appender.db.nosql.NoSQLObject;
24 import org.lightcouch.CouchDbClient;
25 import org.lightcouch.Response;
26
27
28
29
30 public final class CouchDBConnection implements NoSQLConnection<Map<String, Object>, CouchDBObject> {
31 private final CouchDbClient client;
32 private boolean closed = false;
33
34 public CouchDBConnection(final CouchDbClient client) {
35 this.client = client;
36 }
37
38 @Override
39 public CouchDBObject createObject() {
40 return new CouchDBObject();
41 }
42
43 @Override
44 public CouchDBObject[] createList(final int length) {
45 return new CouchDBObject[length];
46 }
47
48 @Override
49 public void insertObject(final NoSQLObject<Map<String, Object>> object) {
50 try {
51 final Response response = this.client.save(object.unwrap());
52 if (response.getError() != null && response.getError().length() > 0) {
53 throw new AppenderLoggingException("Failed to write log event to CouchDB due to error: " +
54 response.getError() + ".");
55 }
56 } catch (final Exception e) {
57 throw new AppenderLoggingException("Failed to write log event to CouchDB due to error: " + e.getMessage(),
58 e);
59 }
60 }
61
62 @Override
63 public synchronized void close() {
64 this.closed = true;
65 this.client.shutdown();
66 }
67
68 @Override
69 public synchronized boolean isClosed() {
70 return this.closed;
71 }
72 }