[incubator-plc4x] 02/02: Fixed some SonarQube findings

2018-05-25 Thread cdutz
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

commit 0b770620d4e02c5950229dbfbd08ebda79db72cc
Author: Christofer Dutz 
AuthorDate: Fri May 25 13:56:03 2018 +0200

Fixed some SonarQube findings
---
 .../iotfactory/IotElasticsearchFactory.java| 31 --
 1 file changed, 23 insertions(+), 8 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 8f70545..a89ea50 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
@@ -54,6 +54,15 @@ public class IotElasticsearchFactory {
 RUNNING_RIGHT
 }
 
+private static class IotElasticsearchFactoryException extends 
RuntimeException {
+private IotElasticsearchFactoryException(String message) {
+super(message);
+}
+private IotElasticsearchFactoryException(String message, Throwable 
cause) {
+super(message, cause);
+}
+}
+
 private static class MyNode extends Node {
 private MyNode(Settings preparedSettings, Collection> classpathPlugins) {
 
super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null), 
classpathPlugins);
@@ -91,7 +100,7 @@ public class IotElasticsearchFactory {
 "}", XContentType.JSON);
 CreateIndexResponse createIndexResponse = 
indicesAdminClient.create(createIndexRequest).actionGet();
 if(!createIndexResponse.isAcknowledged()) {
-throw new RuntimeException("Could not create index 
'iot-factory-data'");
+throw new IotElasticsearchFactoryException("Could not create 
index 'iot-factory-data'");
 }
 }
 
@@ -113,7 +122,7 @@ public class IotElasticsearchFactory {
 "}", XContentType.JSON);
 CreateIndexResponse createIndexResponse = 
indicesAdminClient.create(createIndexRequest).actionGet();
 if(!createIndexResponse.isAcknowledged()) {
-throw new RuntimeException("Could not create index 
'iot-product-data'");
+throw new IotElasticsearchFactoryException("Could not create 
index 'iot-product-data'");
 }
 }
 }
@@ -167,7 +176,7 @@ public class IotElasticsearchFactory {
 boolean conveyorRight = (input & 64) != 0;
 
 try {
-return XContentFactory.jsonBuilder()
+XContentBuilder builder = XContentFactory.jsonBuilder()
 .startObject()
 .field("time", Calendar.getInstance().getTimeInMillis())
 .field("conveyorEntry", conveyorEntry)
@@ -178,8 +187,10 @@ public class IotElasticsearchFactory {
 .field( "conveyorLeft", conveyorLeft)
 .field( "conveyorRight", conveyorRight)
 .endObject();
+builder.close();
+return builder;
 } catch (IOException e) {
-throw new RuntimeException("Error building JSON message.", e);
+throw new IotElasticsearchFactoryException("Error building JSON 
message.", e);
 }
 }
 
@@ -191,24 +202,28 @@ public class IotElasticsearchFactory {
 if (transferLeft) {
 conveyorState = ConveyorState.RUNNING_LEFT;
 try {
-return XContentFactory.jsonBuilder()
+XContentBuilder builder = XContentFactory.jsonBuilder()
 .startObject()
 .field("time", 
Calendar.getInstance().getTimeInMillis())
 .field("type", "small")
 .endObject();
+builder.close();
+return builder;
 } catch (IOException e) {
-throw new RuntimeException("Error building JSON message.", 
e);
+throw new IotElasticsearchFactoryException("Error building 
JSON message.", e);
 }
 } else if (transferRight){
 conveyorState = ConveyorState.RUNNING_RIGHT;
 try {
-return XContentFactory.jsonBuilder()
+XContentBuilder builder = XContentFactory.jsonBuilder()
 .startObject()
 .field("time", 
Calendar.getInstance().getTimeInMillis())
 .field("type", "large")
 .endObject();
+builder.close();
+return builder;
 } catch (IOException e) {
-thr

[incubator-plc4x] 02/02: Fixed some SonarQube findings

2018-05-25 Thread cdutz
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

commit 7fbe0ec2aafdeb6865f64b46ddc340a357c9431a
Author: Christofer Dutz 
AuthorDate: Fri May 25 11:09:11 2018 +0200

Fixed some SonarQube findings
---
 .../iotfactory/IotElasticsearchFactory.java| 153 +++--
 1 file changed, 77 insertions(+), 76 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 1a0c225..8f70545 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
@@ -54,6 +54,12 @@ public class IotElasticsearchFactory {
 RUNNING_RIGHT
 }
 
+private static class MyNode extends Node {
+private MyNode(Settings preparedSettings, Collection> classpathPlugins) {
+
super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null), 
classpathPlugins);
+}
+}
+
 private ConveyorState conveyorState = ConveyorState.STOPPED;
 
 private Node startElasticsearchNode() throws NodeValidationException {
@@ -67,14 +73,14 @@ public class IotElasticsearchFactory {
 return node;
 }
 
-private void prepareIndexes(Client esClient) throws Exception {
+private void prepareIndexes(Client esClient) {
 IndicesAdminClient indicesAdminClient = esClient.admin().indices();
 
 // Check if the factory-data index exists and create it, if it doesn't.
 IndicesExistsRequest factoryDataIndexExistsRequest =
-indicesAdminClient.prepareExists("product-data").request();
+indicesAdminClient.prepareExists("iot-factory-data").request();
 
if(!indicesAdminClient.exists(factoryDataIndexExistsRequest).actionGet().isExists())
 {
-CreateIndexRequest createIndexRequest = new 
CreateIndexRequest("factory-data");
+CreateIndexRequest createIndexRequest = new 
CreateIndexRequest("iot-factory-data");
 createIndexRequest.mapping("FactoryData",
 "{\n" +
 "\"properties\": {\n" +
@@ -85,15 +91,15 @@ public class IotElasticsearchFactory {
 "}", XContentType.JSON);
 CreateIndexResponse createIndexResponse = 
indicesAdminClient.create(createIndexRequest).actionGet();
 if(!createIndexResponse.isAcknowledged()) {
-throw new Exception("Could not create index 'product-data'");
+throw new RuntimeException("Could not create index 
'iot-factory-data'");
 }
 }
 
 // Check if the product-data index exists and create it, if it doesn't.
 IndicesExistsRequest productDataIndexExistsRequest =
-indicesAdminClient.prepareExists("product-data").request();
+indicesAdminClient.prepareExists("iot-product-data").request();
 
if(!indicesAdminClient.exists(productDataIndexExistsRequest).actionGet().isExists())
 {
-CreateIndexRequest createIndexRequest = new 
CreateIndexRequest("product-data");
+CreateIndexRequest createIndexRequest = new 
CreateIndexRequest("iot-product-data");
 createIndexRequest.mapping("ProductData",
 "{\n" +
 "\"properties\": {\n" +
@@ -107,7 +113,7 @@ public class IotElasticsearchFactory {
 "}", XContentType.JSON);
 CreateIndexResponse createIndexResponse = 
indicesAdminClient.create(createIndexRequest).actionGet();
 if(!createIndexResponse.isAcknowledged()) {
-throw new Exception("Could not create index 'product-data'");
+throw new RuntimeException("Could not create index 
'iot-product-data'");
 }
 }
 }
@@ -134,75 +140,16 @@ public class IotElasticsearchFactory {
 TStream plcOutputStates = top.poll(plcSupplier, 100, 
TimeUnit.MILLISECONDS);
 
 // 3a) Create a stream that pumps all data into a 'factory-data' 
index.
-TStream factoryData = plcOutputStates.map(value 
-> {
-boolean conveyorEntry = (value & 1) != 0;
-boolean load = (value & 2) != 0;
-boolean unload = (value & 4) != 0;
-boolean transferLeft = (value & 8) != 0;
-boolean transferRight = (value & 16) != 0;
-boolean conveyorLeft = (value & 32) != 0;
-boolean conveyorRight = (value & 64) != 0;
-
-try {
-return XContentFactory.jsonBuilder()
-.startObject()
-