View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender.db.nosql.mongo;
18  
19  import org.apache.logging.log4j.Logger;
20  import org.apache.logging.log4j.core.appender.db.nosql.NoSQLConnection;
21  import org.apache.logging.log4j.core.appender.db.nosql.NoSQLObject;
22  import org.apache.logging.log4j.status.StatusLogger;
23  
24  import com.mongodb.BasicDBObject;
25  import com.mongodb.DB;
26  import com.mongodb.DBCollection;
27  import com.mongodb.Mongo;
28  import com.mongodb.MongoException;
29  import com.mongodb.WriteConcern;
30  import com.mongodb.WriteResult;
31  
32  /**
33   * The MongoDB implementation of {@link NoSQLConnection}.
34   */
35  public final class MongoDBConnection implements NoSQLConnection<BasicDBObject, MongoDBObject> {
36      private static final Logger LOGGER = StatusLogger.getLogger();
37  
38      private final DBCollection collection;
39      private final Mongo mongo;
40      private final WriteConcern writeConcern;
41  
42      public MongoDBConnection(final DB database, final WriteConcern writeConcern, final String collectionName) {
43          this.mongo = database.getMongo();
44          this.collection = database.getCollection(collectionName);
45          this.writeConcern = writeConcern;
46      }
47  
48      @Override
49      public MongoDBObject createObject() {
50          return new MongoDBObject();
51      }
52  
53      @Override
54      public MongoDBObject[] createList(final int length) {
55          return new MongoDBObject[length];
56      }
57  
58      @Override
59      public void insertObject(final NoSQLObject<BasicDBObject> object) {
60          try {
61              final WriteResult result = this.collection.insert(object.unwrap(), this.writeConcern);
62              if (result.getN() < 1) {
63                  LOGGER.error("Failed to write log event to MongoDB due to invalid result [{}].", result.getN());
64              }
65          } catch (final MongoException e) {
66              LOGGER.error("Failed to write log event to MongoDB due to error.", e);
67          }
68      }
69  
70      @Override
71      public void close() {
72          this.mongo.close();
73      }
74  
75      @Override
76      public boolean isClosed() {
77          return !this.mongo.getConnector().isOpen();
78      }
79  }