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.lang.reflect.Method;
    20
    21  import org.apache.logging.log4j.Logger;
    22  import org.apache.logging.log4j.core.appender.AbstractAppender;
    23  import org.apache.logging.log4j.core.config.plugins.Plugin;
    24  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
    25  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
    26  import org.apache.logging.log4j.core.util.Loader;
    27  import org.apache.logging.log4j.core.util.NameUtil;
    28  import org.apache.logging.log4j.nosql.appender.NoSqlProvider;
    29  import org.apache.logging.log4j.status.StatusLogger;
    30  import org.apache.logging.log4j.util.Strings;
    31  import org.lightcouch.CouchDbClient;
    32  import org.lightcouch.CouchDbProperties;
    33
    34  /**
    35   * The Apache CouchDB implementation of {@link NoSqlProvider}.
    36   */
    37  @Plugin(name = "CouchDB", category = "Core", printObject = true)
    38  public final class CouchDbProvider implements NoSqlProvider<CouchDbConnection> {
    39      private static final int HTTP = 80;
    40      private static final int HTTPS = 443;
    41      private static final Logger LOGGER = StatusLogger.getLogger();
    42
    43      private final CouchDbClient client;
    44      private final String description;
    45
    46      private CouchDbProvider(final CouchDbClient client, final String description) {
    47          this.client = client;
    48          this.description = "couchDb{ " + description + " }";
    49      }
    50
    51      @Override
    52      public CouchDbConnection getConnection() {
    53          return new CouchDbConnection(this.client);
    54      }
    55
    56      @Override
    57      public String toString() {
    58          return this.description;
    59      }
    60
    61      /**
    62       * Factory method for creating an Apache CouchDB provider within the plugin manager.
    63       *
    64       * @param databaseName The name of the database to which log event documents will be written.
    65       * @param protocol Either "http" or "https," defaults to "http" and mutually exclusive with
    66       *                 {@code factoryClassName&factoryMethodName!=null}.
    67       * @param server The host name of the CouchDB server, defaults to localhost and mutually exclusive with
    68       *               {@code factoryClassName&factoryMethodName!=null}.
    69       * @param port The port that CouchDB is listening on, defaults to 80 if {@code protocol} is "http" and 443 if
    70       *             {@code protocol} is "https," and mutually exclusive with
    71       *             {@code factoryClassName&factoryMethodName!=null}.
    72       * @param username The username to authenticate against the MongoDB server with, mutually exclusive with
    73       *                 {@code factoryClassName&factoryMethodName!=null}.
    74       * @param password The password to authenticate against the MongoDB server with, mutually exclusive with
    75       *                 {@code factoryClassName&factoryMethodName!=null}.
    76       * @param factoryClassName A fully qualified class name containing a static factory method capable of returning a
    77       *                         {@link CouchDbClient} or {@link CouchDbProperties}.
    78       * @param factoryMethodName The name of the public static factory method belonging to the aforementioned factory
    79       *                          class.
    80       * @return a new Apache CouchDB provider.
    81       */
    82      @PluginFactory
    83      public static CouchDbProvider createNoSqlProvider(
    84              @PluginAttribute("databaseName") final String databaseName,
    85              @PluginAttribute("protocol") String protocol,
    86              @PluginAttribute("server") String server,
    87              @PluginAttribute("port") final String port,
    88              @PluginAttribute("username") final String username,
    89              @PluginAttribute(value = "password", sensitive = true) final String password,
    90              @PluginAttribute("factoryClassName") final String factoryClassName,
    91              @PluginAttribute("factoryMethodName") final String factoryMethodName) {
    92          CouchDbClient client;
    93          String description;
    94          if (factoryClassName != null && factoryClassName.length() > 0 &&
    95                  factoryMethodName != null && factoryMethodName.length() > 0) {
    96              try {
    97                  final Class<?> factoryClass = Loader.loadClass(factoryClassName);
    98                  final Method method = factoryClass.getMethod(factoryMethodName);
    99                  final Object object = method.invoke(null);
    100
    101                 if (object instanceof CouchDbClient) {
    102                     client = (CouchDbClient) object;
    103                     description = "uri=" + client.getDBUri();
    104                 } else if (object instanceof CouchDbProperties) {
    105                     final CouchDbProperties properties = (CouchDbProperties) object;
    106                     client = new CouchDbClient(properties);
    107                     description = "uri=" + client.getDBUri() + ", username=" + properties.getUsername()
    108                             + ", passwordHash=" + NameUtil.md5(password + CouchDbProvider.class.getName())
    109                             + ", maxConnections=" + properties.getMaxConnections() + ", connectionTimeout="
    110                             + properties.getConnectionTimeout() + ", socketTimeout=" + properties.getSocketTimeout();
    111                 } else if (object == null) {
    112                     LOGGER.error("The factory method [{}.{}()] returned null.", factoryClassName, factoryMethodName);
    113                     return null;
    114                 } else {
    115                     LOGGER.error("The factory method [{}.{}()] returned an unsupported type [{}].", factoryClassName,
    116                             factoryMethodName, object.getClass().getName());
    117                     return null;
    118                 }
    119             } catch (final ClassNotFoundException e) {
    120                 LOGGER.error("The factory class [{}] could not be loaded.", factoryClassName, e);
    121                 return null;
    122             } catch (final NoSuchMethodException e) {
    123                 LOGGER.error("The factory class [{}] does not have a no-arg method named [{}].", factoryClassName,
    124                         factoryMethodName, e);
    125                 return null;
    126             } catch (final Exception e) {
    127                 LOGGER.error("The factory method [{}.{}()] could not be invoked.", factoryClassName, factoryMethodName,
    128                         e);
    129                 return null;
    130             }
    131         } else if (databaseName != null && databaseName.length() > 0) {
    132             if (protocol != null && protocol.length() > 0) {
    133                 protocol = protocol.toLowerCase();
    134                 if (!protocol.equals("http") && !protocol.equals("https")) {
    135                     LOGGER.error("Only protocols [http] and [https] are supported, [{}] specified.", protocol);
    136                     return null;
    137                 }
    138             } else {
    139                 protocol = "http";
    140                 LOGGER.warn("No protocol specified, using default port [http].");
    141             }
    142
    143             final int portInt = AbstractAppender.parseInt(port, protocol.equals("https") ? HTTPS : HTTP);
    144
    145             if (Strings.isEmpty(server)) {
    146                 server = "localhost";
    147                 LOGGER.warn("No server specified, using default server localhost.");
    148             }
    149
    150             if (Strings.isEmpty(username) || Strings.isEmpty(password)) {
    151                 LOGGER.error("You must provide a username and password for the CouchDB provider.");
    152                 return null;
    153             }
    154
    155             client = new CouchDbClient(databaseName, false, protocol, server, portInt, username, password);
    156             description = "uri=" + client.getDBUri() + ", username=" + username + ", passwordHash="
    157                     + NameUtil.md5(password + CouchDbProvider.class.getName());
    158         } else {
    159             LOGGER.error("No factory method was provided so the database name is required.");
    160             return null;
    161         }
    162
    163         return new CouchDbProvider(client, description);
    164     }
    165 }