This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/master by this push:
     new bbd4737  Fixed some SonarQube findings
bbd4737 is described below

commit bbd4737f23ee8ae46d5f07c8087c7bae0f214699
Author: Christofer Dutz <christofer.d...@c-ware.de>
AuthorDate: Fri May 25 14:30:08 2018 +0200

    Fixed some SonarQube findings
---
 .../iotfactory/IotElasticsearchFactory.java        | 74 +++++++++++-----------
 1 file changed, 36 insertions(+), 38 deletions(-)

diff --git 
a/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotElasticsearchFactory.java
 
b/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotElasticsearchFactory.java
index a89ea50..2544140 100644
--- 
a/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotElasticsearchFactory.java
+++ 
b/examples/iot-factory/src/main/java/org/apache/plc4x/java/examples/iotfactory/IotElasticsearchFactory.java
@@ -71,15 +71,19 @@ public class IotElasticsearchFactory {
 
     private ConveyorState conveyorState = ConveyorState.STOPPED;
 
-    private Node startElasticsearchNode() throws NodeValidationException {
-        Node node = new MyNode(Settings.builder()
-            .put("transport.type", "netty4")
-            .put("http.type", "netty4")
-            .put("http.enabled", "true")
-            .put("path.home", "elasticsearch-data")
-            .build(), Collections.singletonList(Netty4Plugin.class));
-        node.start();
-        return node;
+    private Node startElasticsearchNode() {
+        try {
+            Node node = new MyNode(Settings.builder()
+                .put("transport.type", "netty4")
+                .put("http.type", "netty4")
+                .put("http.enabled", "true")
+                .put("path.home", "elasticsearch-data")
+                .build(), Collections.singletonList(Netty4Plugin.class));
+            node.start();
+            return node;
+        } catch (NodeValidationException e) {
+            throw new IotElasticsearchFactoryException("Could not start 
Elasticsearch node.", e);
+        }
     }
 
     private void prepareIndexes(Client esClient) {
@@ -127,7 +131,7 @@ public class IotElasticsearchFactory {
         }
     }
 
-    private void runFactory() throws Exception {
+    private void runFactory() {
         // Start an Elasticsearch node.
         Node esNode = startElasticsearchNode();
         Client esClient = esNode.client();
@@ -175,19 +179,17 @@ public class IotElasticsearchFactory {
         boolean conveyorLeft = (input & 32) != 0;
         boolean conveyorRight = (input & 64) != 0;
 
-        try {
-            XContentBuilder builder = XContentFactory.jsonBuilder()
-                .startObject()
-                .field("time", Calendar.getInstance().getTimeInMillis())
-                .field("conveyorEntry", conveyorEntry)
-                .field("load", load)
-                .field( "unload", unload)
-                .field( "transferLeft", transferLeft)
-                .field( "transferRight", transferRight)
-                .field( "conveyorLeft", conveyorLeft)
-                .field( "conveyorRight", conveyorRight)
-                .endObject();
-            builder.close();
+        try(XContentBuilder builder = XContentFactory.jsonBuilder()
+            .startObject()
+            .field("time", Calendar.getInstance().getTimeInMillis())
+            .field("conveyorEntry", conveyorEntry)
+            .field("load", load)
+            .field( "unload", unload)
+            .field( "transferLeft", transferLeft)
+            .field( "transferRight", transferRight)
+            .field( "conveyorLeft", conveyorLeft)
+            .field( "conveyorRight", conveyorRight)
+            .endObject()) {
             return builder;
         } catch (IOException e) {
             throw new IotElasticsearchFactoryException("Error building JSON 
message.", e);
@@ -201,26 +203,22 @@ public class IotElasticsearchFactory {
         if (conveyorState == ConveyorState.STOPPED) {
             if (transferLeft) {
                 conveyorState = ConveyorState.RUNNING_LEFT;
-                try {
-                    XContentBuilder builder = XContentFactory.jsonBuilder()
-                        .startObject()
-                        .field("time", 
Calendar.getInstance().getTimeInMillis())
-                        .field("type", "small")
-                        .endObject();
-                    builder.close();
+                try (XContentBuilder builder = XContentFactory.jsonBuilder()
+                    .startObject()
+                    .field("time", Calendar.getInstance().getTimeInMillis())
+                    .field("type", "small")
+                    .endObject()) {
                     return builder;
                 } catch (IOException e) {
                     throw new IotElasticsearchFactoryException("Error building 
JSON message.", e);
                 }
             } else if (transferRight){
                 conveyorState = ConveyorState.RUNNING_RIGHT;
-                try {
-                    XContentBuilder builder = XContentFactory.jsonBuilder()
-                        .startObject()
-                        .field("time", 
Calendar.getInstance().getTimeInMillis())
-                        .field("type", "large")
-                        .endObject();
-                    builder.close();
+                try (XContentBuilder builder = XContentFactory.jsonBuilder()
+                    .startObject()
+                    .field("time", Calendar.getInstance().getTimeInMillis())
+                    .field("type", "large")
+                    .endObject()) {
                     return builder;
                 } catch (IOException e) {
                     throw new IotElasticsearchFactoryException("Error building 
JSON message.", e);
@@ -232,7 +230,7 @@ public class IotElasticsearchFactory {
         return null;
     }
 
-    public static void main(String[] args) throws Exception {
+    public static void main(String[] args) {
         IotElasticsearchFactory factory = new IotElasticsearchFactory();
         factory.runFactory();
     }

-- 
To stop receiving notification emails like this one, please contact
cd...@apache.org.

Reply via email to