Skip to content

Commit

Permalink
[DSIP-84][Feature] Update the connection address of Caché datasource(#…
Browse files Browse the repository at this point in the history
  • Loading branch information
edclol committed Nov 26, 2024
1 parent fb2881a commit 7012bdd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,30 @@ public BaseDataSourceParamDTO createDatasourceParamDTO(String connectionJson) {
mysqlDatasourceParamDTO.setUserName(connectionParams.getUser());
mysqlDatasourceParamDTO.setDatabase(connectionParams.getDatabase());
String address = connectionParams.getAddress();
try {
String[] hostPortSplit = getHostPortSplit(address);
mysqlDatasourceParamDTO.setPort(Integer.parseInt(hostPortSplit[1]));
mysqlDatasourceParamDTO.setHost(hostPortSplit[0]);
} catch (NumberFormatException e) {
log.error("Invalid port number", e);
}
return mysqlDatasourceParamDTO;
}

public static String[] getHostPortSplit(String address) {
String[] hostSeperator = address.split(Constants.DOUBLE_SLASH);
if (hostSeperator.length < 2) {
log.error("Invalid address format");
}
String[] hostPortArray = hostSeperator[hostSeperator.length - 1].split(Constants.COMMA);
mysqlDatasourceParamDTO.setPort(Integer.parseInt(hostPortArray[0].split(Constants.COLON)[1]));
mysqlDatasourceParamDTO.setHost(hostPortArray[0].split(Constants.COLON)[0]);

return mysqlDatasourceParamDTO;
if (hostPortArray.length < 1) {
log.error("Invalid hostPortArray format");
}
String[] hostPortSplit = hostPortArray[0].split(Constants.COLON);
if (hostPortSplit.length < 2) {
log.error("Invalid hostPortSplit format");
}
return hostPortSplit;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.dolphinscheduler.plugin.datasource.api.utils.PasswordUtils;
import org.apache.dolphinscheduler.spi.enums.DbType;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -126,4 +127,20 @@ void testGetDatasourceUniqueId() {
}
}

@Test
void testGetHostPortSplit() {
String address = "jdbc:Cache://localhost:1972";
String[] expected = {"localhost", "1972"};
String[] result = CacheDataSourceProcessor.getHostPortSplit(address);
Assertions.assertArrayEquals(expected, result);
}

@Test
void testGetHostPortSplit_InvalidFormat() {
String invalidAddress = "invalid-format";
String[] hostPortSplit = CacheDataSourceProcessor.getHostPortSplit(invalidAddress);
Assertions.assertEquals("[invalid-format]", Arrays.toString(hostPortSplit));

}

}

0 comments on commit 7012bdd

Please sign in to comment.