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     */
017    package org.apache.logging.log4j.core.appender.db.nosql.couch;
018    
019    import java.util.Map;
020    
021    import org.apache.logging.log4j.Logger;
022    import org.apache.logging.log4j.core.appender.db.nosql.NoSQLConnection;
023    import org.apache.logging.log4j.core.appender.db.nosql.NoSQLObject;
024    import org.apache.logging.log4j.status.StatusLogger;
025    import org.lightcouch.CouchDbClient;
026    import org.lightcouch.Response;
027    
028    /**
029     * The Apache CouchDB implementation of {@link NoSQLConnection}.
030     */
031    public final class CouchDBConnection implements NoSQLConnection<Map<String, Object>, CouchDBObject> {
032        private static final Logger LOGGER = StatusLogger.getLogger();
033    
034        private final CouchDbClient client;
035        private boolean closed = false;
036    
037        public CouchDBConnection(final CouchDbClient client) {
038            this.client = client;
039        }
040    
041        @Override
042        public CouchDBObject createObject() {
043            return new CouchDBObject();
044        }
045    
046        @Override
047        public CouchDBObject[] createList(final int length) {
048            return new CouchDBObject[length];
049        }
050    
051        @Override
052        public void insertObject(final NoSQLObject<Map<String, Object>> object) {
053            try {
054                final Response response = this.client.save(object.unwrap());
055                if (response.getError() != null && response.getError().length() > 0) {
056                    LOGGER.error("Failed to write log event to CouchDB due to error: [{}].", response.getError());
057                }
058            } catch (final Exception e) {
059                LOGGER.error("Failed to write log event to CouchDB due to error.", e);
060            }
061        }
062    
063        @Override
064        public synchronized void close() {
065            this.closed = true;
066            this.client.shutdown();
067        }
068    
069        @Override
070        public synchronized boolean isClosed() {
071            return this.closed;
072        }
073    }