Support parse ipv6 (#14584)
This commit is contained in:
parent
20cf4ad4e6
commit
93c3871925
|
|
@ -183,7 +183,7 @@ public class LoggerServiceTest {
|
|||
.thenReturn(new byte[0]);
|
||||
Mockito.when(projectMapper.queryProjectByTaskInstanceId(1)).thenReturn(project);
|
||||
byte[] result = loggerService.getLogBytes(loginUser, 1);
|
||||
Assertions.assertEquals(90, result.length);
|
||||
Assertions.assertEquals(62, result.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -109,10 +109,10 @@ public class RegistryClient {
|
|||
// todo: add host, port in heartBeat Info, so that we don't need to parse this again
|
||||
server.setZkDirectory(registryNodeType.getRegistryPath() + "/" + serverPath);
|
||||
// set host and port
|
||||
String[] hostAndPort = serverPath.split(Constants.COLON);
|
||||
int lastColonIndex = serverPath.lastIndexOf(":");
|
||||
// fetch the last one
|
||||
server.setHost(hostAndPort[0]);
|
||||
server.setPort(Integer.parseInt(hostAndPort[1]));
|
||||
server.setHost(serverPath.substring(0, lastColonIndex));
|
||||
server.setPort(Integer.parseInt(serverPath.substring(lastColonIndex + 1)));
|
||||
serverList.add(server);
|
||||
}
|
||||
return serverList;
|
||||
|
|
|
|||
|
|
@ -17,33 +17,20 @@
|
|||
|
||||
package org.apache.dolphinscheduler.remote.utils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import static org.apache.dolphinscheduler.common.constants.Constants.COLON;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NonNull;
|
||||
|
||||
/**
|
||||
* server address
|
||||
*/
|
||||
@Data
|
||||
public class Host implements Serializable {
|
||||
|
||||
private static final String COLON = ":";
|
||||
|
||||
public static final Host EMPTY = new Host();
|
||||
|
||||
/**
|
||||
* address
|
||||
*/
|
||||
private String address;
|
||||
|
||||
/**
|
||||
* ip
|
||||
*/
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* port
|
||||
*/
|
||||
private int port;
|
||||
|
||||
public Host() {
|
||||
|
|
@ -52,107 +39,23 @@ public class Host implements Serializable {
|
|||
public Host(String ip, int port) {
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
this.address = ip + COLON + port;
|
||||
}
|
||||
|
||||
public Host(String address) {
|
||||
String[] parts = splitAddress(address);
|
||||
this.ip = parts[0];
|
||||
this.port = Integer.parseInt(parts[1]);
|
||||
this.address = address;
|
||||
int lastColonIndex = address.lastIndexOf(COLON);
|
||||
if (lastColonIndex < 0) {
|
||||
throw new IllegalArgumentException(String.format("Host : %s illegal.", address));
|
||||
}
|
||||
this.ip = address.substring(0, lastColonIndex);
|
||||
this.port = Integer.parseInt(address.substring(lastColonIndex + 1));
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
return ip + COLON + port;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
String[] parts = splitAddress(address);
|
||||
this.ip = parts[0];
|
||||
this.port = Integer.parseInt(parts[1]);
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
this.address = ip + COLON + port;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
this.address = ip + COLON + port;
|
||||
}
|
||||
|
||||
/**
|
||||
* address convert host
|
||||
*
|
||||
* @param address address
|
||||
* @return host
|
||||
*/
|
||||
public static Host of(@NonNull String address) {
|
||||
String[] parts = splitAddress(address);
|
||||
return new Host(parts[0], Integer.parseInt(parts[1]));
|
||||
return new Host(address);
|
||||
}
|
||||
|
||||
/**
|
||||
* address convert host
|
||||
*
|
||||
* @param address address
|
||||
* @return host
|
||||
*/
|
||||
public static String[] splitAddress(String address) {
|
||||
if (address == null) {
|
||||
throw new IllegalArgumentException("Host : address is null.");
|
||||
}
|
||||
String[] parts = address.split(COLON);
|
||||
if (parts.length != 2) {
|
||||
throw new IllegalArgumentException(String.format("Host : %s illegal.", address));
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* whether old version
|
||||
*
|
||||
* @param address address
|
||||
* @return old version is true , otherwise is false
|
||||
*/
|
||||
public static Boolean isOldVersion(String address) {
|
||||
String[] parts = address.split(COLON);
|
||||
return parts.length != 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Host{"
|
||||
+ "address='" + address + '\''
|
||||
+ ", ip='" + ip + '\''
|
||||
+ ", port=" + port
|
||||
+ '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Host host = (Host) o;
|
||||
return port == host.port && Objects.equals(address, host.address) && Objects.equals(ip, host.ip);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(address, ip, port);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,9 +29,13 @@ public class HostTest {
|
|||
public void testHost() {
|
||||
Host host = Host.of("192.158.2.2:22");
|
||||
Assertions.assertEquals(22, host.getPort());
|
||||
host.setAddress("127.0.0.1:8888");
|
||||
host = new Host("127.0.0.1:8888");
|
||||
Assertions.assertEquals("127.0.0.1", host.getIp());
|
||||
Assertions.assertEquals(8888, host.getPort());
|
||||
|
||||
host = new Host("2001:db8:1::ab9:C0A8:102:5678");
|
||||
Assertions.assertEquals("2001:db8:1::ab9:C0A8:102", host.getIp());
|
||||
Assertions.assertEquals(5678, host.getPort());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue