Rewrite some code to avoid deprecated methods. There is only spot left to deal with WRT deprecations. Tested with a secure MongoDB instance on Windows 7.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/95d59cda Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/95d59cda Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/95d59cda Branch: refs/heads/LOG4J2-1136 Commit: 95d59cda1e3fe60ab90cece67b82396538c81cc7 Parents: 847320c Author: ggregory <[email protected]> Authored: Thu Sep 24 00:51:19 2015 -0700 Committer: Ralph Goers <[email protected]> Committed: Sun Sep 27 10:47:29 2015 -0700 ---------------------------------------------------------------------- .../appender/mongodb/MongoDbConnection.java | 32 ------------- .../nosql/appender/mongodb/MongoDbProvider.java | 48 +++++++++++--------- .../src/test/resources/log4j2-mongodb-auth.xml | 30 ++++++++++++ 3 files changed, 56 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/95d59cda/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbConnection.java ---------------------------------------------------------------------- diff --git a/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbConnection.java b/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbConnection.java index b521439..a803378 100644 --- a/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbConnection.java +++ b/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbConnection.java @@ -93,36 +93,4 @@ public final class MongoDbConnection implements NoSqlConnection<BasicDBObject, M public boolean isClosed() { return !this.mongo.getConnector().isOpen(); } - - /** - * To prevent class loading issues during plugin discovery, this code cannot - * live within MongoDbProvider. This is because of how Java treats - * references to Exception classes different from references to other - * classes. When Java loads a class, it normally won't load that class's - * dependent classes until and unless A) they are used, B) the class being - * loaded extends or implements those classes, or C) those classes are the - * types of static members in the class. However, exceptions that a class - * uses are always loaded when the class is loaded, even before they are - * actually used. - * - * @param database - * The database to authenticate - * @param userName - * The username to authenticate with - * @param password - * The password to authenticate with - */ - static void authenticate(final DB database, final String userName, final String password) { - try { - if (!database.authenticate(userName, password.toCharArray())) { - LOGGER.error("Failed to authenticate against MongoDB server. Unknown error."); - } - } catch (final MongoException e) { - LOGGER.error("Failed to authenticate against MongoDB: " + e.getMessage(), e); - } catch (final IllegalStateException e) { - LOGGER.error( - "Factory-supplied MongoDB database connection already authenticated with different credentials but lost connection.", - e); - } - } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/95d59cda/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbProvider.java ---------------------------------------------------------------------- diff --git a/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbProvider.java b/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbProvider.java index 1627491..be9d9f4 100644 --- a/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbProvider.java +++ b/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbProvider.java @@ -18,12 +18,9 @@ package org.apache.logging.log4j.nosql.appender.mongodb; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.List; -import com.mongodb.DB; -import com.mongodb.MongoClient; -import com.mongodb.ServerAddress; -import com.mongodb.WriteConcern; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.appender.AbstractAppender; import org.apache.logging.log4j.core.config.plugins.Plugin; @@ -35,6 +32,12 @@ import org.apache.logging.log4j.nosql.appender.NoSqlProvider; import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.Strings; +import com.mongodb.DB; +import com.mongodb.MongoClient; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; +import com.mongodb.WriteConcern; + /** * The MongoDB implementation of {@link NoSqlProvider}. */ @@ -152,23 +155,30 @@ public final class MongoDbProvider implements NoSqlProvider<MongoDbConnection> { return null; } } else if (Strings.isNotEmpty(databaseName)) { + 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 { if (Strings.isNotEmpty(server)) { final int portInt = AbstractAppender.parseInt(port, 0); description += ", server=" + server; if (portInt > 0) { description += ", port=" + portInt; - database = new MongoClient(server, portInt).getDB(databaseName); + database = new MongoClient(new ServerAddress(server, portInt), credentials).getDB(databaseName); } else { - database = new MongoClient(server).getDB(databaseName); + database = new MongoClient(new ServerAddress(server), credentials).getDB(databaseName); } } else { - database = new MongoClient().getDB(databaseName); + database = new MongoClient(new ServerAddress(), credentials).getDB(databaseName); } } catch (final Exception e) { - LOGGER.error("Failed to obtain a database instance from the MongoClient at server [{}] and " - + "port [{}].", server, port); + LOGGER.error( + "Failed to obtain a database instance from the MongoClient at server [{}] and " + "port [{}].", + server, port); return null; } } else { @@ -176,19 +186,13 @@ public final class MongoDbProvider implements NoSqlProvider<MongoDbConnection> { return null; } - if (!database.isAuthenticated()) { - if (Strings.isNotEmpty(userName) && Strings.isNotEmpty(password)) { - description += ", username=" + userName + ", passwordHash=" - + NameUtil.md5(password + MongoDbProvider.class.getName()); - MongoDbConnection.authenticate(database, userName, password); - } else { - try { - database.getCollectionNames(); // Check if the database actually requires authentication - } catch (final Exception e) { - LOGGER.error("The database is not already authenticated so you must supply a username and password for the MongoDB provider.", e); - 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; } WriteConcern writeConcern = toWriteConcern(writeConcernConstant, writeConcernConstantClassName); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/95d59cda/log4j-nosql/src/test/resources/log4j2-mongodb-auth.xml ---------------------------------------------------------------------- diff --git a/log4j-nosql/src/test/resources/log4j2-mongodb-auth.xml b/log4j-nosql/src/test/resources/log4j2-mongodb-auth.xml new file mode 100644 index 0000000..2eec2b6 --- /dev/null +++ b/log4j-nosql/src/test/resources/log4j2-mongodb-auth.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration status="error"> + <Appenders> + <NoSql name="MongoDbAppender"> + <MongoDb databaseName="test" collectionName="applog" server="localhost" userName="log4jUser" password="12345678"/> + </NoSql> + </Appenders> + <Loggers> + <Root level="ALL"> + <AppenderRef ref="MongoDbAppender" /> + </Root> + </Loggers> +</Configuration>
