Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package org.springframework.cloud.client;


import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* Default implementation of {@link ServiceInstance}.
*
* @author Spencer Gibb
* @author Steven van Beelen
*/
public class DefaultServiceInstance implements ServiceInstance {

Expand All @@ -48,24 +49,61 @@ public DefaultServiceInstance(String serviceId, String host, int port, boolean s
this.metadata = metadata;
}

public DefaultServiceInstance(String serviceId, String host, int port,
boolean secure) {
this(serviceId, host, port, secure, new LinkedHashMap<String, String>());
public DefaultServiceInstance(String serviceId, String host, int port, boolean secure) {
this(serviceId, host, port, secure, new LinkedHashMap<>());
}

@Override
public URI getUri() {
return getUri(this);
}
@Override
public String getServiceId() {
return serviceId;
}

@Override
public String getHost() {
return host;
}

@Override
public int getPort() {
return port;
}

@Override
public boolean isSecure() {
return secure;
}

@Override
public Map<String, String> getMetadata() {
return this.metadata;
}

@Override
public void putMetadata(String key, String value) {
this.metadata.put(key, value);
}

@Override
public void setMetadata(Map<String, String> metadata) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the approach of removing all the old fields and adding them because metadata is final for this class.

Removing final from the field seemed more problematic because of the @RequiredArgsConstructor.

Set<String> keySet = this.metadata.keySet();
for (String key : keySet) {
this.metadata.remove(key);
}

for (Map.Entry<String, String> newEntry : metadata.entrySet()) {
this.metadata.put(newEntry.getKey(), newEntry.getValue());
}
}

@Override
public URI getUri() {
return getUri(this);
}

/**
* Create a uri from the given ServiceInstance's host:port
* @param instance
*
* @param instance a {@link ServiceInstance} to create an {@link java.net.URI} out of.
* @return URI of the form (secure)?https:http + "host:port"
*/
public static URI getUri(ServiceInstance instance) {
Expand All @@ -75,26 +113,6 @@ public static URI getUri(ServiceInstance instance) {
return URI.create(uri);
}

@Override
public String getServiceId() {
return serviceId;
}

@Override
public String getHost() {
return host;
}

@Override
public int getPort() {
return port;
}

@Override
public boolean isSecure() {
return secure;
}

@Override
public String toString() {
return "DefaultServiceInstance{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

/**
* Represents an instance of a Service in a Discovery System
*
* @author Spencer Gibb
* @author Steven van Beelen
*/
public interface ServiceInstance {

Expand Down Expand Up @@ -54,4 +56,22 @@ public interface ServiceInstance {
* @return the key value pair metadata associated with the service instance
*/
Map<String, String> getMetadata();

/**
* Add a key/value pair to the metadata map.
*
* @param key a metadata key as a String
* @param value a corresponding metadata value as a String
*/
default void putMetadata(String key, String value) {
}

/**
* Replace the current metadata map for a new one.
*
* @param metadata a metadata map to replace the current metadata
*/
default void setMetadata(Map<String, String> metadata) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,16 @@ public URI getUri() {
public Map<String, String> getMetadata() {
return this.metadata;
}

@Override
public void putMetadata(String key, String value) {
this.metadata.put(key, value);
}

@Override
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

}
}