/// <summary> /// 更新一个配置 /// </summary> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private void updateOneConfItem(String keyName, com.baidu.disconf.client.common.model.DisconfCenterItem disconfCenterItem) throws Exception private void updateOneConfItem(string keyName, DisconfCenterItem disconfCenterItem) { if (disconfCenterItem == null) { throw new Exception("cannot find disconfCenterItem " + keyName); } string value = null; // // 开启disconf才需要远程下载, 否则就用默认值 // if (DisClientConfig.Instance.ENABLE_DISCONF) { // // 下载配置 // try { string url = disconfCenterItem.RemoteServerUrl; value = fetcherMgr.getValueFromServer(url); if (!string.ReferenceEquals(value, null)) { LOGGER.debug("value: " + value); } } catch (Exception e) { LOGGER.error("cannot use remote configuration: " + keyName, e); LOGGER.info("using local variable: " + keyName); } LOGGER.debug("download ok."); } // // 注入到仓库中 // disconfStoreProcessor.inject2Store(keyName, new DisconfValue(value, null)); LOGGER.debug("inject ok."); // // Watch // if (DisClientConfig.Instance.ENABLE_DISCONF) { if (watchMgr != null) { DisConfCommonModel disConfCommonModel = disconfStoreProcessor.getCommonModel(keyName); watchMgr.watchPath(this, disConfCommonModel, keyName, DisConfigTypeEnum.ITEM, value); LOGGER.debug("watch ok."); } else { LOGGER.warn("cannot monitor {} because watch mgr is null", keyName); } } }
/// <summary> /// 某个配置项:注入到实例中 /// </summary> private void inject2OneConf(string key, DisconfCenterItem disconfCenterItem) { if (disconfCenterItem == null) { return; } try { object @object = null; // // 静态 // if (!disconfCenterItem.Static) { @object = registry.getFirstByType(disconfCenterItem.DeclareClass, false, true); } disconfStoreProcessor.inject2Instance(@object, key); } catch (Exception e) { LOGGER.warn(e.ToString(), e); } }
/// <summary> /// 转换配置项 /// </summary> private static DisconfCenterItem transformScanItem(Method method) { DisconfCenterItem disconfCenterItem = new DisconfCenterItem(); // class Type cls = method.DeclaringClass; // fields Field[] expectedFields = cls.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); // field Field field = MethodUtils.getFieldFromMethod(method, expectedFields, DisConfigTypeEnum.ITEM); if (field == null) { return null; } // 获取标注 DisconfItem disconfItem = method.getAnnotation(typeof(DisconfItem)); // 去掉空格 string key = disconfItem.key().replace(" ", ""); // get setter method Method setterMethod = MethodUtils.getSetterMethodFromField(cls, field); disconfCenterItem.SetMethod = setterMethod; // field disconfCenterItem.Field = field; // key disconfCenterItem.Key = key; // access field.Accessible = true; // object disconfCenterItem.Object = null; // value if (Modifier.isStatic(field.Modifiers)) { try { disconfCenterItem.Value = field.get(null); } catch (Exception e) { LOGGER.error(e.ToString()); disconfCenterItem.Value = null; } } else { disconfCenterItem.Value = null; } // // disConfCommonModel DisConfCommonModel disConfCommonModel = makeDisConfCommonModel(disconfItem.env(), disconfItem.version()); disconfCenterItem.DisConfCommonModel = disConfCommonModel; // Disconf-web url string url = DisconfWebPathMgr.getRemoteUrlParameter(DisClientSysConfig.Instance.CONF_SERVER_STORE_ACTION, disConfCommonModel.App, disConfCommonModel.Version, disConfCommonModel.Env, key, DisConfigTypeEnum.ITEM); disconfCenterItem.RemoteServerUrl = url; return disconfCenterItem; }