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.mongodb;
18
19 import org.apache.logging.log4j.Level;
20 import org.apache.logging.log4j.Logger;
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.apache.logging.log4j.core.helpers.Strings;
25 import org.apache.logging.log4j.status.StatusLogger;
26 import org.bson.BSON;
27 import org.bson.Transformer;
28
29 import com.mongodb.BasicDBObject;
30 import com.mongodb.DB;
31 import com.mongodb.DBCollection;
32 import com.mongodb.Mongo;
33 import com.mongodb.MongoException;
34 import com.mongodb.WriteConcern;
35 import com.mongodb.WriteResult;
36
37
38
39
40 public final class MongoDBConnection implements NoSQLConnection<BasicDBObject, MongoDBObject> {
41
42 private static final Logger LOGGER = StatusLogger.getLogger();
43
44 static {
45 BSON.addEncodingHook(Level.class, new Transformer() {
46 @Override
47 public Object transform(Object o) {
48 if (o instanceof Level) {
49 return ((Level) o).name();
50 }
51 return o;
52 }
53 });
54 }
55
56 private final DBCollection collection;
57 private final Mongo mongo;
58 private final WriteConcern writeConcern;
59
60 public MongoDBConnection(final DB database, final WriteConcern writeConcern, final String collectionName) {
61 this.mongo = database.getMongo();
62 this.collection = database.getCollection(collectionName);
63 this.writeConcern = writeConcern;
64 }
65
66 @Override
67 public MongoDBObject createObject() {
68 return new MongoDBObject();
69 }
70
71 @Override
72 public MongoDBObject[] createList(final int length) {
73 return new MongoDBObject[length];
74 }
75
76 @Override
77 public void insertObject(final NoSQLObject<BasicDBObject> object) {
78 try {
79 final WriteResult result = this.collection.insert(object.unwrap(), this.writeConcern);
80 if (Strings.isNotEmpty(result.getError())) {
81 throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " +
82 result.getError() + ".");
83 }
84 } catch (final MongoException e) {
85 throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(),
86 e);
87 }
88 }
89
90 @Override
91 public void close() {
92 this.mongo.close();
93 }
94
95 @Override
96 public boolean isClosed() {
97 return !this.mongo.getConnector().isOpen();
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112 static void authenticate(final DB database, final String username, final String password) {
113 try {
114 if (!database.authenticate(username, password.toCharArray())) {
115 LOGGER.error("Failed to authenticate against MongoDB server. Unknown error.");
116 }
117 } catch (final MongoException e) {
118 LOGGER.error("Failed to authenticate against MongoDB: " + e.getMessage(), e);
119 } catch (final IllegalStateException e) {
120 LOGGER.error("Factory-supplied MongoDB database connection already authenticated with different" +
121 "credentials but lost connection.");
122 }
123 }
124 }