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.couchdb; 018 019import java.lang.reflect.Method; 020 021import org.apache.logging.log4j.Logger; 022import org.apache.logging.log4j.core.config.plugins.Plugin; 023import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 024import org.apache.logging.log4j.core.config.plugins.PluginFactory; 025import org.apache.logging.log4j.core.config.plugins.convert.TypeConverters; 026import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost; 027import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort; 028import org.apache.logging.log4j.core.util.NameUtil; 029import org.apache.logging.log4j.core.appender.nosql.NoSqlProvider; 030import org.apache.logging.log4j.status.StatusLogger; 031import org.apache.logging.log4j.util.LoaderUtil; 032import org.apache.logging.log4j.util.Strings; 033import org.lightcouch.CouchDbClient; 034import org.lightcouch.CouchDbProperties; 035 036/** 037 * The Apache CouchDB implementation of {@link NoSqlProvider}. 038 */ 039@Plugin(name = "CouchDB", category = "Core", printObject = true) 040public final class CouchDbProvider implements NoSqlProvider<CouchDbConnection> { 041 private static final int HTTP = 80; 042 private static final int HTTPS = 443; 043 private static final Logger LOGGER = StatusLogger.getLogger(); 044 045 private final CouchDbClient client; 046 private final String description; 047 048 private CouchDbProvider(final CouchDbClient client, final String description) { 049 this.client = client; 050 this.description = "couchDb{ " + description + " }"; 051 } 052 053 @Override 054 public CouchDbConnection getConnection() { 055 return new CouchDbConnection(this.client); 056 } 057 058 @Override 059 public String toString() { 060 return this.description; 061 } 062 063 /** 064 * Factory method for creating an Apache CouchDB provider within the plugin manager. 065 * 066 * @param databaseName The name of the database to which log event documents will be written. 067 * @param protocol Either "http" or "https," defaults to "http" and mutually exclusive with 068 * {@code factoryClassName&factoryMethodName!=null}. 069 * @param server The host name of the CouchDB server, defaults to localhost and mutually exclusive with 070 * {@code factoryClassName&factoryMethodName!=null}. 071 * @param port The port that CouchDB is listening on, defaults to 80 if {@code protocol} is "http" and 443 if 072 * {@code protocol} is "https," and mutually exclusive with 073 * {@code factoryClassName&factoryMethodName!=null}. 074 * @param username The username to authenticate against the MongoDB server with, mutually exclusive with 075 * {@code factoryClassName&factoryMethodName!=null}. 076 * @param password The password to authenticate against the MongoDB server with, mutually exclusive with 077 * {@code factoryClassName&factoryMethodName!=null}. 078 * @param factoryClassName A fully qualified class name containing a static factory method capable of returning a 079 * {@link CouchDbClient} or {@link CouchDbProperties}. 080 * @param factoryMethodName The name of the public static factory method belonging to the aforementioned factory 081 * class. 082 * @return a new Apache CouchDB provider. 083 */ 084 @PluginFactory 085 public static CouchDbProvider createNoSqlProvider( 086 @PluginAttribute("databaseName") final String databaseName, 087 @PluginAttribute("protocol") String protocol, 088 @PluginAttribute(value = "server", defaultString = "localhost") @ValidHost final String server, 089 @PluginAttribute(value = "port", defaultString = "0") @ValidPort final String port, 090 @PluginAttribute("username") final String username, 091 @PluginAttribute(value = "password", sensitive = true) final String password, 092 @PluginAttribute("factoryClassName") final String factoryClassName, 093 @PluginAttribute("factoryMethodName") final String factoryMethodName) { 094 CouchDbClient client; 095 String description; 096 if (Strings.isNotEmpty(factoryClassName) && Strings.isNotEmpty(factoryMethodName)) { 097 try { 098 final Class<?> factoryClass = LoaderUtil.loadClass(factoryClassName); 099 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}