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