Github user codescale commented on a diff in the pull request:

    https://github.com/apache/logging-log4j2/pull/62#discussion_r126504038
  
    --- Diff: 
log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbProvider.java
 ---
    @@ -93,130 +103,249 @@ public String toString() {
          * @param factoryMethodName The name of the public static factory 
method belonging to the aforementioned factory
          *                          class.
          * @return a new MongoDB provider.
    +     * @deprecated in 2.8; use {@link #newBuilder()} instead.
          */
         @PluginFactory
         public static MongoDbProvider createNoSqlProvider(
    -            @PluginAttribute("collectionName") final String collectionName,
    -            @PluginAttribute("writeConcernConstant") final String 
writeConcernConstant,
    -            @PluginAttribute("writeConcernConstantClass") final String 
writeConcernConstantClassName,
    -            @PluginAttribute("databaseName") final String databaseName,
    -            @PluginAttribute(value = "server", defaultString = 
"localhost") @ValidHost final String server,
    -            @PluginAttribute(value = "port", defaultString = "" + 
DEFAULT_PORT) @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) {
    -        DB database;
    -        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 DB) {
    -                    database = (DB) object;
    -                } else if (object instanceof MongoClient) {
    -                    if (Strings.isNotEmpty(databaseName)) {
    -                        database = ((MongoClient) 
object).getDB(databaseName);
    -                    } else {
    -                        LOGGER.error("The factory method [{}.{}()] 
returned a MongoClient so the database name is "
    -                                + "required.", factoryClassName, 
factoryMethodName);
    -                        return null;
    -                    }
    -                } 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;
    -                }
    -
    -                description = "database=" + database.getName();
    -                final List<ServerAddress> addresses = 
database.getMongo().getAllAddress();
    -                if (addresses.size() == 1) {
    -                    description += ", server=" + 
addresses.get(0).getHost() + ", port=" + addresses.get(0).getPort();
    -                } else {
    -                    description += ", servers=[";
    -                    for (final ServerAddress address : addresses) {
    -                        description += " { " + address.getHost() + ", " + 
address.getPort() + " } ";
    -                    }
    -                    description += "]";
    -                }
    -            } 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)) {
    -            final List<MongoCredential> credentials = new ArrayList<>();
    -            description = "database=" + databaseName;
    -            if (Strings.isNotEmpty(userName) && 
Strings.isNotEmpty(password)) {
    -                description += ", username=" + userName + ", passwordHash="
    -                        + NameUtil.md5(password + 
MongoDbProvider.class.getName());
    -                credentials.add(MongoCredential.createCredential(userName, 
databaseName, password.toCharArray()));
    -            }
    -            try {
    -                final int portInt = TypeConverters.convert(port, 
int.class, DEFAULT_PORT);
    -                description += ", server=" + server + ", port=" + portInt;
    -                database = new MongoClient(new ServerAddress(server, 
portInt), credentials).getDB(databaseName);
    -            } catch (final Exception e) {
    -                LOGGER.error(
    -                        "Failed to obtain a database instance from the 
MongoClient at server [{}] and " + "port [{}].",
    -                        server, port);
    -                return null;
    -            }
    -        } else {
    -            LOGGER.error("No factory method was provided so the database 
name is required.");
    -            return null;
    -        }
    -
    -        try {
    -            database.getCollectionNames(); // Check if the database 
actually requires authentication
    -        } catch (final Exception e) {
    -            LOGGER.error(
    -                    "The database is not up, or you are not authenticated, 
try supplying a username and password to the MongoDB provider.",
    -                    e);
    -            return null;
    -        }
    -
    -        final WriteConcern writeConcern = 
toWriteConcern(writeConcernConstant, writeConcernConstantClassName);
    -
    -        return new MongoDbProvider(database, writeConcern, collectionName, 
description);
    -    }
    +            final String collectionName,
    +            final String writeConcernConstant,
    +            final String writeConcernConstantClassName,
    +            final String databaseName,
    +            final String server,
    +            final String port,
    +            final String userName,
    +            final String password,
    +            final String factoryClassName,
    +                   final String factoryMethodName) {
    +           LOGGER.info("createNoSqlProvider");
    +           return 
newBuilder().setCollectionName(collectionName).setWriteConcernConstant(writeConcernConstantClassName)
    +                           
.setWriteConcernConstant(writeConcernConstant).setDatabaseName(databaseName).setServer(server)
    +                           
.setPort(port).setUserName(userName).setPassword(password).setFactoryClassName(factoryClassName)
    +                           
.setFactoryMethodName(factoryMethodName).build();
    +   }
    +
    +   @PluginBuilderFactory
    +   public static <B extends Builder<B>> B newBuilder() {
    +           return new Builder<B>().asBuilder();
    +   }
    +
    +   public static class Builder<B extends Builder<B>> extends 
AbstractFilterable.Builder<B>
    +                   implements 
org.apache.logging.log4j.core.util.Builder<MongoDbProvider> {
    +
    +           @PluginBuilderAttribute
    +           @ValidHost
    +           private String server = "localhost";
    +
    +           @PluginBuilderAttribute
    +           @ValidPort
    +           private String port = "" + DEFAULT_PORT;
    --- End diff --
    
    
https://github.com/codescale/logging-log4j2/blob/master/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbProvider.java#L141


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to