Skip to content

Commit 10ee5cc

Browse files
committed
Cleaned up redundant logic to SonarProperties loader.
1 parent b32c58f commit 10ee5cc

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

src/main/java/fr/cnes/sonar/plugin/tools/SonarPropertiesLoader.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,39 @@
99

1010
public class SonarPropertiesLoader {
1111

12+
Properties properties;
13+
14+
public SonarPropertiesLoader(Path path) throws IOException{
15+
16+
loadSonarProperties(path);
17+
}
18+
1219
/** Logger of this class */
1320
private static final Logger LOGGER = Logger.getLogger(FileTools.class.getName());
1421

15-
public Properties loadSonarProperties(Path sonarPropertiesPath)
22+
public final Properties loadSonarProperties(Path sonarPropertiesPath)
1623
throws IOException {
1724

18-
Properties properties = new Properties();
25+
this.properties = new Properties();
1926
try {
2027
InputStream sonar = Files.newInputStream(sonarPropertiesPath);
2128
if (sonar != null) {
22-
properties.load(sonar);
29+
this.properties.load(sonar);
2330
}
2431
} catch (IOException e) {
25-
//LOGGER.log(Level.SEVERE, "-i : Could not find or open provided sonar.properties at: " + e.getMessage(), e);
32+
// LOGGER.log(Level.SEVERE, "-i : Could not find or open provided
33+
// sonar.properties at: " + e.getMessage(), e);
2634
throw e;
2735
}
28-
return properties;
36+
return this.properties;
37+
}
38+
39+
public String getSonarProperty(String propName) {
40+
String prop = this.properties.getProperty(propName);
41+
// We check that the property existed and was actually returned. If it is we
42+
// keep it as is.
43+
// If it's not, we assign "" to it for the following checks.
44+
prop = prop != null ? prop : "";
45+
return prop;
2946
}
3047
}

src/main/java/fr/cnes/sonar/report/utils/ReportConfiguration.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.nio.file.Paths;
2222
import java.text.SimpleDateFormat;
2323
import java.util.Date;
24-
import java.util.Properties;
2524

2625
import fr.cnes.sonar.plugin.tools.SonarPropertiesLoader;
2726

@@ -140,30 +139,24 @@ public static ReportConfiguration create(final String[] pArgs) throws IOExceptio
140139
String token = new String();
141140

142141
if (commandLineManager.hasOption("i")) {
143-
// If we have a sonar.properties file, we read it and configure url and
144-
// projectkey from it, possibly token.
142+
// If we have a sonar.properties file, we read it and set server, projectKey and token from it if possible.
145143
try {
146-
SonarPropertiesLoader propLoader = new SonarPropertiesLoader();
147-
Properties sonarProps = propLoader
148-
.loadSonarProperties(Paths.get(commandLineManager.getOptionValue("i")));
144+
SonarPropertiesLoader propLoader = new SonarPropertiesLoader(Paths.get(commandLineManager.getOptionValue("i")));
149145

150146
// If -s input does not exist, add it with the value of
151-
// sonarprops.get("sonar.host.url"). If it does, no action (Command line is
152-
// authoritative).
153-
if (!commandLineManager.hasOption("s")) {
154-
server = sonarProps.getProperty(StringManager.SONAR_SERVER);
155-
server = server != null ? server : "";
156-
}
157-
if (!commandLineManager.hasOption("p")) {
158-
projectKey = sonarProps.getProperty(StringManager.SONAR_KEY);
159-
projectKey = projectKey != null ? projectKey : "";
160-
}
161-
if (!commandLineManager.hasOption("t")) {
162-
token = sonarProps.getProperty(StringManager.SONAR_TOKEN);
163-
token = token != null ? token : "";
164-
}
147+
// sonarprops.get("sonar.host.url"). If it does, no action (Command line is authoritative).
165148
// Similar logic for other parameters, projectKey, token, etc.
149+
150+
if (!commandLineManager.hasOption("s"))
151+
server = propLoader.getSonarProperty(StringManager.SONAR_SERVER);
152+
if (!commandLineManager.hasOption("p"))
153+
projectKey = propLoader.getSonarProperty(StringManager.SONAR_KEY);
154+
if (!commandLineManager.hasOption("t"))
155+
token = propLoader.getSonarProperty(StringManager.SONAR_TOKEN);
156+
166157
} catch (IOException e) {
158+
// If sonar.properties was not properly opened, we raise this to main to halt
159+
// execution.
167160
throw e;
168161
}
169162
}
@@ -173,22 +166,16 @@ public static ReportConfiguration create(final String[] pArgs) throws IOExceptio
173166
StringManager.NO_BRANCH);
174167
return new ReportConfiguration(commandLineManager.hasOption("h"), commandLineManager.hasOption("v"),
175168
commandLineManager.getOptionValue("i", StringManager.getProperty(StringManager.SONAR_PROPERTIES)),
176-
server.isEmpty()
177-
? commandLineManager.getOptionValue("s", StringManager.getProperty(StringManager.SONAR_URL))
178-
: server,
179-
token.isEmpty()
180-
? commandLineManager.getOptionValue("t", StringManager.getProperty(StringManager.SONAR_TOKEN))
181-
: token,
169+
server.isEmpty() ? commandLineManager.getOptionValue("s", StringManager.getProperty(StringManager.SONAR_URL)): server,
170+
token.isEmpty() ? commandLineManager.getOptionValue("t", StringManager.getProperty(StringManager.SONAR_TOKEN)): token,
182171
projectKey.isEmpty() ? commandLineManager.getOptionValue("p", StringManager.EMPTY) : projectKey,
183172
commandLineManager.getOptionValue("o", StringManager.getProperty(StringManager.DEFAULT_OUTPUT)),
184173
commandLineManager.getOptionValue("l", StringManager.getProperty(StringManager.DEFAULT_LANGUAGE)),
185174
commandLineManager.getOptionValue("a", StringManager.getProperty(StringManager.DEFAULT_AUTHOR)),
186175
commandLineManager.getOptionValue("d",
187176
new SimpleDateFormat(StringManager.DATE_PATTERN).format(new Date())),
188177
!commandLineManager.hasOption("c"), !commandLineManager.hasOption("w"),
189-
!commandLineManager.hasOption("e"), !commandLineManager.hasOption("f"), // Why f ? Because every "logic"
190-
// options like "c" are already
191-
// used
178+
!commandLineManager.hasOption("e"), !commandLineManager.hasOption("f"), // Why f ? Because every "logic" options like "c" are already used
192179
!commandLineManager.hasOption("m"), commandLineManager.getOptionValue("r", StringManager.EMPTY),
193180
commandLineManager.getOptionValue("x", StringManager.EMPTY),
194181
commandLineManager.getOptionValue("n", StringManager.EMPTY),

0 commit comments

Comments
 (0)