/* * * Copyright 2013 Netflix, Inc. * * 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 com.netflix.client.config; import com.google.common.reflect.TypeToken; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; import static com.google.common.base.Preconditions.checkArgument; public abstract class CommonClientConfigKey implements IClientConfigKey { public static final String DEFAULT_NAME_SPACE = "ribbon"; public static final IClientConfigKey AppName = new CommonClientConfigKey("AppName"){}; public static final IClientConfigKey Version = new CommonClientConfigKey("Version"){}; public static final IClientConfigKey Port = new CommonClientConfigKey("Port", 7001){}; public static final IClientConfigKey SecurePort = new CommonClientConfigKey("SecurePort", 7001){}; public static final IClientConfigKey VipAddress = new CommonClientConfigKey("VipAddress"){}; public static final IClientConfigKey ForceClientPortConfiguration = new CommonClientConfigKey("ForceClientPortConfiguration"){}; // use client defined port regardless of server advert public static final IClientConfigKey DeploymentContextBasedVipAddresses = new CommonClientConfigKey("DeploymentContextBasedVipAddresses"){}; public static final IClientConfigKey MaxAutoRetries = new CommonClientConfigKey("MaxAutoRetries", 0){}; public static final IClientConfigKey MaxAutoRetriesNextServer = new CommonClientConfigKey("MaxAutoRetriesNextServer", 1){}; public static final IClientConfigKey OkToRetryOnAllOperations = new CommonClientConfigKey("OkToRetryOnAllOperations", false){}; public static final IClientConfigKey RequestSpecificRetryOn = new CommonClientConfigKey("RequestSpecificRetryOn"){}; public static final IClientConfigKey ReceiveBufferSize = new CommonClientConfigKey("ReceiveBufferSize"){}; public static final IClientConfigKey EnablePrimeConnections = new CommonClientConfigKey("EnablePrimeConnections", false){}; public static final IClientConfigKey PrimeConnectionsClassName = new CommonClientConfigKey("PrimeConnectionsClassName", "com.netflix.niws.client.http.HttpPrimeConnection"){}; public static final IClientConfigKey MaxRetriesPerServerPrimeConnection = new CommonClientConfigKey("MaxRetriesPerServerPrimeConnection", 9){}; public static final IClientConfigKey MaxTotalTimeToPrimeConnections = new CommonClientConfigKey("MaxTotalTimeToPrimeConnections", 30000){}; public static final IClientConfigKey MinPrimeConnectionsRatio = new CommonClientConfigKey("MinPrimeConnectionsRatio", 1.0f){}; public static final IClientConfigKey PrimeConnectionsURI = new CommonClientConfigKey("PrimeConnectionsURI", "/"){}; public static final IClientConfigKey PoolMaxThreads = new CommonClientConfigKey("PoolMaxThreads", 200){}; public static final IClientConfigKey PoolMinThreads = new CommonClientConfigKey("PoolMinThreads", 1){}; public static final IClientConfigKey PoolKeepAliveTime = new CommonClientConfigKey("PoolKeepAliveTime", 15 * 60){}; public static final IClientConfigKey PoolKeepAliveTimeUnits = new CommonClientConfigKey("PoolKeepAliveTimeUnits", TimeUnit.SECONDS.toString()){}; public static final IClientConfigKey EnableConnectionPool = new CommonClientConfigKey("EnableConnectionPool", true) {}; /** * Use {@link #MaxConnectionsPerHost} */ @Deprecated public static final IClientConfigKey MaxHttpConnectionsPerHost = new CommonClientConfigKey("MaxHttpConnectionsPerHost", 50){}; /** * Use {@link #MaxTotalConnections} */ @Deprecated public static final IClientConfigKey MaxTotalHttpConnections = new CommonClientConfigKey("MaxTotalHttpConnections", 200){}; public static final IClientConfigKey MaxConnectionsPerHost = new CommonClientConfigKey("MaxConnectionsPerHost", 50){}; public static final IClientConfigKey MaxTotalConnections = new CommonClientConfigKey("MaxTotalConnections", 200){}; public static final IClientConfigKey IsSecure = new CommonClientConfigKey("IsSecure"){}; public static final IClientConfigKey GZipPayload = new CommonClientConfigKey("GZipPayload"){}; public static final IClientConfigKey ConnectTimeout = new CommonClientConfigKey("ConnectTimeout", 2000){}; public static final IClientConfigKey BackoffInterval = new CommonClientConfigKey("BackoffTimeout", 0){}; public static final IClientConfigKey ReadTimeout = new CommonClientConfigKey("ReadTimeout", 5000){}; public static final IClientConfigKey SendBufferSize = new CommonClientConfigKey("SendBufferSize"){}; public static final IClientConfigKey StaleCheckingEnabled = new CommonClientConfigKey("StaleCheckingEnabled", false){}; public static final IClientConfigKey Linger = new CommonClientConfigKey("Linger", 0){}; public static final IClientConfigKey ConnectionManagerTimeout = new CommonClientConfigKey("ConnectionManagerTimeout", 2000){}; public static final IClientConfigKey FollowRedirects = new CommonClientConfigKey("FollowRedirects", false){}; public static final IClientConfigKey ConnectionPoolCleanerTaskEnabled = new CommonClientConfigKey("ConnectionPoolCleanerTaskEnabled", true){}; public static final IClientConfigKey ConnIdleEvictTimeMilliSeconds = new CommonClientConfigKey("ConnIdleEvictTimeMilliSeconds", 30*1000){}; public static final IClientConfigKey ConnectionCleanerRepeatInterval = new CommonClientConfigKey("ConnectionCleanerRepeatInterval", 30*1000){}; public static final IClientConfigKey EnableGZIPContentEncodingFilter = new CommonClientConfigKey("EnableGZIPContentEncodingFilter", false){}; public static final IClientConfigKey ProxyHost = new CommonClientConfigKey("ProxyHost"){}; public static final IClientConfigKey ProxyPort = new CommonClientConfigKey("ProxyPort"){}; public static final IClientConfigKey KeyStore = new CommonClientConfigKey("KeyStore"){}; public static final IClientConfigKey KeyStorePassword = new CommonClientConfigKey("KeyStorePassword"){}; public static final IClientConfigKey TrustStore = new CommonClientConfigKey("TrustStore"){}; public static final IClientConfigKey TrustStorePassword = new CommonClientConfigKey("TrustStorePassword"){}; // if this is a secure rest client, must we use client auth too? public static final IClientConfigKey IsClientAuthRequired = new CommonClientConfigKey("IsClientAuthRequired", false){}; public static final IClientConfigKey CustomSSLSocketFactoryClassName = new CommonClientConfigKey("CustomSSLSocketFactoryClassName"){}; // must host name match name in certificate? public static final IClientConfigKey IsHostnameValidationRequired = new CommonClientConfigKey("IsHostnameValidationRequired"){}; // see also http://hc.apache.org/httpcomponents-client-ga/tutorial/html/advanced.html public static final IClientConfigKey IgnoreUserTokenInConnectionPoolForSecureClient = new CommonClientConfigKey("IgnoreUserTokenInConnectionPoolForSecureClient"){}; // Client implementation public static final IClientConfigKey ClientClassName = new CommonClientConfigKey("ClientClassName", "com.netflix.niws.client.http.RestClient"){}; //LoadBalancer Related public static final IClientConfigKey InitializeNFLoadBalancer = new CommonClientConfigKey("InitializeNFLoadBalancer", true){}; public static final IClientConfigKey NFLoadBalancerClassName = new CommonClientConfigKey("NFLoadBalancerClassName", "com.netflix.loadbalancer.ZoneAwareLoadBalancer"){}; public static final IClientConfigKey NFLoadBalancerRuleClassName = new CommonClientConfigKey("NFLoadBalancerRuleClassName", "com.netflix.loadbalancer.AvailabilityFilteringRule"){}; public static final IClientConfigKey NFLoadBalancerPingClassName = new CommonClientConfigKey("NFLoadBalancerPingClassName", "com.netflix.loadbalancer.DummyPing"){}; public static final IClientConfigKey NFLoadBalancerPingInterval = new CommonClientConfigKey("NFLoadBalancerPingInterval"){}; public static final IClientConfigKey NFLoadBalancerMaxTotalPingTime = new CommonClientConfigKey("NFLoadBalancerMaxTotalPingTime"){}; public static final IClientConfigKey NFLoadBalancerStatsClassName = new CommonClientConfigKey("NFLoadBalancerStatsClassName", "com.netflix.loadbalancer.LoadBalancerStats"){}; public static final IClientConfigKey NIWSServerListClassName = new CommonClientConfigKey("NIWSServerListClassName", "com.netflix.loadbalancer.ConfigurationBasedServerList"){}; public static final IClientConfigKey ServerListUpdaterClassName = new CommonClientConfigKey("ServerListUpdaterClassName", "com.netflix.loadbalancer.PollingServerListUpdater"){}; public static final IClientConfigKey NIWSServerListFilterClassName = new CommonClientConfigKey("NIWSServerListFilterClassName"){}; public static final IClientConfigKey ServerListRefreshInterval = new CommonClientConfigKey("ServerListRefreshInterval"){}; public static final IClientConfigKey EnableMarkingServerDownOnReachingFailureLimit = new CommonClientConfigKey("EnableMarkingServerDownOnReachingFailureLimit"){}; public static final IClientConfigKey ServerDownFailureLimit = new CommonClientConfigKey("ServerDownFailureLimit"){}; public static final IClientConfigKey ServerDownStatWindowInMillis = new CommonClientConfigKey("ServerDownStatWindowInMillis"){}; public static final IClientConfigKey EnableZoneAffinity = new CommonClientConfigKey("EnableZoneAffinity", false){}; public static final IClientConfigKey EnableZoneExclusivity = new CommonClientConfigKey("EnableZoneExclusivity", false){}; public static final IClientConfigKey PrioritizeVipAddressBasedServers = new CommonClientConfigKey("PrioritizeVipAddressBasedServers", true){}; public static final IClientConfigKey VipAddressResolverClassName = new CommonClientConfigKey("VipAddressResolverClassName", "com.netflix.client.SimpleVipAddressResolver"){}; public static final IClientConfigKey TargetRegion = new CommonClientConfigKey("TargetRegion"){}; public static final IClientConfigKey RulePredicateClasses = new CommonClientConfigKey("RulePredicateClasses"){}; public static final IClientConfigKey RequestIdHeaderName = new CommonClientConfigKey("RequestIdHeaderName") {}; public static final IClientConfigKey UseIPAddrForServer = new CommonClientConfigKey("UseIPAddrForServer", false) {}; public static final IClientConfigKey ListOfServers = new CommonClientConfigKey("listOfServers", "") {}; private static final Set keys = new HashSet(); static { for (Field f: CommonClientConfigKey.class.getDeclaredFields()) { if (Modifier.isStatic(f.getModifiers()) //&& Modifier.isPublic(f.getModifiers()) && IClientConfigKey.class.isAssignableFrom(f.getType())) { try { keys.add((IClientConfigKey) f.get(null)); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } } } /** * @deprecated see {@link #keys()} */ @edu.umd.cs.findbugs.annotations.SuppressWarnings @Deprecated public static IClientConfigKey[] values() { return keys().toArray(new IClientConfigKey[0]); } /** * return all the public static keys defined in this class */ public static Set keys() { return keys; } public static IClientConfigKey valueOf(final String name) { for (IClientConfigKey key: keys()) { if (key.key().equals(name)) { return key; } } return new IClientConfigKey() { @Override public String key() { return name; } @Override public Class type() { return String.class; } }; } private final String configKey; private final Class type; private T defaultValue; @SuppressWarnings("unchecked") protected CommonClientConfigKey(String configKey) { this(configKey, null); } protected CommonClientConfigKey(String configKey, T defaultValue) { this.configKey = configKey; Type superclass = getClass().getGenericSuperclass(); checkArgument(superclass instanceof ParameterizedType, "%s isn't parameterized", superclass); Type runtimeType = ((ParameterizedType) superclass).getActualTypeArguments()[0]; type = (Class) TypeToken.of(runtimeType).getRawType(); this.defaultValue = defaultValue; } @Override public Class type() { return type; } /* (non-Javadoc) * @see com.netflix.niws.client.ClientConfig#key() */ @Override public String key() { return configKey; } @Override public String toString() { return configKey; } @Override public T defaultValue() { return defaultValue; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CommonClientConfigKey that = (CommonClientConfigKey) o; return Objects.equals(configKey, that.configKey); } @Override public int hashCode() { return Objects.hash(configKey); } }