Search in sources :

Example 1 with CouchDbClient

use of org.lightcouch.CouchDbClient in project logging-log4j2 by apache.

the class CouchDbProvider method createNoSqlProvider.

/**
     * Factory method for creating an Apache CouchDB provider within the plugin manager.
     *
     * @param databaseName The name of the database to which log event documents will be written.
     * @param protocol Either "http" or "https," defaults to "http" and mutually exclusive with
     *                 {@code factoryClassName&factoryMethodName!=null}.
     * @param server The host name of the CouchDB server, defaults to localhost and mutually exclusive with
     *               {@code factoryClassName&factoryMethodName!=null}.
     * @param port The port that CouchDB is listening on, defaults to 80 if {@code protocol} is "http" and 443 if
     *             {@code protocol} is "https," and mutually exclusive with
     *             {@code factoryClassName&factoryMethodName!=null}.
     * @param username The username to authenticate against the MongoDB server with, mutually exclusive with
     *                 {@code factoryClassName&factoryMethodName!=null}.
     * @param password The password to authenticate against the MongoDB server with, mutually exclusive with
     *                 {@code factoryClassName&factoryMethodName!=null}.
     * @param factoryClassName A fully qualified class name containing a static factory method capable of returning a
     *                         {@link CouchDbClient} or {@link CouchDbProperties}.
     * @param factoryMethodName The name of the public static factory method belonging to the aforementioned factory
     *                          class.
     * @return a new Apache CouchDB provider.
     */
@PluginFactory
public static CouchDbProvider createNoSqlProvider(@PluginAttribute("databaseName") final String databaseName, @PluginAttribute("protocol") String protocol, @PluginAttribute(value = "server", defaultString = "localhost") @ValidHost String server, @PluginAttribute(value = "port", defaultString = "0") @ValidPort final String port, @PluginAttribute("username") final String username, @PluginAttribute(value = "password", sensitive = true) final String password, @PluginAttribute("factoryClassName") final String factoryClassName, @PluginAttribute("factoryMethodName") final String factoryMethodName) {
    CouchDbClient client;
    String description;
    if (Strings.isNotEmpty(factoryClassName) && Strings.isNotEmpty(factoryMethodName)) {
        try {
            final Class<?> factoryClass = LoaderUtil.loadClass(factoryClassName);
            final Method method = factoryClass.getMethod(factoryMethodName);
            final Object object = method.invoke(null);
            if (object instanceof CouchDbClient) {
                client = (CouchDbClient) object;
                description = "uri=" + client.getDBUri();
            } else if (object instanceof CouchDbProperties) {
                final CouchDbProperties properties = (CouchDbProperties) object;
                client = new CouchDbClient(properties);
                description = "uri=" + client.getDBUri() + ", username=" + properties.getUsername() + ", passwordHash=" + NameUtil.md5(password + CouchDbProvider.class.getName()) + ", maxConnections=" + properties.getMaxConnections() + ", connectionTimeout=" + properties.getConnectionTimeout() + ", socketTimeout=" + properties.getSocketTimeout();
            } else if (object == null) {
                LOGGER.error("The factory method [{}.{}()] returned null.", factoryClassName, factoryMethodName);
                return null;
            } else {
                LOGGER.error("The factory method [{}.{}()] returned an unsupported type [{}].", factoryClassName, factoryMethodName, object.getClass().getName());
                return null;
            }
        } catch (final ClassNotFoundException e) {
            LOGGER.error("The factory class [{}] could not be loaded.", factoryClassName, e);
            return null;
        } catch (final NoSuchMethodException e) {
            LOGGER.error("The factory class [{}] does not have a no-arg method named [{}].", factoryClassName, factoryMethodName, e);
            return null;
        } catch (final Exception e) {
            LOGGER.error("The factory method [{}.{}()] could not be invoked.", factoryClassName, factoryMethodName, e);
            return null;
        }
    } else if (Strings.isNotEmpty(databaseName)) {
        if (protocol != null && protocol.length() > 0) {
            protocol = protocol.toLowerCase();
            if (!protocol.equals("http") && !protocol.equals("https")) {
                LOGGER.error("Only protocols [http] and [https] are supported, [{}] specified.", protocol);
                return null;
            }
        } else {
            protocol = "http";
            LOGGER.warn("No protocol specified, using default port [http].");
        }
        final int portInt = TypeConverters.convert(port, int.class, protocol.equals("https") ? HTTPS : HTTP);
        if (Strings.isEmpty(username) || Strings.isEmpty(password)) {
            LOGGER.error("You must provide a username and password for the CouchDB provider.");
            return null;
        }
        client = new CouchDbClient(databaseName, false, protocol, server, portInt, username, password);
        description = "uri=" + client.getDBUri() + ", username=" + username + ", passwordHash=" + NameUtil.md5(password + CouchDbProvider.class.getName());
    } else {
        LOGGER.error("No factory method was provided so the database name is required.");
        return null;
    }
    return new CouchDbProvider(client, description);
}
Also used : CouchDbClient(org.lightcouch.CouchDbClient) CouchDbProperties(org.lightcouch.CouchDbProperties) Method(java.lang.reflect.Method) PluginFactory(org.apache.logging.log4j.core.config.plugins.PluginFactory)

Aggregations

Method (java.lang.reflect.Method)1 PluginFactory (org.apache.logging.log4j.core.config.plugins.PluginFactory)1 CouchDbClient (org.lightcouch.CouchDbClient)1 CouchDbProperties (org.lightcouch.CouchDbProperties)1