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 org.apache.logging.log4j.Logger;
20 import org.apache.logging.log4j.core.appender.nosql.NoSqlProvider;
21 import org.apache.logging.log4j.core.config.plugins.Plugin;
22 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
23 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
24 import org.apache.logging.log4j.core.config.plugins.convert.TypeConverters;
25 import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
26 import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
27 import org.apache.logging.log4j.status.StatusLogger;
28 import org.apache.logging.log4j.util.LoaderUtil;
29 import org.apache.logging.log4j.util.Strings;
30 import org.lightcouch.CouchDbClient;
31 import org.lightcouch.CouchDbProperties;
32
33 import java.lang.reflect.Method;
34
35
36
37
38 @Plugin(name = "CouchDB", category = "Core", printObject = true)
39 public final class CouchDbProvider implements NoSqlProvider<CouchDbConnection> {
40 private static final int HTTP = 80;
41 private static final int HTTPS = 443;
42 private static final Logger LOGGER = StatusLogger.getLogger();
43
44 private final CouchDbClient client;
45 private final String description;
46
47 private CouchDbProvider(final CouchDbClient client, final String description) {
48 this.client = client;
49 this.description = "couchDb{ " + description + " }";
50 }
51
52 @Override
53 public CouchDbConnection getConnection() {
54 return new CouchDbConnection(this.client);
55 }
56
57 @Override
58 public String toString() {
59 return this.description;
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 @PluginFactory
84 public static CouchDbProvider createNoSqlProvider(
85 @PluginAttribute("databaseName") final String databaseName,
86 @PluginAttribute("protocol") String protocol,
87 @PluginAttribute(value = "server", defaultString = "localhost") @ValidHost final String server,
88 @PluginAttribute(value = "port", defaultString = "0") @ValidPort final String port,
89 @PluginAttribute("username") final String username,
90 @PluginAttribute(value = "password", sensitive = true) final String password,
91 @PluginAttribute("factoryClassName") final String factoryClassName,
92 @PluginAttribute("factoryMethodName") final String factoryMethodName) {
93 CouchDbClient client;
94 String description;
95 if (Strings.isNotEmpty(factoryClassName) && Strings.isNotEmpty(factoryMethodName)) {
96 try {
97 final Class<?> factoryClass = LoaderUtil.loadClass(factoryClassName);
98 final Method method = factoryClass.getMethod(factoryMethodName);
99 final Object object = method.invoke(null);
100
101 if (object instanceof CouchDbClient) {
102 client = (CouchDbClient) object;
103 description = "uri=" + client.getDBUri();
104 } else if (object instanceof CouchDbProperties) {
105 final CouchDbProperties properties = (CouchDbProperties) object;
106 client = new CouchDbClient(properties);
107 description = "uri=" + client.getDBUri() + ", username=" + properties.getUsername()
108 + ", maxConnections=" + properties.getMaxConnections() + ", connectionTimeout="
109 + properties.getConnectionTimeout() + ", socketTimeout=" + properties.getSocketTimeout();
110 } else {
111 if (object == null) {
112 LOGGER.error("The factory method [{}.{}()] returned null.", factoryClassName, factoryMethodName);
113 } else {
114 LOGGER.error("The factory method [{}.{}()] returned an unsupported type [{}].", factoryClassName,
115 factoryMethodName, object.getClass().getName());
116 }
117 return null;
118 }
119 } catch (final ClassNotFoundException e) {
120 LOGGER.error("The factory class [{}] could not be loaded.", factoryClassName, e);
121 return null;
122 } catch (final NoSuchMethodException e) {
123 LOGGER.error("The factory class [{}] does not have a no-arg method named [{}].", factoryClassName,
124 factoryMethodName, e);
125 return null;
126 } catch (final Exception e) {
127 LOGGER.error("The factory method [{}.{}()] could not be invoked.", factoryClassName, factoryMethodName,
128 e);
129 return null;
130 }
131 } else if (Strings.isNotEmpty(databaseName)) {
132 if (protocol != null && protocol.length() > 0) {
133 protocol = protocol.toLowerCase();
134 if (!protocol.equals("http") && !protocol.equals("https")) {
135 LOGGER.error("Only protocols [http] and [https] are supported, [{}] specified.", protocol);
136 return null;
137 }
138 } else {
139 protocol = "http";
140 LOGGER.warn("No protocol specified, using default port [http].");
141 }
142
143 final int portInt = TypeConverters.convert(port, int.class, protocol.equals("https") ? HTTPS : HTTP);
144
145 if (Strings.isEmpty(username) || Strings.isEmpty(password)) {
146 LOGGER.error("You must provide a username and password for the CouchDB provider.");
147 return null;
148 }
149
150 client = new CouchDbClient(databaseName, false, protocol, server, portInt, username, password);
151 description = "uri=" + client.getDBUri() + ", username=" + username;
152 } else {
153 LOGGER.error("No factory method was provided so the database name is required.");
154 return null;
155 }
156
157 return new CouchDbProvider(client, description);
158 }
159 }