/*
 * DISCLAIMER
 *
 * Copyright 2017 ArangoDB GmbH, Cologne, Germany
 *
 * 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.
 *
 * Copyright holder is ArangoDB GmbH, Cologne, Germany
 */

package com.arangodb.internal.net;

import java.util.Collection;
import java.util.List;

import com.arangodb.internal.util.HostUtils;

/**
 * @author Mark Vollmary
 *
 */
public class ExtendedHostResolver implements HostResolver {

	private static final long MAX_CACHE_TIME = 60 * 60 * 1000;
	private EndpointResolver resolver;
	private final List<Host> hosts;
	private final Integer maxConnections;
	private final ConnectionFactory connectionFactory;
	private long lastUpdate;

	public ExtendedHostResolver(final List<Host> hosts, final Integer maxConnections,
		final ConnectionFactory connectionFactory) {
		super();
		this.hosts = hosts;
		this.maxConnections = maxConnections;
		this.connectionFactory = connectionFactory;
		lastUpdate = 0;
	}

	@Override
	public void init(final EndpointResolver resolver) {
		this.resolver = resolver;
	}

	@Override
	public List<Host> resolve(final boolean initial, final boolean closeConnections) {
		if (!initial && isExpired()) {
			lastUpdate = System.currentTimeMillis();
			final Collection<String> endpoints = resolver.resolve(closeConnections);
			if (!endpoints.isEmpty()) {
				hosts.clear();
			}
			for (final String endpoint : endpoints) {
				if (endpoint.matches(".*://.+:[0-9]+")) {
					final String[] s = endpoint.replaceAll(".*://", "").split(":");
					if (s.length == 2) {
						final HostDescription description = new HostDescription(s[0], Integer.valueOf(s[1]));
						hosts.add(HostUtils.createHost(description, maxConnections, connectionFactory));
					}
				}
			}
		}
		return hosts;
	}

	private boolean isExpired() {
		return System.currentTimeMillis() > lastUpdate + MAX_CACHE_TIME;
	}
}

@@ -716,24 +716,49 @@
 ernal.net;%0A%0A
+import java.util.Arrays;%0A
 import java.
@@ -794,24 +794,82 @@
 util.List;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import com.a
@@ -997,16 +997,109 @@
 solver %7B
+%0A%09%0A%09private static final Logger LOGGER = LoggerFactory.getLogger(ExtendedHostResolver.class);
 %0A%0A%09priva
@@ -1801,16 +1801,19 @@
 ions) %7B%0A
+%09%09%0A
 %09%09if (!i
@@ -1837,16 +1837,20 @@
 ed()) %7B%0A
+%09%09%09%0A
 %09%09%09lastU
@@ -1885,16 +1885,20 @@
 llis();%0A
+%09%09%09%0A
 %09%09%09final
@@ -1965,16 +1965,160 @@
 tions);%0A
+%09%09%09LOGGER.info(%22Resolve %22 + endpoints.size() + %22 Endpoints%22);%0A%09%09%09LOGGER.debug(%22Endpoints %22 + Arrays.deepToString(endpoints.toArray()));%0A%09%09%09%0A%09%09%09%0A
 %09%09%09if (!
@@ -2164,16 +2164,20 @@
 );%0A%09%09%09%7D%0A
+%09%09%09%0A
 %09%09%09for (
@@ -2209,24 +2209,79 @@
 ndpoints) %7B%0A
+%09%09%09%09LOGGER.debug(%22Create HOST from %22 + endpoint);%0A%09%09%09%09%0A
 %09%09%09%09if (endp
@@ -2310,24 +2310,30 @@
 %5B0-9%5D+%22)) %7B%0A
+%09%09%09%09%09%0A
 %09%09%09%09%09final S
@@ -2596,24 +2596,174 @@
 ry));%0A%09%09%09%09%09%7D
+ else %7B%0A%09%09%09%09%09%09LOGGER.warn(%22Skip Endpoint (Missung Port)%22 + endpoint);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09LOGGER.warn(%22Skip Endpoint (Format)%22 + endpoint);
 %0A%09%09%09%09%7D%0A%09%09%09%7D%0A
@@ -2766,16 +2766,19 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%09%0A
 %09%09return

add Debug Logging