forked from Netflix/Hystrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Archaius a soft dependency through reflection and improve plugin…
… loading. See Netflix#970 Netflix#129 Netflix#252
- Loading branch information
Showing
6 changed files
with
360 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...rix-core/src/main/java/com/netflix/hystrix/strategy/properties/HystrixArchaiusHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.netflix.hystrix.strategy.properties; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
|
||
public class HystrixArchaiusHelper { | ||
|
||
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; | ||
} | ||
|
||
public static boolean isArchaiusV1Available() { | ||
return loadCascadedPropertiesFromResources != null; | ||
} | ||
|
||
static void loadCascadedPropertiesFromResources(String name) { | ||
if (isArchaiusV1Available()) { | ||
try { | ||
loadCascadedPropertiesFromResources.invoke(null, name); | ||
} catch (IllegalAccessException e) { | ||
} catch (IllegalArgumentException e) { | ||
} catch (InvocationTargetException e) { | ||
} | ||
} | ||
} | ||
|
||
public 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) { | ||
// TODO Auto-generated catch block | ||
throw new RuntimeException(e); | ||
} catch (InstantiationException e) { | ||
// TODO Auto-generated catch block | ||
throw new RuntimeException(e); | ||
} catch (IllegalAccessException e) { | ||
// TODO Auto-generated catch block | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
//TODO Using system properties but we could just fail. | ||
return new SystemPropertiesHystrixDynamicProperties(); | ||
} | ||
|
||
private static class SystemPropertiesHystrixDynamicProperties implements HystrixDynamicProperties { | ||
|
||
@Override | ||
public HystrixDynamicProperty<Integer> getInteger(final String name, final Integer fallback) { | ||
return new HystrixDynamicProperty<Integer>() { | ||
|
||
@Override | ||
public Integer get() { | ||
return Integer.getInteger(name, fallback); | ||
} | ||
@Override | ||
public void addCallback(Runnable callback) { | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public HystrixDynamicProperty<String> getString(final String name, final String fallback) { | ||
return new HystrixDynamicProperty<String>() { | ||
|
||
@Override | ||
public String get() { | ||
return System.getProperty(name, fallback); | ||
} | ||
|
||
@Override | ||
public void addCallback(Runnable callback) { | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public HystrixDynamicProperty<Long> getLong(final String name, final Long fallback) { | ||
return new HystrixDynamicProperty<Long>() { | ||
|
||
@Override | ||
public Long get() { | ||
return Long.getLong(name, fallback); | ||
} | ||
|
||
@Override | ||
public void addCallback(Runnable callback) { | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public HystrixDynamicProperty<Boolean> getBoolean(final String name, final Boolean fallback) { | ||
return new HystrixDynamicProperty<Boolean>() { | ||
|
||
@Override | ||
public Boolean get() { | ||
return Boolean.getBoolean(name); | ||
} | ||
|
||
@Override | ||
public void addCallback(Runnable callback) { | ||
} | ||
}; | ||
} | ||
|
||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...-core/src/main/java/com/netflix/hystrix/strategy/properties/HystrixDynamicProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.netflix.hystrix.strategy.properties; | ||
|
||
public interface HystrixDynamicProperties { | ||
|
||
public HystrixDynamicProperty<String> getString(String name, String fallback); | ||
public HystrixDynamicProperty<Integer> getInteger(String name, Integer fallback); | ||
public HystrixDynamicProperty<Long> getLong(String name, Long fallback); | ||
public HystrixDynamicProperty<Boolean> getBoolean(String name, Boolean fallback); | ||
|
||
public static class Util { | ||
@SuppressWarnings("unchecked") | ||
public static <T> HystrixDynamicProperty<T> getProperty( | ||
HystrixDynamicProperties delegate, String name, T fallback, Class<T> type) { | ||
return (HystrixDynamicProperty<T>) doProperty(delegate, name, fallback, type); | ||
} | ||
|
||
private static HystrixDynamicProperty<?> doProperty( | ||
HystrixDynamicProperties delegate, | ||
String name, Object fallback, Class<?> type) { | ||
if(type == String.class) { | ||
return delegate.getString(name, (String) fallback); | ||
} | ||
else if (type == Integer.class) { | ||
return delegate.getInteger(name, (Integer) fallback); | ||
} | ||
else if (type == Long.class) { | ||
return delegate.getLong(name, (Long) fallback); | ||
} | ||
else if (type == Boolean.class) { | ||
return delegate.getBoolean(name, (Boolean) fallback); | ||
} | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...ix-core/src/main/java/com/netflix/hystrix/strategy/properties/HystrixDynamicProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.netflix.hystrix.strategy.properties; | ||
|
||
public interface HystrixDynamicProperty<T> extends HystrixProperty<T>{ | ||
|
||
public void addCallback(Runnable callback); | ||
|
||
} |
Oops, something went wrong.