From 557225447f85ca928632e139032263066d56cc47 Mon Sep 17 00:00:00 2001 From: Shabirmean Date: Fri, 8 Jan 2016 11:59:25 +0530 Subject: [PATCH] Fixing JIRA: https://wso2.org/jira/browse/IOTS-12 (Allows spaces in the file Path to Agent Configs) --- .../agent/core/AgentConstants.java | 12 ---- .../agent/core/AgentManager.java | 10 +++- .../agent/core/AgentUtilOperations.java | 60 +++++-------------- .../transport/mqtt/MQTTTransportHandler.java | 8 +++ 4 files changed, 32 insertions(+), 58 deletions(-) diff --git a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentConstants.java b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentConstants.java index 22ee97bc9b..f5dd270230 100644 --- a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentConstants.java +++ b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentConstants.java @@ -84,18 +84,6 @@ public class AgentConstants { /* --------------------------------------------------------------------------------------- Default values for the Device/Agent specific configurations listed above --------------------------------------------------------------------------------------- */ - public static final String DEFAULT_SERVER_NAME = "WSO2IoTServer"; - public static final String DEFAULT_DEVICE_OWNER = "admin"; - public static final String DEFAULT_DEVICE_ID = "1234567890"; - public static final String DEFAULT_DEVICE_NAME = "admin_1234567890"; - public static final String DEFAULT_HTTPS_SERVER_EP = "https://localhost:9443"; - public static final String DEFAULT_HTTP_SERVER_EP = "http://localhost:9763"; - public static final String DEFAULT_APIM_GATEWAY_EP = "http://127.0.0.1:8281"; - public static final String DEFAULT_MQTT_BROKER_EP = "tcp://127.0.0.1:1883"; - public static final String DEFAULT_XMPP_SERVER_EP = "http://127.0.0.1:9061"; - public static final String DEFAULT_AUTH_METHOD = "token"; - public static final String DEFAULT_AUTH_TOKEN = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321"; - public static final String DEFAULT_REFRESH_TOKEN = "1234567890ZYXWVUTSRQPONMKLJIHGFEDCBA"; public static final String DEFAULT_NETWORK_INTERFACE = "en0"; public static final int DEFAULT_DATA_PUBLISH_INTERVAL = 15; // seconds public static final String DEFAULT_PROTOCOL = "MQTT"; diff --git a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentManager.java b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentManager.java index af814a2c67..b1d204d62d 100644 --- a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentManager.java +++ b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentManager.java @@ -76,7 +76,13 @@ public class AgentManager { agentCommunicator = new HashMap<>(); // Read IoT-Server specific configurations from the 'deviceConfig.properties' file - this.agentConfigs = AgentUtilOperations.readIoTServerConfigs(); + try { + this.agentConfigs = AgentUtilOperations.readIoTServerConfigs(); + } catch (AgentCoreOperationException e) { + log.error("Reading device configuration from configd file failed:\n"); + log.error(e); + System.exit(0); + } // Initialise IoT-Server URL endpoints from the configuration read from file AgentUtilOperations.initializeServerEndPoints(); @@ -153,7 +159,7 @@ public class AgentManager { EnrollmentManager.getInstance().beginEnrollmentFlow(); } catch (AgentCoreOperationException e) { log.error("Device Enrollment Failed:\n"); - e.printStackTrace(); + log.error(e); System.exit(0); } diff --git a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentUtilOperations.java b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentUtilOperations.java index cc3f456766..d12baaa2b3 100644 --- a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentUtilOperations.java +++ b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentUtilOperations.java @@ -26,11 +26,14 @@ import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCore import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.CommunicationUtils; import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException; +import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Properties; @@ -56,7 +59,7 @@ public class AgentUtilOperations { * @return an object of type 'AgentConfiguration' which contains all the necessary * configuration attributes */ - public static AgentConfiguration readIoTServerConfigs() { + public static AgentConfiguration readIoTServerConfigs() throws AgentCoreOperationException { AgentManager agentManager = AgentManager.getInstance(); AgentConfiguration iotServerConfigs = new AgentConfiguration(); Properties properties = new Properties(); @@ -67,14 +70,14 @@ public class AgentUtilOperations { ClassLoader loader = AgentUtilOperations.class.getClassLoader(); URL path = loader.getResource(propertiesFileName); System.out.println(path); - String root = path.getPath().replace( - "wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", - "").replace("jar:", "").replace("file:", ""); + String root = path.getPath().replace("wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", "") + .replace("jar:", "").replace("file:", ""); + root = URLDecoder.decode(root, StandardCharsets.UTF_8.toString()); agentManager.setRootPath(root); - propertiesInputStream = new FileInputStream( - root + AgentConstants.AGENT_PROPERTIES_FILE_NAME); + String deviceConfigFilePath = root + AgentConstants.AGENT_PROPERTIES_FILE_NAME; + propertiesInputStream = new FileInputStream(deviceConfigFilePath); //load a properties file from class path, inside static method properties.load(propertiesInputStream); @@ -137,16 +140,15 @@ public class AgentUtilOperations { iotServerConfigs.getDataPushInterval()); } catch (FileNotFoundException ex) { - log.error(AgentConstants.LOG_APPENDER + "Unable to find " + propertiesFileName + - " file at: " + AgentConstants.PROPERTIES_FILE_PATH); - iotServerConfigs = setDefaultDeviceConfigs(); + String errorMsg = "[" + propertiesFileName + "] file not found at: " + AgentConstants.PROPERTIES_FILE_PATH; + log.error(AgentConstants.LOG_APPENDER + errorMsg); + throw new AgentCoreOperationException(errorMsg); } catch (IOException ex) { - log.error(AgentConstants.LOG_APPENDER + "Error occurred whilst trying to fetch '" + - propertiesFileName + "' from: " + - AgentConstants.PROPERTIES_FILE_PATH); - iotServerConfigs = setDefaultDeviceConfigs(); - + String errorMsg = "Error occurred whilst trying to fetch [" + propertiesFileName + "] from: " + + AgentConstants.PROPERTIES_FILE_PATH; + log.error(AgentConstants.LOG_APPENDER + errorMsg); + throw new AgentCoreOperationException(errorMsg); } finally { if (propertiesInputStream != null) { try { @@ -161,36 +163,6 @@ public class AgentUtilOperations { return iotServerConfigs; } - /** - * Sets the default Device specific configurations listed in the 'AgentConstants' class. - * - * @return an object of AgentConfiguration class including all default device specific configs. - */ - private static AgentConfiguration setDefaultDeviceConfigs() { - log.warn(AgentConstants.LOG_APPENDER + - "Default Values are being set to all Agent specific configurations"); - - AgentConfiguration iotServerConfigs = new AgentConfiguration(); - - iotServerConfigs.setDeviceOwner(AgentConstants.DEFAULT_SERVER_NAME); - iotServerConfigs.setDeviceOwner(AgentConstants.DEFAULT_DEVICE_OWNER); - iotServerConfigs.setDeviceId(AgentConstants.DEFAULT_DEVICE_ID); - iotServerConfigs.setDeviceName(AgentConstants.DEFAULT_DEVICE_NAME); - iotServerConfigs.setControllerContext(AgentConstants.DEVICE_CONTROLLER_API_EP); - iotServerConfigs.setHTTPS_ServerEndpoint(AgentConstants.DEFAULT_HTTPS_SERVER_EP); - iotServerConfigs.setHTTP_ServerEndpoint(AgentConstants.DEFAULT_HTTP_SERVER_EP); - iotServerConfigs.setApimGatewayEndpoint(AgentConstants.DEFAULT_APIM_GATEWAY_EP); - iotServerConfigs.setMqttBrokerEndpoint(AgentConstants.DEFAULT_MQTT_BROKER_EP); - iotServerConfigs.setXmppServerEndpoint(AgentConstants.DEFAULT_XMPP_SERVER_EP); - iotServerConfigs.setAuthMethod(AgentConstants.DEFAULT_AUTH_METHOD); - iotServerConfigs.setAuthToken(AgentConstants.DEFAULT_AUTH_TOKEN); - iotServerConfigs.setRefreshToken(AgentConstants.DEFAULT_REFRESH_TOKEN); - iotServerConfigs.setDataPushInterval(AgentConstants.DEFAULT_DATA_PUBLISH_INTERVAL); - - return iotServerConfigs; - } - - /** * This method constructs the URLs for each of the API Endpoints called by the device agent * Ex: Register API, Push-Data API diff --git a/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/transport/mqtt/MQTTTransportHandler.java b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/transport/mqtt/MQTTTransportHandler.java index a6363be01c..5f3c54a439 100644 --- a/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/transport/mqtt/MQTTTransportHandler.java +++ b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot/src/main/java/org/wso2/carbon/device/mgt/iot/transport/mqtt/MQTTTransportHandler.java @@ -27,6 +27,7 @@ import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.MqttSecurityException; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.iot.transport.TransportHandler; import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException; @@ -315,6 +316,13 @@ public abstract class MQTTTransportHandler log.debug("Got an MQTT message '" + mqttMessage.toString() + "' for topic '" + topic + "'."); } + + try { + throw new DeviceManagementException("Test Exception"); + } catch (DeviceManagementException e) { + e.printStackTrace(); + } + Thread messageProcessorThread = new Thread() { public void run() { try {