-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Close unreleased InputStreams in the Rule Engines and ApiReader #1234
Changes from 3 commits
c007b12
977cd7c
9a0552e
7f2c7e1
df1fbd2
3ae31d5
6ff3051
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.inject.Inject; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
/** Implements the RuleEngine interface. It represents a rule engine for transformations. */ | ||
public class TransformationRuleEngine implements RuleEngine { | ||
|
@@ -53,6 +54,7 @@ public void ensureRulesLoaded() throws RuleLoaderException { | |
ruleLoader.loadRules(resourceStream, new TypeReference<>() {}); | ||
rules.addAll(parsedRules); | ||
rulesLoaded = true; | ||
IOUtils.closeQuietly(resourceStream); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't use this. Use the "try with resources" mechanism. I.e. the same thing that @basiliskus mentioned below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
import gov.hhs.cdc.trustedintermediary.wrappers.OpenApiReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.charset.Charset; | ||
import java.util.Objects; | ||
|
||
/** | ||
|
@@ -22,15 +22,17 @@ public static OpenApiReaderImplementation getInstance() { | |
* Loads the stream of an OpenApi file and returns the output as a string. | ||
* | ||
* @param fileName Name of the file to load | ||
* @param charset Charset used for the output string | ||
* @return File contents as string | ||
* @throws UnableToReadOpenApiSpecificationException If there is an issue loading the Api file | ||
*/ | ||
@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); | ||
public String openAsString(String fileName, Charset charset) | ||
throws UnableToReadOpenApiSpecificationException { | ||
try (InputStream stream = getClass().getClassLoader().getResourceAsStream(fileName)) { | ||
var out = new String(Objects.requireNonNull(stream).readAllBytes(), charset); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have that there to silence a warning on the IDE, but since we're catching the |
||
stream.close(); | ||
luis-pabon-tf marked this conversation as resolved.
Show resolved
Hide resolved
luis-pabon-tf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return out; | ||
} catch (IOException | NullPointerException e) { | ||
throw new UnableToReadOpenApiSpecificationException( | ||
"Failed to open OpenAPI specification for " + fileName, e); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package gov.hhs.cdc.trustedintermediary.wrappers; | ||
|
||
import gov.hhs.cdc.trustedintermediary.domainconnector.UnableToReadOpenApiSpecificationException; | ||
import java.nio.charset.Charset; | ||
|
||
/** Wrapper for classes that load OpenApi documentation. */ | ||
public interface OpenApiReader { | ||
String openAsString(String fileName) throws UnableToReadOpenApiSpecificationException; | ||
String openAsString(String fileName, Charset charset) | ||
throws UnableToReadOpenApiSpecificationException; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't pass in
StandardCharset.UTF_8
. There's no reason for.openAsString(...)
to support other charsets. Instead, hardcodeStandardCharsets.UTF_8
inOpenApiReaderImplementation#openAsString
.This applies to the other places where we call
OpenApiReaderImplementation.getInstance() .openAsString(...)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had made this change for this PR. Reverted the change, the diff should be a bit smaller now too