Skip to content

Commit 82105c7

Browse files
author
Vladimir Kotal
authored
another round of fixes for Sonar warnings (#3787)
* another round of fixes for Sonar warnings * fix Sonar warnings * deduplicate
1 parent 6068c59 commit 82105c7

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/Info.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer;
2424

@@ -39,14 +39,16 @@ public final class Info {
3939
private static final String REVISION;
4040
private static final String REVISION_SHORT;
4141

42+
private static final String UNKNOWN = "unknown";
43+
4244
static {
4345
try (InputStream in = Info.class.getResourceAsStream("info.properties")) {
4446
if (in != null) {
4547
properties.load(in);
4648
}
47-
VERSION = properties.getProperty("version", "unknown");
48-
REVISION = properties.getProperty("changeset", "unknown");
49-
REVISION_SHORT = properties.getProperty("changeset_short", "unknown");
49+
VERSION = properties.getProperty("version", UNKNOWN);
50+
REVISION = properties.getProperty("changeset", UNKNOWN);
51+
REVISION_SHORT = properties.getProperty("changeset_short", UNKNOWN);
5052
} catch (IOException ioe) {
5153
throw new RuntimeException(ioe);
5254
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/SSCMRepository.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private Properties getProperties(File file) {
113113
LOGGER.log(Level.WARNING,
114114
"Failed to work with {0} file of {1}: {2}", new Object[]{
115115
MYSCMSERVERINFO_FILE,
116-
getDirectoryName(), ex.getClass().toString()});
116+
getDirectoryName(), ex.getClass()});
117117
}
118118
}
119119

@@ -140,8 +140,12 @@ Executor getHistoryLogExecutor(final File file, String sinceRevision) throws IOE
140140
} else {
141141
argv.add(file.getName());
142142
}
143-
if (sinceRevision != null && new Scanner(sinceRevision).hasNextInt()) {
144-
argv.add("-v" + (Integer.parseInt(sinceRevision) + 1) + ":" + Integer.MAX_VALUE);
143+
if (sinceRevision != null) {
144+
try (Scanner scanner = new Scanner(sinceRevision)) {
145+
if (scanner.hasNextInt()) {
146+
argv.add("-v" + (Integer.parseInt(sinceRevision) + 1) + ":" + Integer.MAX_VALUE);
147+
}
148+
}
145149
}
146150
argv.add("-w-");
147151

@@ -227,9 +231,8 @@ boolean getHistoryGet(OutputStream out, String parent, String basename, String r
227231
}
228232
}
229233
return true;
230-
} catch (IOException exp) {
231-
LOGGER.log(Level.SEVERE,
232-
"Failed to get file: " + exp.getClass().toString(), exp);
234+
} catch (IOException exception) {
235+
LOGGER.log(Level.SEVERE, "Failed to get file", exception);
233236
}
234237

235238
return false;
@@ -249,8 +252,12 @@ boolean fileHasAnnotation(File file) {
249252
if (parts[0].equals(file.getName())) {
250253
// Check if the version field is greater than 1
251254
// which indicates that annotate will work
252-
if (parts.length > 2 && new Scanner(parts[2]).hasNextInt()) {
253-
return Integer.parseInt(parts[2]) > 1;
255+
if (parts.length > 2) {
256+
try (Scanner scanner = new Scanner(parts[2])) {
257+
if (scanner.hasNextInt()) {
258+
return Integer.parseInt(parts[2]) > 1;
259+
}
260+
}
254261
}
255262
break;
256263
}
@@ -259,7 +266,7 @@ boolean fileHasAnnotation(File file) {
259266
LOGGER.log(Level.WARNING,
260267
"Failed to work with {0} file of {1}: {2}", new Object[]{
261268
MYSCMSERVERINFO_FILE,
262-
getDirectoryName(), ex.getClass().toString()});
269+
getDirectoryName(), ex.getClass()});
263270
}
264271
}
265272
return false;

