1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 (Strings.isNotEmpty(factoryClassName) && Strings.isNotEmpty(factoryMethodName)) {
95 try {
96 final Class<?> factoryClass = Loader.loadClass(factoryClassName);
97 final Method method = factoryClass.getMethod(factoryMethodName);
98 final Object object = method.invoke(null);
99
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 (Strings.isNotEmpty(databaseName)) {
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 }