1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.nosql.appender.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.nosql.appender.NoSqlConnection;
23 import org.apache.logging.log4j.nosql.appender.NoSqlObject;
24 import org.apache.logging.log4j.status.StatusLogger;
25 import org.apache.logging.log4j.util.Strings;
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(final 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
93
94
95 }
96
97 @Override
98 public boolean isClosed() {
99 return !this.mongo.getConnector().isOpen();
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114 static void authenticate(final DB database, final String username, final String password) {
115 try {
116 if (!database.authenticate(username, password.toCharArray())) {
117 LOGGER.error("Failed to authenticate against MongoDB server. Unknown error.");
118 }
119 } catch (final MongoException e) {
120 LOGGER.error("Failed to authenticate against MongoDB: " + e.getMessage(), e);
121 } catch (final IllegalStateException e) {
122 LOGGER.error("Factory-supplied MongoDB database connection already authenticated with different" +
123 "credentials but lost connection.", e);
124 }
125 }
126 }