From df9a9f5fb68758a6d653984c0eaf4ece133cbb69 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 24 Jul 2015 18:40:17 +0200 Subject: [PATCH] DATAMONGO-1257 - Polishing. Made internal helper methods in MongoCredentialPropertyEditor static where possible. Original pull request: #310. --- .../config/MongoCredentialPropertyEditor.java | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java index 4e5021585..a008d1a6b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java @@ -31,6 +31,7 @@ import com.mongodb.MongoCredential; * Parse a {@link String} to a Collection of {@link MongoCredential}. * * @author Christoph Strobl + * @author Oliver Gierke * @since 1.7 */ public class MongoCredentialPropertyEditor extends PropertyEditorSupport { @@ -95,16 +96,16 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport { credentials.add(MongoCredential.createScramSha1Credential(userNameAndPassword[0], database, userNameAndPassword[1].toCharArray())); } else { - throw new IllegalArgumentException(String.format( - "Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism)); + throw new IllegalArgumentException( + String.format("Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism)); } } } else { verifyUsernameAndPasswordPresent(userNameAndPassword); verifyDatabasePresent(database); - credentials.add(MongoCredential.createCredential(userNameAndPassword[0], database, - userNameAndPassword[1].toCharArray())); + credentials.add( + MongoCredential.createCredential(userNameAndPassword[0], database, userNameAndPassword[1].toCharArray())); } } @@ -114,8 +115,8 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport { private List extractCredentialsString(String source) { Matcher matcher = GROUP_PATTERN.matcher(source); - List list = new ArrayList(); + while (matcher.find()) { String value = StringUtils.trimLeadingCharacter(matcher.group(), '\''); @@ -125,6 +126,7 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport { if (!list.isEmpty()) { return list; } + return Arrays.asList(source.split(",")); } @@ -132,14 +134,9 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport { int index = text.lastIndexOf(DATABASE_DELIMINATOR); - if (index == -1) { - index = text.lastIndexOf(OPTIONS_DELIMINATOR); - } - if (index == -1) { - return new String[] {}; - } - String userNameAndPassword = text.substring(0, index); - return userNameAndPassword.split(USERNAME_PASSWORD_DELIMINATOR); + index = index != -1 ? index : text.lastIndexOf(OPTIONS_DELIMINATOR); + + return index == -1 ? new String[] {} : text.substring(0, index).split(USERNAME_PASSWORD_DELIMINATOR); } private static String extractDB(String text) { @@ -175,26 +172,27 @@ public class MongoCredentialPropertyEditor extends PropertyEditorSupport { return properties; } - private void verifyUserNamePresent(String[] source) { - - if (source.length == 0 || !StringUtils.hasText(source[0])) { - throw new IllegalArgumentException("Credentials need to specify username!"); - } - } - - private void verifyUsernameAndPasswordPresent(String[] source) { + private static void verifyUsernameAndPasswordPresent(String[] source) { verifyUserNamePresent(source); + if (source.length != 2) { throw new IllegalArgumentException( "Credentials need to specify username and password like in 'username:password@database'!"); } } - private void verifyDatabasePresent(String source) { + private static void verifyDatabasePresent(String source) { if (!StringUtils.hasText(source)) { throw new IllegalArgumentException("Credentials need to specify database like in 'username:password@database'!"); } } + + private static void verifyUserNamePresent(String[] source) { + + if (source.length == 0 || !StringUtils.hasText(source[0])) { + throw new IllegalArgumentException("Credentials need to specify username!"); + } + } }