|
|
@ -18,37 +18,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.wso2.carbon.device.mgt.input.adapter.mqtt.util;
|
|
|
|
package org.wso2.carbon.device.mgt.input.adapter.mqtt.util;
|
|
|
|
|
|
|
|
|
|
|
|
import org.wso2.carbon.base.ServerConfiguration;
|
|
|
|
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterException;
|
|
|
|
import org.wso2.carbon.core.util.Utils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class PropertyUtils {
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
private static final String MQTT_PORT = "\\$\\{mqtt.broker.port\\}";
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
private static final String MQTT_BROKER_HOST = "\\$\\{mqtt.broker.host\\}";
|
|
|
|
|
|
|
|
private static final String CARBON_CONFIG_PORT_OFFSET = "Ports.Offset";
|
|
|
|
|
|
|
|
private static final String DEFAULT_CARBON_LOCAL_IP_PROPERTY = "carbon.local.ip";
|
|
|
|
|
|
|
|
private static final int CARBON_DEFAULT_PORT_OFFSET = 0;
|
|
|
|
|
|
|
|
private static final int DEFAULT_MQTT_PORT = 1886;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//This method is only used if the mb features are within DAS.
|
|
|
|
class PropertyUtils {
|
|
|
|
public static String replaceMqttProperty (String urlWithPlaceholders) {
|
|
|
|
|
|
|
|
urlWithPlaceholders = Utils.replaceSystemProperty(urlWithPlaceholders);
|
|
|
|
|
|
|
|
urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_PORT, "" + (DEFAULT_MQTT_PORT + getPortOffset()));
|
|
|
|
|
|
|
|
urlWithPlaceholders = urlWithPlaceholders.replaceAll(MQTT_BROKER_HOST, System.getProperty(
|
|
|
|
|
|
|
|
DEFAULT_CARBON_LOCAL_IP_PROPERTY, "localhost"));
|
|
|
|
|
|
|
|
return urlWithPlaceholders;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static int getPortOffset() {
|
|
|
|
//This method is only used if the mb features are within DAS.
|
|
|
|
ServerConfiguration carbonConfig = ServerConfiguration.getInstance();
|
|
|
|
static String replaceMqttProperty(String urlWithPlaceholders) throws InputEventAdapterException {
|
|
|
|
String portOffset = System.getProperty("portOffset", carbonConfig.getFirstProperty(CARBON_CONFIG_PORT_OFFSET));
|
|
|
|
String regex = "\\$\\{(.*?)\\}";
|
|
|
|
try {
|
|
|
|
Pattern pattern = Pattern.compile(regex);
|
|
|
|
if (portOffset != null) {
|
|
|
|
Matcher matchPattern = pattern.matcher(urlWithPlaceholders);
|
|
|
|
return Integer.parseInt(portOffset.trim());
|
|
|
|
while (matchPattern.find()) {
|
|
|
|
|
|
|
|
String sysPropertyName = matchPattern.group(1);
|
|
|
|
|
|
|
|
String sysPropertyValue = System.getProperty(sysPropertyName);
|
|
|
|
|
|
|
|
if (sysPropertyValue != null && !sysPropertyName.isEmpty()) {
|
|
|
|
|
|
|
|
urlWithPlaceholders = urlWithPlaceholders.replaceAll("\\$\\{(" + sysPropertyName + ")\\}", sysPropertyValue);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return CARBON_DEFAULT_PORT_OFFSET;
|
|
|
|
throw new InputEventAdapterException("System property - " + sysPropertyName
|
|
|
|
|
|
|
|
+ " is not defined, hence cannot resolve : " + urlWithPlaceholders);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
|
|
|
return CARBON_DEFAULT_PORT_OFFSET;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return urlWithPlaceholders;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|