|
|
|
@ -18,37 +18,28 @@
|
|
|
|
|
|
|
|
|
|
package org.wso2.carbon.device.mgt.input.adapter.mqtt.util;
|
|
|
|
|
|
|
|
|
|
import org.wso2.carbon.base.ServerConfiguration;
|
|
|
|
|
import org.wso2.carbon.core.util.Utils;
|
|
|
|
|
import org.wso2.carbon.event.input.adapter.core.exception.InputEventAdapterException;
|
|
|
|
|
|
|
|
|
|
public class PropertyUtils {
|
|
|
|
|
private static final String MQTT_PORT = "\\$\\{mqtt.broker.port\\}";
|
|
|
|
|
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;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
//This method is only used if the mb features are within DAS.
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
class PropertyUtils {
|
|
|
|
|
|
|
|
|
|
private static int getPortOffset() {
|
|
|
|
|
ServerConfiguration carbonConfig = ServerConfiguration.getInstance();
|
|
|
|
|
String portOffset = System.getProperty("portOffset", carbonConfig.getFirstProperty(CARBON_CONFIG_PORT_OFFSET));
|
|
|
|
|
try {
|
|
|
|
|
if (portOffset != null) {
|
|
|
|
|
return Integer.parseInt(portOffset.trim());
|
|
|
|
|
//This method is only used if the mb features are within DAS.
|
|
|
|
|
static String replaceMqttProperty(String urlWithPlaceholders) throws InputEventAdapterException {
|
|
|
|
|
String regex = "\\$\\{(.*?)\\}";
|
|
|
|
|
Pattern pattern = Pattern.compile(regex);
|
|
|
|
|
Matcher matchPattern = pattern.matcher(urlWithPlaceholders);
|
|
|
|
|
while (matchPattern.find()) {
|
|
|
|
|
String sysPropertyName = matchPattern.group(1);
|
|
|
|
|
String sysPropertyValue = System.getProperty(sysPropertyName);
|
|
|
|
|
if (sysPropertyValue != null && !sysPropertyName.isEmpty()) {
|
|
|
|
|
urlWithPlaceholders = urlWithPlaceholders.replaceAll("\\$\\{(" + sysPropertyName + ")\\}", sysPropertyValue);
|
|
|
|
|
} 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|