opengrok-indexer/src/main/java/org/opengrok/indexer/search/context/LineMatcher.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828
/**
2929
* Base class for matching a line against terms.
3030
*/
31+
// Avoid the warning about 'WAIT' being confusing w.r.t. Object.wait().
32+
@SuppressWarnings("java:S1845")
3133
public abstract class LineMatcher {
34+
// Line Matcher States
3235
public static final int NOT_MATCHED = 0;
3336
public static final int MATCHED = 1;
3437
public static final int WAIT = 2;
3538

3639
/**
37-
* Tells whether the matching should be done in a case insensitive manner.
40+
* Tells whether the matching should be done in a case-insensitive manner.
3841
*/
3942
private final boolean caseInsensitive;
4043

@@ -49,7 +52,7 @@ public abstract class LineMatcher {
4952
}
5053

5154
/**
52-
* Check if two strings are equal. If this is a case insensitive matcher,
55+
* Check if two strings are equal. If this is a case-insensitive matcher,
5356
* the check will return true if the only difference between the strings
5457
* is difference in case.
5558
*/
@@ -60,7 +63,7 @@ boolean equal(String s1, String s2) {
6063
/**
6164
* Compare two strings and return -1, 0 or 1 if the first string is
6265
* lexicographically smaller than, equal to or greater than the second
63-
* string. If this is a case insensitive matcher, case differences will
66+
* string. If this is a case-insensitive matcher, case differences will
6467
* be ignored.
6568
*/
6669
int compareStrings(String s1, String s2) {
@@ -75,7 +78,7 @@ int compareStrings(String s1, String s2) {
7578

7679
/**
7780
* Normalize a string token for comparison with other string tokens. That
78-
* is, convert to lower case if this is a case insensitive matcher.
81+
* is, convert to lower case if this is a case-insensitive matcher.
7982
* Otherwise, return the string itself.
8083
*/
8184
String normalizeString(String s) {

plugins/src/main/java/opengrok/auth/plugin/LdapUserPlugin.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin;
2424

@@ -69,8 +69,8 @@ public class LdapUserPlugin extends AbstractLdapPlugin {
6969

7070
private String ldapFilter;
7171
private Boolean useDN;
72-
private Set<String> attributes;
73-
private Integer instance;
72+
private Set<String> attrSet;
73+
private Integer instanceNum;
7474

7575
// for testing
7676
void load(Map<String, Object> parameters, AbstractLdapProvider provider) {
@@ -92,22 +92,22 @@ private void init(Map<String, Object> parameters) {
9292
throw new NullPointerException("Missing configuration parameter [" + ATTRIBUTES +
9393
"] in the setup");
9494
}
95-
attributes = new HashSet<>(Arrays.asList(attributesVal.split(",")));
95+
attrSet = new HashSet<>(Arrays.asList(attributesVal.split(",")));
9696

9797
ldapFilter = (String) parameters.get(LDAP_FILTER);
9898

9999
if ((useDN = (Boolean) parameters.get(USE_DN)) == null) {
100100
useDN = false;
101101
}
102102

103-
String instance_param = (String) parameters.get(INSTANCE);
104-
if (instance_param != null) {
105-
instance = Integer.parseInt(instance_param);
103+
String instanceParam = (String) parameters.get(INSTANCE);
104+
if (instanceParam != null) {
105+
instanceNum = Integer.parseInt(instanceParam);
106106
}
107107

108108
LOGGER.log(Level.FINE, "LdapUser plugin loaded with filter={0}, " +
109109
"attributes={1}, useDN={2}, instance={3}",
110-
new Object[]{ldapFilter, attributes, useDN, instance});
110+
new Object[]{ldapFilter, attrSet, useDN, instanceNum});
111111
}
112112

113113
/**
@@ -155,7 +155,7 @@ public void fillSession(HttpServletRequest req, User user) {
155155
}
156156

157157
String dn = null;
158-
if (useDN) {
158+
if (Boolean.TRUE.equals(useDN)) {
159159
dn = user.getUsername();
160160
LOGGER.log(Level.FINEST, "using DN ''{0}'' for user {1}",
161161
new Object[]{dn, user});
@@ -172,17 +172,17 @@ public void fillSession(HttpServletRequest req, User user) {
172172
try {
173173
AbstractLdapProvider.LdapSearchResult<Map<String, Set<String>>> res;
174174
if ((res = ldapProvider.lookupLdapContent(dn, expandedFilter,
175-
attributes.toArray(new String[0]))) == null) {
175+
attrSet.toArray(new String[0]))) == null) {
176176
LOGGER.log(Level.WARNING, "failed to get LDAP attributes ''{2}'' for user {0} " +
177177
"with filter ''{1}'' from LDAP provider {3}",
178-
new Object[]{user, expandedFilter, attributes, getLdapProvider()});
178+
new Object[]{user, expandedFilter, attrSet, getLdapProvider()});
179179
return;
180180
}
181181

182182
records = res.getAttrs();
183-
if (!useDN) {
183+
if (Boolean.FALSE.equals(useDN)) {
184184
dn = res.getDN();
185-
LOGGER.log(Level.FINEST, "got DN ''{0}'' for user {1} on {2}",
185+
LOGGER.log(Level.FINEST, "got DN ''{0}'' for user {1}",
186186
new Object[]{dn, user});
187187
}
188188
} catch (LdapException ex) {
@@ -195,21 +195,21 @@ public void fillSession(HttpServletRequest req, User user) {
195195
return;
196196
}
197197

198-
for (String attrName : attributes) {
198+
for (String attrName : attrSet) {
199199
if (!records.containsKey(attrName) || records.get(attrName) == null || records.get(attrName).isEmpty()) {
200200
LOGGER.log(Level.WARNING, "''{0}'' record for user {1} is not present or empty on {2}",
201201
new Object[]{attrName, user, ldapProvider});
202202
}
203203
}
204204

205-
Map<String, Set<String>> attrSet = new HashMap<>();
206-
for (String attrName : attributes) {
207-
attrSet.put(attrName, records.get(attrName));
205+
Map<String, Set<String>> userAttrSet = new HashMap<>();
206+
for (String attrName : this.attrSet) {
207+
userAttrSet.put(attrName, records.get(attrName));
208208
}
209209

210210
LOGGER.log(Level.FINEST, "DN for user {0} is ''{1}'' on {2}",
211211
new Object[]{user, dn, ldapProvider});
212-
updateSession(req, new LdapUser(dn, attrSet));
212+
updateSession(req, new LdapUser(dn, userAttrSet));
213213
}
214214

215215
/**
@@ -227,7 +227,7 @@ static String getSessionAttrName(Integer instance) {
227227
}
228228

229229
private String getSessionAttrName() {
230-
return getSessionAttrName(instance);
230+
return getSessionAttrName(instanceNum);
231231
}
232232

233233
@Override

0 commit comments

Comments
 (0)