Last active
June 30, 2016 15:23
-
-
Save pferraro/d970617a4c3934276dd9a9a8ca274a7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RouteBuilder implements Builder<String>, Value<String> { | |
private static final String DELIMITER = ":"; | |
private final String serverName; | |
private final InjectedValue<UndertowService> service = new InjectedValue<>(); | |
public RouteBuilder(String serverName) { | |
this.serverName = serverName; | |
} | |
@Override | |
public ServiceName getServiceName() { | |
return UndertowService.SERVER.append(this.serverName, "route"); | |
} | |
@Override | |
public ServiceBuilder<String> build(ServiceTarget target) { | |
return target.addService(this.getServiceName(), new ValueService<>(this)) | |
.addDependency(UndertowService.UNDERTOW, UndertowService.class, this.service) | |
.setInitialMode(ServiceController.Mode.ON_DEMAND) | |
; | |
} | |
@Override | |
public String getValue() { | |
UndertowService service = this.service.getValue(); | |
String instanceId = service.getInstanceId(); | |
// Don't append server name for the default server | |
if (this.serverName.equals(service.getDefaultServer())) return instanceId; | |
StringBuilder builder = new StringBuilder(instanceId.length() + DELIMITER.length() + this.serverName.length()); | |
builder.append(instanceId); | |
builder.append(DELIMITER); | |
builder.append(this.serverName); | |
return builder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment