1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 }