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.core.appender.db.nosql.couchdb;
018
019import java.lang.reflect.Method;
020
021import org.apache.logging.log4j.Logger;
022import org.apache.logging.log4j.core.appender.AbstractAppender;
023import org.apache.logging.log4j.core.appender.db.nosql.NoSQLProvider;
024import org.apache.logging.log4j.core.config.plugins.Plugin;
025import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
026import org.apache.logging.log4j.core.config.plugins.PluginFactory;
027import org.apache.logging.log4j.core.helpers.NameUtil;
028import org.apache.logging.log4j.core.helpers.Strings;
029import org.apache.logging.log4j.status.StatusLogger;
030import org.lightcouch.CouchDbClient;
031import org.lightcouch.CouchDbProperties;
032
033/**
034 * The Apache CouchDB implementation of {@link NoSQLProvider}.
035 */
036@Plugin(name = "CouchDB", category = "Core", printObject = true)
037public final class CouchDBProvider implements NoSQLProvider<CouchDBConnection> {
038    private static final int HTTP = 80;
039    private static final int HTTPS = 443;
040    private static final Logger LOGGER = StatusLogger.getLogger();
041
042    private final CouchDbClient client;
043    private final String description;
044
045    private CouchDBProvider(final CouchDbClient client, final String description) {
046        this.client = client;
047        this.description = "couchDb{ " + description + " }";
048    }
049
050    @Override
051    public CouchDBConnection getConnection() {
052        return new CouchDBConnection(this.client);
053    }
054
055    @Override
056    public String toString() {
057        return this.description;
058    }
059
060    /**
061     * Factory method for creating an Apache CouchDB provider within the plugin manager.
062     *
063     * @param databaseName The name of the database to which log event documents will be written.
064     * @param protocol Either "http" or "https," defaults to "http" and mutually exclusive with
065     *                 {@code factoryClassName&factoryMethodName!=null}.
066     * @param server The host name of the CouchDB server, defaults to localhost and mutually exclusive with
067     *               {@code factoryClassName&factoryMethodName!=null}.
068     * @param port The port that CouchDB is listening on, defaults to 80 if {@code protocol} is "http" and 443 if
069     *             {@code protocol} is "https," and mutually exclusive with
070     *             {@code factoryClassName&factoryMethodName!=null}.
071     * @param username The username to authenticate against the MongoDB server with, mutually exclusive with
072     *                 {@code factoryClassName&factoryMethodName!=null}.
073     * @param password The password to authenticate against the MongoDB server with, mutually exclusive with
074     *                 {@code factoryClassName&factoryMethodName!=null}.
075     * @param factoryClassName A fully qualified class name containing a static factory method capable of returning a
076     *                         {@link CouchDbClient} or {@link CouchDbProperties}.
077     * @param factoryMethodName The name of the public static factory method belonging to the aforementioned factory
078     *                          class.
079     * @return a new Apache CouchDB provider.
080     */
081    @PluginFactory
082    public static CouchDBProvider createNoSQLProvider(
083            @PluginAttribute("databaseName") final String databaseName,
084            @PluginAttribute("protocol") String protocol,
085            @PluginAttribute("server") String server,
086            @PluginAttribute("port") final String port,
087            @PluginAttribute("username") final String username,
088            @PluginAttribute("password") final String password,
089            @PluginAttribute("factoryClassName") final String factoryClassName,
090            @PluginAttribute("factoryMethodName") final String factoryMethodName) {
091        CouchDbClient client;
092        String description;
093        if (factoryClassName != null && factoryClassName.length() > 0 &&
094                factoryMethodName != null && factoryMethodName.length() > 0) {
095            try {
096                final Class<?> factoryClass = Class.forName(factoryClassName);
097                final Method method = factoryClass.getMethod(factoryMethodName);
098                final Object object = method.invoke(null);
099
100                if (object instanceof CouchDbClient) {
101                    client = (CouchDbClient) object;
102                    description = "uri=" + client.getDBUri();
103                } else if (object instanceof CouchDbProperties) {
104                    final CouchDbProperties properties = (CouchDbProperties) object;
105                    client = new CouchDbClient(properties);
106                    description = "uri=" + client.getDBUri() + ", username=" + properties.getUsername()
107                            + ", passwordHash=" + NameUtil.md5(password + CouchDBProvider.class.getName())
108                            + ", maxConnections=" + properties.getMaxConnections() + ", connectionTimeout="
109                            + properties.getConnectionTimeout() + ", socketTimeout=" + properties.getSocketTimeout();
110                } else if (object == null) {
111                    LOGGER.error("The factory method [{}.{}()] returned null.", factoryClassName, factoryMethodName);
112                    return null;
113                } else {
114                    LOGGER.error("The factory method [{}.{}()] returned an unsupported type [{}].", factoryClassName,
115                            factoryMethodName, object.getClass().getName());
116                    return null;
117                }
118            } catch (final ClassNotFoundException e) {
119                LOGGER.error("The factory class [{}] could not be loaded.", factoryClassName, e);
120                return null;
121            } catch (final NoSuchMethodException e) {
122                LOGGER.error("The factory class [{}] does not have a no-arg method named [{}].", factoryClassName,
123                        factoryMethodName, e);
124                return null;
125            } catch (final Exception e) {
126                LOGGER.error("The factory method [{}.{}()] could not be invoked.", factoryClassName, factoryMethodName,
127                        e);
128                return null;
129            }
130        } else if (databaseName != null && databaseName.length() > 0) {
131            if (protocol != null && protocol.length() > 0) {
132                protocol = protocol.toLowerCase();
133                if (!protocol.equals("http") && !protocol.equals("https")) {
134                    LOGGER.error("Only protocols [http] and [https] are supported, [{}] specified.", protocol);
135                    return null;
136                }
137            } else {
138                protocol = "http";
139                LOGGER.warn("No protocol specified, using default port [http].");
140            }
141
142            final int portInt = AbstractAppender.parseInt(port, protocol.equals("https") ? HTTPS : HTTP);
143
144            if (Strings.isEmpty(server)) {
145                server = "localhost";
146                LOGGER.warn("No server specified, using default server localhost.");
147            }
148
149            if (Strings.isEmpty(username) || Strings.isEmpty(password)) {
150                LOGGER.error("You must provide a username and password for the CouchDB provider.");
151                return null;
152            }
153
154            client = new CouchDbClient(databaseName, false, protocol, server, portInt, username, password);
155            description = "uri=" + client.getDBUri() + ", username=" + username + ", passwordHash="
156                    + NameUtil.md5(password + CouchDBProvider.class.getName());
157        } else {
158            LOGGER.error("No factory method was provided so the database name is required.");
159            return null;
160        }
161
162        return new CouchDBProvider(client, description);
163    }
164}