Skip to content

Commit

Permalink
Merge pull request #1234 from CDCgov/story/1212/release-resources
Browse files Browse the repository at this point in the history
Close unreleased InputStreams in the Rule Engines and ApiReader
  • Loading branch information
halprin authored Aug 13, 2024
2 parents eb3a40f + 6ff3051 commit 39769b2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gov.hhs.cdc.trustedintermediary.wrappers.Logger;
import gov.hhs.cdc.trustedintermediary.wrappers.formatter.TypeReference;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -40,19 +41,20 @@ public void ensureRulesLoaded() throws RuleLoaderException {
if (!rulesLoaded) {
synchronized (rules) {
if (!rulesLoaded) {
InputStream resourceStream =
try (InputStream stream =
getClass()
.getClassLoader()
.getResourceAsStream(ruleDefinitionsFileName);
if (resourceStream == null) {
.getResourceAsStream(ruleDefinitionsFileName)) {
List<TransformationRule> parsedRules =
ruleLoader.loadRules(stream, new TypeReference<>() {});
rules.addAll(parsedRules);
rulesLoaded = true;

} catch (IOException | NullPointerException e) {
throw new RuleLoaderException(
"File not found: " + ruleDefinitionsFileName,
new FileNotFoundException());
}
List<TransformationRule> parsedRules =
ruleLoader.loadRules(resourceStream, new TypeReference<>() {});
rules.addAll(parsedRules);
rulesLoaded = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gov.hhs.cdc.trustedintermediary.wrappers.Logger;
import gov.hhs.cdc.trustedintermediary.wrappers.formatter.TypeReference;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -40,19 +41,20 @@ public void ensureRulesLoaded() throws RuleLoaderException {
if (!rulesLoaded) {
synchronized (rules) {
if (!rulesLoaded) {
InputStream resourceStream =
try (InputStream stream =
getClass()
.getClassLoader()
.getResourceAsStream(ruleDefinitionsFileName);
if (resourceStream == null) {
.getResourceAsStream(ruleDefinitionsFileName)) {
List<ValidationRule> parsedRules =
ruleLoader.loadRules(stream, new TypeReference<>() {});
rules.addAll(parsedRules);
rulesLoaded = true;

} catch (IOException | NullPointerException e) {
throw new RuleLoaderException(
"File not found: " + ruleDefinitionsFileName,
new FileNotFoundException());
}
List<ValidationRule> parsedRules =
ruleLoader.loadRules(resourceStream, new TypeReference<>() {});
rules.addAll(parsedRules);
rulesLoaded = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
* Basic implementation of a Reader for OpenApi to promote code reuse. If additional functionality
Expand All @@ -27,10 +26,8 @@ public static OpenApiReaderImplementation getInstance() {
*/
@Override
public String openAsString(String fileName) throws UnableToReadOpenApiSpecificationException {
InputStream stream = getClass().getClassLoader().getResourceAsStream(fileName);
try {
return new String(
Objects.requireNonNull(stream).readAllBytes(), StandardCharsets.UTF_8);
try (InputStream stream = getClass().getClassLoader().getResourceAsStream(fileName)) {
return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException | NullPointerException e) {
throw new UnableToReadOpenApiSpecificationException(
"Failed to open OpenAPI specification for " + fileName, e);
Expand Down

0 comments on commit 39769b2

Please sign in to comment.