Skip to content

Commit

Permalink
Fixing blocker issues reported by SonarCloud (#1896)
Browse files Browse the repository at this point in the history
Mostly addressing possible leaks by possibly not closed streams.
  • Loading branch information
sutaakar authored and mswiderski committed Aug 26, 2019
1 parent f44a4c7 commit 1753048
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 44 deletions.
Expand Up @@ -184,8 +184,7 @@ private void generateModel() throws MojoExecutionException {

private void deleteDrlFiles(Set<String> actualDrlFiles) throws MojoExecutionException {
// Remove drl files
try {
final Stream<Path> drlFilesToDeleted = Files.find(outputDirectory.toPath(), Integer.MAX_VALUE, (p, f) -> drlFileMatcher.matches(p));
try (final Stream<Path> drlFilesToDeleted = Files.find(outputDirectory.toPath(), Integer.MAX_VALUE, (p, f) -> drlFileMatcher.matches(p))) {
Set<String> deletedFiles = new HashSet<>();
drlFilesToDeleted.forEach(p -> {
try {
Expand Down
Expand Up @@ -86,9 +86,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
KieBase kb = kc.getKieBase(kbase);
getLog().info("Writing KBase: " + kbase);
File file = new File(outputFolder, kbase.replace('.', '_').toLowerCase());
FileOutputStream out = new FileOutputStream(file);
DroolsStreamUtils.streamOut(out, kb.getKiePackages());
out.close();
try (FileOutputStream out = new FileOutputStream(file)) {
DroolsStreamUtils.streamOut(out, kb.getKiePackages());
}
}
} catch (Exception e) {
throw new MojoExecutionException("error", e);
Expand Down
Expand Up @@ -115,9 +115,8 @@ protected String initializeURI(URL url, String servicePrefix) {
}
urlString += "services/"+servicePrefix + "/server";

URL serverPlusServicePrefixUrl;
try {
serverPlusServicePrefixUrl = new URL( urlString );
new URL( urlString );
} catch ( MalformedURLException murle ) {
throw new IllegalArgumentException(
"URL (" + url.toExternalForm() + ") is incorrectly formatted: " + murle.getMessage(), murle );
Expand Down Expand Up @@ -547,6 +546,14 @@ protected ServiceResponsesList executeJmsCommand( CommandScript command, String
producer.send(textMsg);
} catch( JMSException jmse ) {
throw new KieServicesException("Unable to send a JMS message.", jmse);
} finally {
if ( producer != null ) {
try {
producer.close();
} catch (JMSException jmse) {
logger.warn("Unable to close producer!", jmse);
}
}
}

// receive
Expand Down
Expand Up @@ -45,30 +45,20 @@ public FileRepository(File repositoryDir) {

@Override
public void persist(Configuration configuration) {


FileOutputStream fos = null;
try {
File configFile = new File(repositoryDir, "kie-server-router.json");
fos = new FileOutputStream(configFile);

File configFile = new File(repositoryDir, "kie-server-router.json");
try (FileOutputStream fos = new FileOutputStream(configFile)) {

String config = marshaller.marshall(configuration);
PrintWriter writer = new PrintWriter(fos);
writer.write(config);
writer.close();

try (PrintWriter writer = new PrintWriter(fos)) {
writer.write(config);
}

configFile.setLastModified(System.currentTimeMillis());

} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
}

Expand Down
Expand Up @@ -16,6 +16,7 @@
package org.kie.server.services.jbpm;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand Down Expand Up @@ -448,11 +449,9 @@ public void createContainer(String id, KieContainerInstance kieContainerInstance
// add any query definition found in kjar
Enumeration<URL> queryDefinitionsFiles = kieContainer.getClassLoader().getResources("query-definitions.json");
while (queryDefinitionsFiles.hasMoreElements()) {

URL definitionsURL = queryDefinitionsFiles.nextElement();
InputStream qdStream = definitionsURL.openStream();

loadAndRegisterQueryDefinitions(qdStream, kieContainerInstance.getMarshaller(MarshallingFormat.JSON), id);
try (InputStream qdStream = queryDefinitionsFiles.nextElement().openStream()) {
loadAndRegisterQueryDefinitions(qdStream, kieContainerInstance.getMarshaller(MarshallingFormat.JSON), id);
}
}

logger.debug("Container {} created successfully by extension {}", id, this);
Expand Down Expand Up @@ -812,26 +811,25 @@ protected void loadAndRegisterQueryDefinitions(InputStream qdStream, org.kie.ser
}

protected void registerDefaultQueryDefinitions() {
try {
// load any default query definitions
InputStream qdStream = this.getClass().getResourceAsStream("/default-query-definitions.json");
if (qdStream == null) {
String externalLocationQueryDefinitions = System.getProperty(KieServerConstants.CFG_DEFAULT_QUERY_DEFS_LOCATION);
if (externalLocationQueryDefinitions != null) {
qdStream = new FileInputStream(externalLocationQueryDefinitions);
}
}

try (InputStream qdStream = getDefaultQueryDefinitionsInputStream()) {
loadAndRegisterQueryDefinitions(qdStream, MarshallerFactory.getMarshaller(MarshallingFormat.JSON, this.getClass().getClassLoader()), null);

if (qdStream != null) {
qdStream.close();
}
} catch (Exception e) {
logger.error("Error when loading default query definitions from default-query-definitions.json", e);
}
}

private InputStream getDefaultQueryDefinitionsInputStream() throws FileNotFoundException {
// load any default query definitions
InputStream qdStream = this.getClass().getResourceAsStream("/default-query-definitions.json");
if (qdStream == null) {
String externalLocationQueryDefinitions = System.getProperty(KieServerConstants.CFG_DEFAULT_QUERY_DEFS_LOCATION);
if (externalLocationQueryDefinitions != null) {
qdStream = new FileInputStream(externalLocationQueryDefinitions);
}
}
return qdStream;
}

// just for tests
void setQueryService(QueryService queryService) {
this.queryService = queryService;
Expand Down

0 comments on commit 1753048

Please sign in to comment.