From be99ca571b7b780239c1709801466a287b84c972 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Mon, 1 May 2017 18:54:01 +0530 Subject: [PATCH 1/3] #665 : Fixing java.lang.IllegalStateException: Tenant ID cannot be -1 issue --- .../device/mgt/input/adapter/mqtt/MQTTEventAdapter.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java index 68f515a3c..af5cb0c28 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java @@ -18,10 +18,9 @@ package org.wso2.carbon.device.mgt.input.adapter.mqtt; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.input.adapter.mqtt.internal.InputAdapterServiceDataHolder; import org.wso2.carbon.device.mgt.input.adapter.mqtt.util.MQTTAdapterListener; -import org.wso2.carbon.device.mgt.input.adapter.mqtt.util.MQTTEventAdapterConstants; import org.wso2.carbon.device.mgt.input.adapter.mqtt.util.MQTTBrokerConnectionConfiguration; +import org.wso2.carbon.device.mgt.input.adapter.mqtt.util.MQTTEventAdapterConstants; import org.wso2.carbon.event.input.adapter.core.InputEventAdapter; import org.wso2.carbon.event.input.adapter.core.InputEventAdapterConfiguration; import org.wso2.carbon.event.input.adapter.core.InputEventAdapterListener; @@ -58,6 +57,10 @@ public class MQTTEventAdapter implements InputEventAdapter { ,globalProperties); String topic = eventAdapterConfiguration.getProperties().get(MQTTEventAdapterConstants.ADAPTER_MESSAGE_TOPIC); String tenantDomain = topic.split("/")[0]; + if (tenantDomain.equals("${tenant-domain}")) { + tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + topic = topic.replaceAll("\\$\\{tenant-domain\\}", tenantDomain); + } mqttAdapterListener = new MQTTAdapterListener(mqttBrokerConnectionConfiguration ,topic ,eventAdapterConfiguration From f7dbc156b3fe6f819fa80a3de74b4d4a9759d862 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Mon, 1 May 2017 19:15:11 +0530 Subject: [PATCH 2/3] #665 : Added some changes to reuse PropertyUtils.replaceTenantDomainProperty() --- .../device/mgt/input/adapter/mqtt/MQTTEventAdapter.java | 4 ---- .../mgt/input/adapter/mqtt/util/MQTTAdapterListener.java | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java index af5cb0c28..f390cf3fc 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java @@ -57,10 +57,6 @@ public class MQTTEventAdapter implements InputEventAdapter { ,globalProperties); String topic = eventAdapterConfiguration.getProperties().get(MQTTEventAdapterConstants.ADAPTER_MESSAGE_TOPIC); String tenantDomain = topic.split("/")[0]; - if (tenantDomain.equals("${tenant-domain}")) { - tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); - topic = topic.replaceAll("\\$\\{tenant-domain\\}", tenantDomain); - } mqttAdapterListener = new MQTTAdapterListener(mqttBrokerConnectionConfiguration ,topic ,eventAdapterConfiguration diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java index 182b5c9a0..f9d62482e 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java @@ -88,7 +88,7 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { int keepAlive = mqttBrokerConnectionConfiguration.getKeepAlive(); this.topic = PropertyUtils.replaceTenantDomainProperty(topic); this.eventAdapterListener = inputEventAdapterListener; - this.tenantDomain = tenantDomain; + this.tenantDomain = PropertyUtils.replaceTenantDomainProperty(tenantDomain); //SORTING messages until the server fetches them String temp_directory = System.getProperty("java.io.tmpdir"); From 9d000381990dde3d0bb3cb8f874a4016cfd76460 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Mon, 1 May 2017 19:38:16 +0530 Subject: [PATCH 3/3] #665 : Added some improvements to the fix --- .../device/mgt/input/adapter/mqtt/MQTTEventAdapter.java | 3 +-- .../mgt/input/adapter/mqtt/util/MQTTAdapterListener.java | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java index f390cf3fc..eed3f82bc 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/MQTTEventAdapter.java @@ -56,11 +56,10 @@ public class MQTTEventAdapter implements InputEventAdapter { mqttBrokerConnectionConfiguration = new MQTTBrokerConnectionConfiguration(eventAdapterConfiguration ,globalProperties); String topic = eventAdapterConfiguration.getProperties().get(MQTTEventAdapterConstants.ADAPTER_MESSAGE_TOPIC); - String tenantDomain = topic.split("/")[0]; mqttAdapterListener = new MQTTAdapterListener(mqttBrokerConnectionConfiguration ,topic ,eventAdapterConfiguration - ,eventAdapterListener, tenantDomain); + ,eventAdapterListener); } catch (Throwable t) { throw new InputEventAdapterException(t.getMessage(), t); } diff --git a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java index f9d62482e..f7ea4d254 100644 --- a/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java +++ b/components/extensions/cdmf-transport-adapters/input/org.wso2.carbon.device.mgt.input.adapter.mqtt/src/main/java/org/wso2/carbon/device/mgt/input/adapter/mqtt/util/MQTTAdapterListener.java @@ -76,7 +76,7 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { public MQTTAdapterListener(MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String topic, InputEventAdapterConfiguration inputEventAdapterConfiguration, - InputEventAdapterListener inputEventAdapterListener, String tenantDomain) { + InputEventAdapterListener inputEventAdapterListener) { String mqttClientId = inputEventAdapterConfiguration.getProperties() .get(MQTTEventAdapterConstants.ADAPTER_CONF_CLIENTID); if(mqttClientId == null || mqttClientId.trim().isEmpty()){ @@ -88,7 +88,7 @@ public class MQTTAdapterListener implements MqttCallback, Runnable { int keepAlive = mqttBrokerConnectionConfiguration.getKeepAlive(); this.topic = PropertyUtils.replaceTenantDomainProperty(topic); this.eventAdapterListener = inputEventAdapterListener; - this.tenantDomain = PropertyUtils.replaceTenantDomainProperty(tenantDomain); + this.tenantDomain = this.topic.split("/")[0]; //SORTING messages until the server fetches them String temp_directory = System.getProperty("java.io.tmpdir");