Skip to content

Commit

Permalink
Add system property to pick HystrixDynamicProperties and made Archaiu…
Browse files Browse the repository at this point in the history
…s Helper more private.
  • Loading branch information
agentgt committed Feb 9, 2016
1 parent ab456d9 commit 664430f
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 181 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Copyright 2016 Netflix, Inc.
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.hystrix.strategy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.netflix.hystrix.strategy.properties.HystrixDynamicProperties;

/**
* @ExcludeFromJavadoc
* @author agentgt
*/
class HystrixArchaiusHelper {

/**
* To keep class loading minimal for those that have archaius in the classpath but choose not to use it.
* @ExcludeFromJavadoc
* @author agent
*/
private static class LazyHolder {
private final static Method loadCascadedPropertiesFromResources;
private final static String CONFIG_MANAGER_CLASS = "com.netflix.config.ConfigurationManager";

static {
Method load = null;
try {
Class<?> configManager = Class.forName(CONFIG_MANAGER_CLASS);
load = configManager.getMethod("loadCascadedPropertiesFromResources", String.class);
} catch (Exception e) {
}

loadCascadedPropertiesFromResources = load;
}
}

/**
* @ExcludeFromJavadoc
*/
static boolean isArchaiusV1Available() {
return LazyHolder.loadCascadedPropertiesFromResources != null;
}

static void loadCascadedPropertiesFromResources(String name) {
if (isArchaiusV1Available()) {
try {
LazyHolder.loadCascadedPropertiesFromResources.invoke(null, name);
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
}
}

/**
* @ExcludeFromJavadoc
*/
static HystrixDynamicProperties createArchaiusDynamicProperties() {
if (isArchaiusV1Available()) {
loadCascadedPropertiesFromResources("hystrix-plugins");
try {
Class<?> defaultProperties = Class.forName(
"com.netflix.hystrix.strategy.properties.archaius" + ".HystrixDynamicPropertiesArchaius");
return (HystrixDynamicProperties) defaultProperties.newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
// Fallback to System properties.
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisherDefault;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisherFactory;
import com.netflix.hystrix.strategy.properties.HystrixArchaiusHelper;
import com.netflix.hystrix.strategy.properties.HystrixDynamicProperties;
import com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategyDefault;

Expand All @@ -42,8 +42,8 @@
* <li>default implementation</li>
* </ol>
*
* The exception to the above order is the {@link HystrixDynamicProperties} implmentation
* which is only loaded through the ServiceLoader (see the {@link HystrixPlugins#getDynamicProperties() getter} for more details).
* The exception to the above order is the {@link HystrixDynamicProperties} implementation
* which is only loaded through <code>System.properties</code> or the ServiceLoader (see the {@link HystrixPlugins#getDynamicProperties() getter} for more details).
* <p>
* See the Hystrix GitHub Wiki for more information: <a href="https://github.com/Netflix/Hystrix/wiki/Plugins">https://github.com/Netflix/Hystrix/wiki/Plugins</a>.
*/
Expand Down Expand Up @@ -245,9 +245,10 @@ public HystrixPropertiesStrategy getPropertiesStrategy() {
* <p>
* The order of precedence for loading implementations is:
* <ol>
* <li>System property of key: <code>hystrix.plugin.HystrixDynamicProperties.implementation</code> with the class as a value.</li>
* <li>The {@link ServiceLoader}.</li>
* <li>An implementation based on Archaius if it is found in the classpath it is used.</li>
* <li>An implementation based on the {@link System#getProperties()}</li>
* <li>An implementation based on Archaius if it is found in the classpath is used.</li>
* <li>A fallback implementation based on the {@link System#getProperties()}</li>
* </ol>
* @return never <code>null</code>
*/
Expand Down Expand Up @@ -314,13 +315,13 @@ public void registerCommandExecutionHook(HystrixCommandExecutionHook impl) {


private <T> T getPluginImplementation(Class<T> pluginClass) {
T p = getPluginImplementationViaProperties(pluginClass);
T p = getPluginImplementationViaProperties(pluginClass, dynamicProperties);
if (p != null) return p;
return findService(pluginClass, classLoader);
}

@SuppressWarnings("unchecked")
private <T> T getPluginImplementationViaProperties(Class<T> pluginClass) {
private static <T> T getPluginImplementationViaProperties(Class<T> pluginClass, HystrixDynamicProperties dynamicProperties) {
String classSimpleName = pluginClass.getSimpleName();
// Check Archaius for plugin class.
String propertyName = "hystrix.plugin." + classSimpleName + ".implementation";
Expand Down Expand Up @@ -348,9 +349,14 @@ private <T> T getPluginImplementationViaProperties(Class<T> pluginClass) {


private static HystrixDynamicProperties resolveDynamicProperties(ClassLoader classLoader) {
HystrixDynamicProperties hp = findService(HystrixDynamicProperties.class, classLoader);
HystrixDynamicProperties hp = getPluginImplementationViaProperties(HystrixDynamicProperties.class,
HystrixDynamicPropertiesSystemProperties.getInstance());
if (hp != null) return hp;
return HystrixArchaiusHelper.createArchaiusDynamicProperties();
hp = findService(HystrixDynamicProperties.class, classLoader);
if (hp != null) return hp;
hp = HystrixArchaiusHelper.createArchaiusDynamicProperties();
if (hp != null) return hp;
return HystrixDynamicPropertiesSystemProperties.getInstance();
}

private static <T> T findService(
Expand Down

This file was deleted.

Loading

0 comments on commit 664430f

Please sign in to comment.