001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.mongodb;
018
019import org.apache.logging.log4j.Level;
020import org.apache.logging.log4j.Logger;
021import org.apache.logging.log4j.core.appender.AppenderLoggingException;
022import org.apache.logging.log4j.core.appender.nosql.AbstractNoSqlConnection;
023import org.apache.logging.log4j.core.appender.nosql.NoSqlConnection;
024import org.apache.logging.log4j.core.appender.nosql.NoSqlObject;
025import org.apache.logging.log4j.status.StatusLogger;
026import org.bson.BSON;
027import org.bson.Transformer;
028
029import com.mongodb.BasicDBObject;
030import com.mongodb.DB;
031import com.mongodb.DBCollection;
032import com.mongodb.MongoException;
033import com.mongodb.WriteConcern;
034
035/**
036 * The MongoDB implementation of {@link NoSqlConnection}.
037 */
038public final class MongoDbConnection extends AbstractNoSqlConnection<BasicDBObject, MongoDbObject> {
039
040    private static final Logger LOGGER = StatusLogger.getLogger();
041
042    static {
043        BSON.addEncodingHook(Level.class, new Transformer() {
044            @Override
045            public Object transform(final Object o) {
046                if (o instanceof Level) {
047                    return ((Level) o).name();
048                }
049                return o;
050            }
051        });
052    }
053
054    private final DBCollection collection;
055    private final WriteConcern writeConcern;
056
057    public MongoDbConnection(final DB database, final WriteConcern writeConcern, final String collectionName,
058            final Boolean isCapped, final Integer collectionSize) {
059        if (database.collectionExists(collectionName)) {
060            collection = database.getCollection(collectionName);
061        } else {
062            final BasicDBObject options = new BasicDBObject();
063            options.put("capped", isCapped);
064            options.put("size", collectionSize);
065            this.collection = database.createCollection(collectionName, options);
066        }
067        this.writeConcern = writeConcern;
068    }
069
070    @Override
071    public MongoDbObject createObject() {
072        return new MongoDbObject();
073    }
074
075    @Override
076    public MongoDbObject[] createList(final int length) {
077        return new MongoDbObject[length];
078    }
079
080    @Override
081    public void insertObject(final NoSqlObject<BasicDBObject> object) {
082        try {
083            this.collection.insert(object.unwrap(), this.writeConcern);
084        } catch (final MongoException e) {
085            throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(),
086                    e);
087        }
088    }
089
090    @Override
091    public void closeImpl() {
092        // LOG4J2-1196
093        this.collection.getDB().getMongo().close();
094    }
095
096}