AbstractConfig implements Serializable { protected static void appendProperties(AbstractConfig config) { if (config == null) { return; } String prefix = "dubbo." + getTagName(config.getClass()) + "."; Method[] methods = config.getClass().getMethods(); for (Method method : methods) { try { String name = method.getName(); if (name.length() > 3 && name.startsWith("set") && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 1 && isPrimitive(method.getParameterTypes()[0])) { String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "."); String value = null; if (config.getId() != null && config.getId().length() > 0) { String pn = prefix + config.getId() + "." + property; value = System.getProperty(pn); if (!StringUtils.isBlank(value)) { logger.info("Use System Property " + pn + " to config dubbo"); } } if (value == null || value.length() == 0) { String pn = prefix + property; value = System.getProperty(pn); if (!StringUtils.isBlank(value)) { logger.info("Use System Property " + pn + " to config dubbo"); } } if (value == null || value.length() == 0) { Method getter; try { getter = config.getClass().getMethod("get" + name.substring(3)); } catch (NoSuchMethodException e) { try { getter = config.getClass().getMethod("is" + name.substring(3)); } catch (NoSuchMethodException e2) { getter = null; } } if (getter != null) { if (getter.invoke(config) == null) { if (config.getId() != null && config.getId().length() > 0) { value = ConfigUtils.getProperty(prefix + config.getId() + "." + property); } if (value == null || value.length() == 0) { value = ConfigUtils.getProperty(prefix + property); } if (value == null || value.length() == 0) { String legacyKey = legacyProperties.get(prefix + property); if (legacyKey != null && legacyKey.length() > 0) { value = convertLegacyValue(legacyKey, ConfigUtils.getProperty(legacyKey)); } } } } } if (value != null && value.length() > 0) { method.invoke(config, convertPrimitive(method.getParameterTypes()[0], value)); } } } catch (Exception e) { logger.error(e.getMessage(), e); } } }  @Parameter(excluded = true) String getId(); void setId(String id); @Override String toString();  }
@Test public void testAppendProperties1() throws Exception { try { System.setProperty("dubbo.properties.i", "1"); System.setProperty("dubbo.properties.c", "c"); System.setProperty("dubbo.properties.b", "2"); System.setProperty("dubbo.properties.d", "3"); System.setProperty("dubbo.properties.f", "4"); System.setProperty("dubbo.properties.l", "5"); System.setProperty("dubbo.properties.s", "6"); System.setProperty("dubbo.properties.str", "dubbo"); System.setProperty("dubbo.properties.bool", "true"); PropertiesConfig config = new PropertiesConfig(); AbstractConfig.appendProperties(config); TestCase.assertEquals(1, config.getI()); TestCase.assertEquals('c', config.getC()); TestCase.assertEquals((byte) 0x02, config.getB()); TestCase.assertEquals(3d, config.getD()); TestCase.assertEquals(4f, config.getF()); TestCase.assertEquals(5L, config.getL()); TestCase.assertEquals(6, config.getS()); TestCase.assertEquals("dubbo", config.getStr()); TestCase.assertTrue(config.isBool()); } finally { System.clearProperty("dubbo.properties.i"); System.clearProperty("dubbo.properties.c"); System.clearProperty("dubbo.properties.b"); System.clearProperty("dubbo.properties.d"); System.clearProperty("dubbo.properties.f"); System.clearProperty("dubbo.properties.l"); System.clearProperty("dubbo.properties.s"); System.clearProperty("dubbo.properties.str"); System.clearProperty("dubbo.properties.bool"); } }
@Test public void testAppendProperties2() throws Exception { try { System.setProperty("dubbo.properties.two.i", "2"); PropertiesConfig config = new PropertiesConfig("two"); AbstractConfig.appendProperties(config); TestCase.assertEquals(2, config.getI()); } finally { System.clearProperty("dubbo.properties.two.i"); } }
@Test public void testAppendProperties3() throws Exception { try { Properties p = new Properties(); p.put("dubbo.properties.str", "dubbo"); ConfigUtils.setProperties(p); PropertiesConfig config = new PropertiesConfig(); AbstractConfig.appendProperties(config); TestCase.assertEquals("dubbo", config.getStr()); } finally { System.clearProperty(Constants.DUBBO_PROPERTIES_KEY); ConfigUtils.setProperties(null); } }