From bed3f0c8c906b93a0f2ecd48b6763c6bab053ae3 Mon Sep 17 00:00:00 2001 From: Kai Zimmermann Date: Wed, 27 Jan 2016 14:16:24 +0100 Subject: [PATCH] Fixed stream handling and some sonar issues --- .../api/client/DistributionSetResource.java | 3 +- .../repository/MongoConfiguration.java | 25 +++---- .../push/AsyncVaadinServletConfiguration.java | 1 + .../targettable/BulkUploadHandler.java | 72 +++++++++---------- 4 files changed, 43 insertions(+), 58 deletions(-) diff --git a/examples/hawkbit-mgmt-api-client/src/main/java/org/eclipse/hawkbit/mgmt/api/client/DistributionSetResource.java b/examples/hawkbit-mgmt-api-client/src/main/java/org/eclipse/hawkbit/mgmt/api/client/DistributionSetResource.java index 747432cd0..62c987ae8 100644 --- a/examples/hawkbit-mgmt-api-client/src/main/java/org/eclipse/hawkbit/mgmt/api/client/DistributionSetResource.java +++ b/examples/hawkbit-mgmt-api-client/src/main/java/org/eclipse/hawkbit/mgmt/api/client/DistributionSetResource.java @@ -20,10 +20,11 @@ import feign.RequestLine; /** * Client binding for the Distribution resource of the management API. */ +@FunctionalInterface public interface DistributionSetResource { /** - * Creates a list of distrbution sets. + * Creates a list of distribution sets. * * @param sets * the request body java bean containing the necessary attributes diff --git a/hawkbit-artifact-repository-mongo/src/main/java/org/eclipse/hawkbit/artifact/repository/MongoConfiguration.java b/hawkbit-artifact-repository-mongo/src/main/java/org/eclipse/hawkbit/artifact/repository/MongoConfiguration.java index 347abea1e..02fb22725 100644 --- a/hawkbit-artifact-repository-mongo/src/main/java/org/eclipse/hawkbit/artifact/repository/MongoConfiguration.java +++ b/hawkbit-artifact-repository-mongo/src/main/java/org/eclipse/hawkbit/artifact/repository/MongoConfiguration.java @@ -59,7 +59,7 @@ public class MongoConfiguration extends AbstractMongoConfiguration { } /** - * Closes mongo client when destroyd. + * Closes mongo client when destroyed. */ @PreDestroy public void close() { @@ -74,22 +74,13 @@ public class MongoConfiguration extends AbstractMongoConfiguration { public Mongo mongo() throws UnknownHostException { final MongoClientURI uri = new MongoClientURI(properties.getUri(), createBuilderOutOfOptions(options)); - try { - if (properties.getPort() != null) { - LOG.debug("Create mongo by properties (host: {}, port: {})", uri.getHosts().get(0), - properties.getPort()); - this.mongoConnection = new MongoClient( - Arrays.asList(new ServerAddress(uri.getHosts().get(0), properties.getPort())), - uri.getOptions()); - } else { - LOG.debug("Create mongo by URI : {}", uri); - this.mongoConnection = new MongoClient(uri); - } - } finally { - if (this.mongoConnection != null) { - this.mongoConnection.close(); - } - + if (properties.getPort() != null) { + LOG.debug("Create mongo by properties (host: {}, port: {})", uri.getHosts().get(0), properties.getPort()); + this.mongoConnection = new MongoClient( + Arrays.asList(new ServerAddress(uri.getHosts().get(0), properties.getPort())), uri.getOptions()); + } else { + LOG.debug("Create mongo by URI : {}", uri); + this.mongoConnection = new MongoClient(uri); } return this.mongoConnection; diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/push/AsyncVaadinServletConfiguration.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/push/AsyncVaadinServletConfiguration.java index 6eb6761e6..89ff3413e 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/push/AsyncVaadinServletConfiguration.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/push/AsyncVaadinServletConfiguration.java @@ -32,6 +32,7 @@ import com.vaadin.spring.boot.internal.VaadinServletConfigurationProperties; @Import(VaadinServletConfiguration.class) public class AsyncVaadinServletConfiguration extends VaadinServletConfiguration { + @Override @Bean protected ServletRegistrationBean vaadinServletRegistration() { return createServletRegistrationBean(); diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/management/targettable/BulkUploadHandler.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/management/targettable/BulkUploadHandler.java index 8ba60ec51..fa4022a3e 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/management/targettable/BulkUploadHandler.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/management/targettable/BulkUploadHandler.java @@ -14,6 +14,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.nio.charset.Charset; @@ -62,15 +63,13 @@ import com.vaadin.ui.Upload.SucceededListener; /** * Bulk target upload handler. - * - * * */ public class BulkUploadHandler extends CustomComponent implements SucceededListener, FailedListener, Receiver, StartedListener { /** - * * + * */ private static final long serialVersionUID = -1273494705754674501L; private static final Logger LOG = LoggerFactory.getLogger(BulkUploadHandler.class); @@ -103,7 +102,7 @@ public class BulkUploadHandler extends CustomComponent final TargetBulkUpdateWindowLayout targetBulkUpdateWindowLayout; /** - * + * * @param targetBulkUpdateWindowLayout * @param targetManagement * @param managementUIState @@ -152,7 +151,7 @@ public class BulkUploadHandler extends CustomComponent /* * (non-Javadoc) - * + * * @see com.vaadin.ui.Upload.Receiver#receiveUpload(java.lang.String, * java.lang.String) */ @@ -173,7 +172,7 @@ public class BulkUploadHandler extends CustomComponent /* * (non-Javadoc) - * + * * @see * com.vaadin.ui.Upload.FailedListener#uploadFailed(com.vaadin.ui.Upload. * FailedEvent) @@ -185,7 +184,7 @@ public class BulkUploadHandler extends CustomComponent /* * (non-Javadoc) - * + * * @see * com.vaadin.ui.Upload.SucceededListener#uploadSucceeded(com.vaadin.ui. * Upload.SucceededEvent) @@ -199,7 +198,7 @@ public class BulkUploadHandler extends CustomComponent final SucceededEvent event; /** - * + * * @param event */ public UploadAsync(final SucceededEvent event) { @@ -208,15 +207,18 @@ public class BulkUploadHandler extends CustomComponent @Override public void run() { - BufferedReader reader = null; long innerCounter = 0; String line; - if (tempFile != null) { - try { + if (tempFile == null) { + return; + } + + try (InputStream tempStream = new FileInputStream(tempFile)) { + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(tempStream, Charset.defaultCharset()))) { LOG.info("Bulk file upload started"); final double totalFileSize = getTotalNumberOfLines(); - reader = new BufferedReader( - new InputStreamReader(new FileInputStream(tempFile), Charset.defaultCharset())); + /** * Once control is in upload succeeded method automatically * upload button is re-enabled. To disable the button firing @@ -232,21 +234,16 @@ public class BulkUploadHandler extends CustomComponent // Clearing after assignments are done managementUIState.getTargetTableFilters().getBulkUpload().getTargetsCreated().clear(); - } catch (final FileNotFoundException e) { - LOG.error("File not found with name {}", tempFile.getName(), e); } catch (final IOException e) { LOG.error("Error reading file {}", tempFile.getName(), e); } finally { - try { - if (null != reader) { - reader.close(); - resetCounts(); - deleteFile(); - } - } catch (final IOException e) { - LOG.error("Error while reading file ", e); - } + resetCounts(); + deleteFile(); } + } catch (final FileNotFoundException e) { + LOG.error("Temporary file not found with name {}", tempFile.getName(), e); + } catch (final IOException e) { + LOG.error("Error while opening temorary file ", e); } } @@ -307,24 +304,19 @@ public class BulkUploadHandler extends CustomComponent } private double getTotalNumberOfLines() { - InputStreamReader inputStreamReader; - BufferedReader readerForSize = null; + double totalFileSize = 0; - try { - inputStreamReader = new InputStreamReader(new FileInputStream(tempFile), Charset.defaultCharset()); - readerForSize = new BufferedReader(inputStreamReader); - totalFileSize = readerForSize.lines().count(); + try (InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(tempFile), + Charset.defaultCharset())) { + try (BufferedReader readerForSize = new BufferedReader(inputStreamReader)) { + totalFileSize = readerForSize.lines().count(); + } } catch (final FileNotFoundException e) { LOG.error("Error reading file {}", tempFile.getName(), e); - } finally { - if (readerForSize != null) { - try { - readerForSize.close(); - } catch (final IOException e) { - LOG.error("Error while closing reader of file {}", tempFile.getName(), e); - } - } + } catch (final IOException e) { + LOG.error("Error while closing reader of file {}", tempFile.getName(), e); } + return totalFileSize; } @@ -449,7 +441,7 @@ public class BulkUploadHandler extends CustomComponent private static class NullOutputStream extends OutputStream { /** * null output stream. - * + * * @param i * byte */ @@ -468,7 +460,7 @@ public class BulkUploadHandler extends CustomComponent /* * (non-Javadoc) - * + * * @see * com.vaadin.ui.Upload.StartedListener#uploadStarted(com.vaadin.ui.Upload * .StartedEvent)