@SuppressWarnings("unchecked") 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"; String implementingClass = dynamicProperties.getString(propertyName, null).get(); if (implementingClass != null) { try { Class<?> cls = Class.forName(implementingClass); // narrow the scope (cast) to the type we're expecting cls = cls.asSubclass(pluginClass); return (T) cls.newInstance(); } catch (ClassCastException e) { throw new RuntimeException(classSimpleName + " implementation is not an instance of " + classSimpleName + ": " + implementingClass); } catch (ClassNotFoundException e) { throw new RuntimeException(classSimpleName + " implementation class not found: " + implementingClass, e); } catch (InstantiationException e) { throw new RuntimeException(classSimpleName + " implementation not able to be instantiated: " + implementingClass, e); } catch (IllegalAccessException e) { throw new RuntimeException(classSimpleName + " implementation not able to be accessed: " + implementingClass, e); } } else { return null; } }
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(); } }
@Test public void testDynamicSystemProperties() throws Exception { //On the off chance this is the first test lets not screw up all the other tests HystrixPlugins.getInstance(); System.setProperty("hystrix.plugin.HystrixDynamicProperties.implementation", "com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties"); HystrixPlugins plugins = setupMockServiceLoader(); assertTrue(plugins.getDynamicProperties() instanceof HystrixDynamicPropertiesSystemProperties); HystrixDynamicProperties p = plugins.getDynamicProperties(); //Some minimum testing of system properties wrapper //this probably should be in its own test class. assertTrue(p.getBoolean("USE_DEFAULT", true).get()); assertEquals("string", p.getString("USE_DEFAULT", "string").get()); assertEquals(1L, p.getLong("USE_DEFAULT", 1L).get().longValue()); assertEquals(1, p.getInteger("USE_DEFAULT", 1).get().intValue()); assertNotNull(p.getString("path.separator", null).get()); assertEvents("[debug: [Created HystrixDynamicProperties instance from System property named \"hystrix.plugin.HystrixDynamicProperties.implementation\". Using class: {}, com.netflix.hystrix.strategy.properties.HystrixDynamicPropertiesSystemProperties]]"); System.clearProperty("hystrix.plugin.HystrixDynamicProperties.implementation"); }
@SuppressWarnings("unchecked") 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"; String implementingClass = dynamicProperties.getString(propertyName, null).get(); if (implementingClass != null) { try { Class<?> cls = Class.forName(implementingClass); // narrow the scope (cast) to the type we're expecting cls = cls.asSubclass(pluginClass); return (T) cls.newInstance(); } catch (ClassCastException e) { throw new RuntimeException(classSimpleName + " implementation is not an instance of " + classSimpleName + ": " + implementingClass); } catch (ClassNotFoundException e) { throw new RuntimeException(classSimpleName + " implementation class not found: " + implementingClass, e); } catch (InstantiationException e) { throw new RuntimeException(classSimpleName + " implementation not able to be instantiated: " + implementingClass, e); } catch (IllegalAccessException e) { throw new RuntimeException(classSimpleName + " implementation not able to be accessed: " + implementingClass, e); } } else { return null; } }
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(); } }