Skip to content

Commit ccb7799

Browse files
authored
Refactor and handle unhandled exceptions (#707)
1 parent c5beec9 commit ccb7799

File tree

8 files changed

+61
-48
lines changed

8 files changed

+61
-48
lines changed

Diff for: build-info-extractor-go/src/main/java/org/jfrog/build/extractor/go/extractor/GoPublish.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.time.Instant;
3030
import java.util.*;
3131
import java.util.stream.Collectors;
32+
import java.util.stream.Stream;
3233
import java.util.zip.ZipEntry;
3334
import java.util.zip.ZipOutputStream;
3435

@@ -152,9 +153,9 @@ private void createAndDeployInfo(ArtifactoryManager artifactoryManager) throws E
152153

153154
private File archiveProjectDir() throws IOException {
154155
File zipFile = File.createTempFile(LOCAL_TMP_PKG_PREFIX + LOCAL_PKG_FILENAME, PKG_ZIP_FILE_EXTENSION, path.toFile());
155-
156-
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
157-
List<Path> pathsList = Files.walk(path)
156+
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()));
157+
Stream<Path> pathFileTree = Files.walk(path)) {
158+
List<Path> pathsList = pathFileTree
158159
// Remove .git dir content, directories and the temp zip file
159160
.filter(p -> !path.relativize(p).startsWith(".git/") && !Files.isDirectory(p) && !p.toFile().getName().equals(zipFile.getName()))
160161
.collect(Collectors.toList());

Diff for: build-info-extractor-go/src/test/java/org/jfrog/build/extractor/go/extractor/GoZipBallStreamerTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.testng.annotations.DataProvider;
55
import org.testng.annotations.Test;
66

7+
import java.io.IOException;
8+
79

810
public class GoZipBallStreamerTest {
911
@Test(dataProvider = "testIsSubModuleProvider")

Diff for: build-info-extractor-nuget/src/main/java/org/jfrog/build/extractor/nuget/extractor/NugetRun.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,15 @@ private void collectDependenciesFromSln(File slnFile) throws IOException, Interr
322322
}
323323

324324
private void collectDependenciesFromProjectDir(File projectRoot) throws Exception {
325-
List<Path> csprojFiles = Files.list(projectRoot.toPath())
326-
.filter(path -> path.toString().endsWith(".csproj")).collect(Collectors.toList());
327-
if (csprojFiles.size() == 1) {
328-
String csprojPath = csprojFiles.get(0).toString();
329-
String projectName = csprojFiles.get(0).getFileName().toString().replace(".csproj", "");
330-
String globalCachePath = toolchainDriver.globalPackagesCache();
331-
singleProjectHandler(projectName, csprojPath, globalCachePath);
325+
try (Stream<Path> files = Files.list(projectRoot.toPath())) {
326+
List<Path> csprojFiles = files
327+
.filter(path -> path.toString().endsWith(".csproj")).collect(Collectors.toList());
328+
if (csprojFiles.size() == 1) {
329+
String csprojPath = csprojFiles.get(0).toString();
330+
String projectName = csprojFiles.get(0).getFileName().toString().replace(".csproj", "");
331+
String globalCachePath = toolchainDriver.globalPackagesCache();
332+
singleProjectHandler(projectName, csprojPath, globalCachePath);
333+
}
332334
}
333335
}
334336

Diff for: build-info-extractor/src/main/java/org/jfrog/build/extractor/BuildInfoExtractorUtils.java

+21-19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.*;
2323
import java.nio.charset.StandardCharsets;
24+
import java.nio.file.Files;
2425
import java.util.HashMap;
2526
import java.util.Map;
2627
import java.util.Properties;
@@ -67,7 +68,7 @@ public static Properties mergePropertiesWithSystemAndPropertyFile(Properties exi
6768
InputStream inputStream = null;
6869
try {
6970
if (propertiesFile.exists()) {
70-
inputStream = new FileInputStream(propertiesFile);
71+
inputStream = Files.newInputStream(propertiesFile.toPath());
7172
props.load(inputStream);
7273
}
7374
} catch (IOException e) {
@@ -147,7 +148,7 @@ public static Properties getEnvProperties(Properties startProps, Log log) {
147148
File propertiesFile = new File(propsFilePath);
148149
InputStream inputStream = null;
149150
try {
150-
inputStream = new FileInputStream(propertiesFile);
151+
inputStream = Files.newInputStream(propertiesFile.toPath());
151152
Properties propertiesFromFile = new Properties();
152153
propertiesFromFile.load(inputStream);
153154
props.putAll(filterDynamicProperties(propertiesFromFile, ENV_PREDICATE));
@@ -178,37 +179,38 @@ private static JsonFactory createJsonFactory() {
178179
public static String buildInfoToJsonString(BuildInfo buildInfo) throws IOException {
179180
JsonFactory jsonFactory = createJsonFactory();
180181

181-
StringWriter writer = new StringWriter();
182-
JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
183-
jsonGenerator.useDefaultPrettyPrinter();
184-
185-
jsonGenerator.writeObject(buildInfo);
186-
String result = writer.getBuffer().toString();
187-
return result;
182+
try (StringWriter writer = new StringWriter();
183+
JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer)) {
184+
jsonGenerator.useDefaultPrettyPrinter();
185+
jsonGenerator.writeObject(buildInfo);
186+
return writer.getBuffer().toString();
187+
}
188188
}
189189

190190
public static BuildInfo jsonStringToBuildInfo(String json) throws IOException {
191191
JsonFactory jsonFactory = createJsonFactory();
192-
JsonParser parser = jsonFactory.createParser(new StringReader(json));
193-
return jsonFactory.getCodec().readValue(parser, BuildInfo.class);
192+
try (JsonParser parser = jsonFactory.createParser(new StringReader(json))) {
193+
return jsonFactory.getCodec().readValue(parser, BuildInfo.class);
194+
}
194195
}
195196

196197
public static <T extends Serializable> String buildInfoToJsonString(T buildComponent) throws IOException {
197198
JsonFactory jsonFactory = createJsonFactory();
198199

199-
StringWriter writer = new StringWriter();
200-
JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
201-
jsonGenerator.useDefaultPrettyPrinter();
200+
try (StringWriter writer = new StringWriter();
201+
JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer)) {
202+
jsonGenerator.useDefaultPrettyPrinter();
203+
jsonGenerator.writeObject(buildComponent);
202204

203-
jsonGenerator.writeObject(buildComponent);
204-
String result = writer.getBuffer().toString();
205-
return result;
205+
return writer.getBuffer().toString();
206+
}
206207
}
207208

208209
public static <T extends Serializable> T jsonStringToGeneric(String json, Class<T> clazz) throws IOException {
209210
JsonFactory jsonFactory = createJsonFactory();
210-
JsonParser parser = jsonFactory.createParser(new StringReader(json));
211-
return jsonFactory.getCodec().readValue(parser, clazz);
211+
try (JsonParser parser = jsonFactory.createParser(new StringReader(json))) {
212+
return jsonFactory.getCodec().readValue(parser, clazz);
213+
}
212214
}
213215

214216
public static void saveBuildInfoToFile(BuildInfo buildInfo, File toFile) throws IOException {

Diff for: build-info-extractor/src/main/java/org/jfrog/build/extractor/ModuleExtractorUtils.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ private static JsonFactory createJsonFactory() {
3838
public static String moduleToJsonString(Module module) throws IOException {
3939
JsonFactory jsonFactory = createJsonFactory();
4040

41-
StringWriter writer = new StringWriter();
42-
JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
43-
jsonGenerator.useDefaultPrettyPrinter();
41+
try (StringWriter writer = new StringWriter();
42+
JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer)) {
43+
jsonGenerator.useDefaultPrettyPrinter();
44+
jsonGenerator.writeObject(module);
4445

45-
jsonGenerator.writeObject(module);
46-
return writer.getBuffer().toString();
46+
return writer.getBuffer().toString();
47+
}
4748
}
4849

4950
/**

Diff for: build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/JsonSerializer.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ public String toJSON(T object) throws IOException {
3838

3939
mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector());
4040
jsonFactory.setCodec(mapper);
41-
StringWriter writer = new StringWriter();
42-
JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);
43-
jsonGenerator.useDefaultPrettyPrinter();
44-
jsonGenerator.writeObject(object);
45-
return writer.getBuffer().toString();
41+
try (StringWriter writer = new StringWriter();
42+
JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer)) {
43+
jsonGenerator.useDefaultPrettyPrinter();
44+
jsonGenerator.writeObject(object);
45+
46+
return writer.getBuffer().toString();
47+
}
4648
}
4749
}

Diff for: build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/JsonUtils.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
public class JsonUtils {
1616
public static String toJsonString(Object object) throws IOException {
1717
JsonFactory jsonFactory = createJsonFactory();
18-
StringWriter writer = new StringWriter();
19-
JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
20-
jsonGenerator.useDefaultPrettyPrinter();
21-
jsonGenerator.writeObject(object);
22-
return writer.getBuffer().toString();
18+
try (StringWriter writer = new StringWriter();
19+
JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer)) {
20+
jsonGenerator.useDefaultPrettyPrinter();
21+
jsonGenerator.writeObject(object);
22+
23+
return writer.getBuffer().toString();
24+
}
2325
}
2426

2527
public static JsonParser createJsonParser(InputStream in) throws IOException {

Diff for: build-info-extractor/src/main/java/org/jfrog/build/extractor/usageReport/UsageReporter.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ public UsageReporter(String productId, String[] featureIds) {
1919
}
2020

2121
public void reportUsage(String artifactoryUrl, String username, String password, String accessToken, ProxyConfiguration proxyConfiguration, Log log) throws IOException {
22-
ArtifactoryManager artifactoryManager = new ArtifactoryManager(artifactoryUrl, username, password, accessToken, log);
23-
if (proxyConfiguration != null) {
24-
artifactoryManager.setProxyConfiguration(proxyConfiguration);
22+
try (ArtifactoryManager artifactoryManager = new ArtifactoryManager(artifactoryUrl, username, password, accessToken, log)) {
23+
if (proxyConfiguration != null) {
24+
artifactoryManager.setProxyConfiguration(proxyConfiguration);
25+
}
26+
artifactoryManager.reportUsage(this);
2527
}
26-
artifactoryManager.reportUsage(this);
2728
}
2829

2930
public String getProductId() {

0 commit comments

Comments
 (0)