Skip to content

Commit df9a9f5

Browse files
committed
DATAMONGO-1257 - Polishing.
Made internal helper methods in MongoCredentialPropertyEditor static where possible. Original pull request: #310.
1 parent bebd0fa commit df9a9f5

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoCredentialPropertyEditor.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Parse a {@link String} to a Collection of {@link MongoCredential}.
3232
*
3333
* @author Christoph Strobl
34+
* @author Oliver Gierke
3435
* @since 1.7
3536
*/
3637
public class MongoCredentialPropertyEditor extends PropertyEditorSupport {
@@ -95,16 +96,16 @@ public void setAsText(String text) throws IllegalArgumentException {
9596
credentials.add(MongoCredential.createScramSha1Credential(userNameAndPassword[0], database,
9697
userNameAndPassword[1].toCharArray()));
9798
} else {
98-
throw new IllegalArgumentException(String.format(
99-
"Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism));
99+
throw new IllegalArgumentException(
100+
String.format("Cannot create MongoCredentials for unknown auth mechanism '%s'!", authMechanism));
100101
}
101102
}
102103
} else {
103104

104105
verifyUsernameAndPasswordPresent(userNameAndPassword);
105106
verifyDatabasePresent(database);
106-
credentials.add(MongoCredential.createCredential(userNameAndPassword[0], database,
107-
userNameAndPassword[1].toCharArray()));
107+
credentials.add(
108+
MongoCredential.createCredential(userNameAndPassword[0], database, userNameAndPassword[1].toCharArray()));
108109
}
109110
}
110111

@@ -114,8 +115,8 @@ public void setAsText(String text) throws IllegalArgumentException {
114115
private List<String> extractCredentialsString(String source) {
115116

116117
Matcher matcher = GROUP_PATTERN.matcher(source);
117-
118118
List<String> list = new ArrayList<String>();
119+
119120
while (matcher.find()) {
120121

121122
String value = StringUtils.trimLeadingCharacter(matcher.group(), '\'');
@@ -125,21 +126,17 @@ private List<String> extractCredentialsString(String source) {
125126
if (!list.isEmpty()) {
126127
return list;
127128
}
129+
128130
return Arrays.asList(source.split(","));
129131
}
130132

131133
private static String[] extractUserNameAndPassword(String text) {
132134

133135
int index = text.lastIndexOf(DATABASE_DELIMINATOR);
134136

135-
if (index == -1) {
136-
index = text.lastIndexOf(OPTIONS_DELIMINATOR);
137-
}
138-
if (index == -1) {
139-
return new String[] {};
140-
}
141-
String userNameAndPassword = text.substring(0, index);
142-
return userNameAndPassword.split(USERNAME_PASSWORD_DELIMINATOR);
137+
index = index != -1 ? index : text.lastIndexOf(OPTIONS_DELIMINATOR);
138+
139+
return index == -1 ? new String[] {} : text.substring(0, index).split(USERNAME_PASSWORD_DELIMINATOR);
143140
}
144141

145142
private static String extractDB(String text) {
@@ -175,26 +172,27 @@ private static Properties extractOptions(String text) {
175172
return properties;
176173
}
177174

178-
private void verifyUserNamePresent(String[] source) {
179-
180-
if (source.length == 0 || !StringUtils.hasText(source[0])) {
181-
throw new IllegalArgumentException("Credentials need to specify username!");
182-
}
183-
}
184-
185-
private void verifyUsernameAndPasswordPresent(String[] source) {
175+
private static void verifyUsernameAndPasswordPresent(String[] source) {
186176

187177
verifyUserNamePresent(source);
178+
188179
if (source.length != 2) {
189180
throw new IllegalArgumentException(
190181
"Credentials need to specify username and password like in 'username:password@database'!");
191182
}
192183
}
193184

194-
private void verifyDatabasePresent(String source) {
185+
private static void verifyDatabasePresent(String source) {
195186

196187
if (!StringUtils.hasText(source)) {
197188
throw new IllegalArgumentException("Credentials need to specify database like in 'username:password@database'!");
198189
}
199190
}
191+
192+
private static void verifyUserNamePresent(String[] source) {
193+
194+
if (source.length == 0 || !StringUtils.hasText(source[0])) {
195+
throw new IllegalArgumentException("Credentials need to specify username!");
196+
}
197+
}
200198
}

0 commit comments

Comments
 (0)