|
| 1 | +package de.codecentric.boot.admin.server.services; |
| 2 | + |
| 3 | +import de.codecentric.boot.admin.server.domain.values.InstanceId; |
| 4 | +import de.codecentric.boot.admin.server.domain.values.Registration; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | + |
| 7 | +import java.io.UnsupportedEncodingException; |
| 8 | +import java.net.MalformedURLException; |
| 9 | +import java.net.URL; |
| 10 | +import java.net.URLEncoder; |
| 11 | +@Slf4j |
| 12 | +public class HostPortPidInstanceIdGenerator implements InstanceIdGenerator { |
| 13 | + private static final char _ = '_'; |
| 14 | + private static final char point = '.'; |
| 15 | + private HashingInstanceUrlIdGenerator hashingInstanceUrlIdGenerator |
| 16 | + = new HashingInstanceUrlIdGenerator(); |
| 17 | + @Override |
| 18 | + public InstanceId generateId(Registration registration) { |
| 19 | + URL serviceUrl; |
| 20 | + try { |
| 21 | + serviceUrl = new URL(registration.getServiceUrl()); |
| 22 | + } catch (MalformedURLException e) { |
| 23 | + log.error("Malformed service url {}, fallback to hashing", registration.getServiceUrl()); |
| 24 | + return hashingInstanceUrlIdGenerator.generateId(registration); |
| 25 | + } |
| 26 | + StringBuilder stringBuilder = |
| 27 | + new StringBuilder(registration.getName()) |
| 28 | + .append(_).append(serviceUrl.getProtocol()) |
| 29 | + .append(_).append(serviceUrl.getHost().replace(point, _)) |
| 30 | + .append(_).append(serviceUrl.getPort()) |
| 31 | + |
| 32 | + ; |
| 33 | + String encode; |
| 34 | + String id = stringBuilder.toString(); |
| 35 | + try { |
| 36 | + encode = URLEncoder.encode(id, "utf-8"); |
| 37 | + return InstanceId.of(encode); |
| 38 | + } catch (UnsupportedEncodingException e) { |
| 39 | + log.error("Failed to encode id of {}, fallback to hashing", id); |
| 40 | + return hashingInstanceUrlIdGenerator.generateId(registration); |
| 41 | + } |
| 42 | + |
| 43 | + } |
| 44 | +} |
0 commit comments