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.nosql.appender.couchdb;
18  
19  import java.util.Map;
20  import java.util.concurrent.atomic.AtomicBoolean;
21  
22  import org.apache.logging.log4j.core.appender.AppenderLoggingException;
23  import org.apache.logging.log4j.nosql.appender.DefaultNoSqlObject;
24  import org.apache.logging.log4j.nosql.appender.NoSqlConnection;
25  import org.apache.logging.log4j.nosql.appender.NoSqlObject;
26  import org.apache.logging.log4j.util.Strings;
27  import org.lightcouch.CouchDbClient;
28  import org.lightcouch.Response;
29  
30  /**
31   * The Apache CouchDB implementation of {@link NoSqlConnection}.
32   */
33  public final class CouchDbConnection implements NoSqlConnection<Map<String, Object>, DefaultNoSqlObject> {
34      private final CouchDbClient client;
35      private final AtomicBoolean closed = new AtomicBoolean(false);
36  
37      public CouchDbConnection(final CouchDbClient client) {
38          this.client = client;
39      }
40  
41      @Override
42      public DefaultNoSqlObject createObject() {
43          return new DefaultNoSqlObject();
44      }
45  
46      @Override
47      public DefaultNoSqlObject[] createList(final int length) {
48          return new DefaultNoSqlObject[length];
49      }
50  
51      @Override
52      public void insertObject(final NoSqlObject<Map<String, Object>> object) {
53          try {
54              final Response response = this.client.save(object.unwrap());
55              if (Strings.isNotEmpty(response.getError())) {
56                  throw new AppenderLoggingException("Failed to write log event to CouchDB due to error: " +
57                          response.getError() + '.');
58              }
59          } catch (final Exception e) {
60              throw new AppenderLoggingException("Failed to write log event to CouchDB due to error: " + e.getMessage(),
61                      e);
62          }
63      }
64  
65      @Override
66      public void close() {
67          if (this.closed.compareAndSet(false, true)) {
68              this.client.shutdown();
69          }
70      }
71  
72      @Override
73      public boolean isClosed() {
74          return this.closed.get();
75      }
76  }