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