/*
 * Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.cloud.netflix.ribbon.eureka;

import java.util.List;

import org.springframework.util.StringUtils;

import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;

/**
 * @author Dave Syer
 *
 */
public class DomainExtractingServerList implements ServerList<Server> {

	private ServerList<Server> list;

	public DomainExtractingServerList(ServerList<Server> list) {
		this.list = list;
	}

	@Override
	public List<Server> getInitialListOfServers() {
		List<Server> servers = setZones(list.getInitialListOfServers());
		return servers;
	}

	@Override
	public List<Server> getUpdatedListOfServers() {
		List<Server> servers = setZones(list.getUpdatedListOfServers());
		return servers;
	}

	private List<Server> setZones(List<Server> servers) {
		for (Server server : servers) {
			String zone = extractApproximateZone(server);
			server.setZone(zone);
		}
		return servers;
	}

	private String extractApproximateZone(Server server) {
		String host = server.getHost();
		if (!host.contains(".")) {
			return host;
		}
		String[] split = StringUtils.split(host, ".");
		StringBuilder builder = new StringBuilder(split[1]);
		for (int i = 2; i < split.length; i++) {
			builder.append(".").append(split[i]);
		}
		return builder.toString();
	}

}

@@ -689,20 +689,93 @@
 va.util.
-List
+ArrayList;%0Aimport java.util.List;%0A%0Aimport lombok.Getter;%0Aimport lombok.Setter
 ;%0A%0Aimpor
@@ -815,16 +815,57 @@
 Utils;%0A%0A
+import com.netflix.appinfo.InstanceInfo;%0A
 import c
@@ -939,16 +939,77 @@
 verList;
+%0Aimport com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
 %0A%0A/**%0A *
@@ -1587,118 +1587,955 @@
 %7B%0A%09%09
-for (Server server : servers) %7B%0A%09%09%09String zone = extractApproximateZone(server);%0A%09%09%09server.setZone(zone
+List%3CServer%3E result = new ArrayList%3CServer%3E();%0A%09%09for (Server server : servers) %7B%0A%09%09%09if (server instanceof DiscoveryEnabledServer) %7B%0A%09%09%09%09result.add(new DomainExtractingServer((DiscoveryEnabledServer) server));%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09result.add(server);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return result;%0A%09%7D%0A%0A%7D%0A%0Aclass DomainExtractingServer extends DiscoveryEnabledServer %7B%0A%0A%09@Getter%0A%09@Setter%0A%09private String id;%0A%0A%09public DomainExtractingServer(DiscoveryEnabledServer server) %7B%0A%09%09super(server.getInstanceInfo(), true, false);%0A%09%09String zone = extractApproximateZone(server);%0A%09%09setZone(zone);%0A%09%09String id = extractId(server);%0A%09%09setId(id);%0A%09%7D%0A%0A%09private String extractId(Server server) %7B%0A%09%09if (server instanceof DiscoveryEnabledServer) %7B%0A%09%09%09DiscoveryEnabledServer enabled = (DiscoveryEnabledServer) server;%0A%09%09%09InstanceInfo instance = enabled.getInstanceInfo();%0A%09%09%09if (instance.getMetadata().containsKey(%22instanceId%22)) %7B%0A%09%09%09%09return instance.getMetadata().get(%22instanceId%22
 );%0A
+%09
 %09%09%7D%0A
+%09%09%7D%0A
 %09%09re
@@ -2537,33 +2537,40 @@
 %0A%09%09return server
-s
+.getId()
 ;%0A%09%7D%0A%0A%09private S

Use instanceId for server id if available