Beispiel #1
0
		/// 
		public static DisconfCenterBaseModel getDisconfCenterFile(string fileName)
		{

			DisconfCenterFile disconfCenterFile = new DisconfCenterFile();

			fileName = fileName.Trim();

			//
			// file name
			disconfCenterFile.FileName = fileName;

			// 非注解式
			disconfCenterFile.IsTaggedWithNonAnnotationFile = true;

			// file type
			disconfCenterFile.SupportFileTypeEnum = SupportFileTypeEnum.getByFileName(fileName);

			//
			// disConfCommonModel
			DisConfCommonModel disConfCommonModel = makeDisConfCommonModel("", "");
			disconfCenterFile.DisConfCommonModel = disConfCommonModel;

			// Remote URL
			string url = DisconfWebPathMgr.getRemoteUrlParameter(DisClientSysConfig.Instance.CONF_SERVER_STORE_ACTION, disConfCommonModel.App, disConfCommonModel.Version, disConfCommonModel.Env, disconfCenterFile.FileName, DisConfigTypeEnum.FILE);
			disconfCenterFile.RemoteServerUrl = url;

			return disconfCenterFile;
		}
Beispiel #2
0
		/// <summary>
		/// 更新 一個配置文件, 下载、注入到仓库、Watch 三步骤
		/// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void updateOneConfFile(String fileName, com.baidu.disconf.client.common.model.DisconfCenterFile disconfCenterFile) throws Exception
		private void updateOneConfFile(string fileName, DisconfCenterFile disconfCenterFile)
		{

			if (disconfCenterFile == null)
			{
				throw new Exception("cannot find disconfCenterFile " + fileName);
			}

			string filePath = fileName;
			IDictionary<string, object> dataMap = new Dictionary<string, object>();

			//
			// 开启disconf才需要远程下载, 否则就本地就好
			//
			if (DisClientConfig.Instance.ENABLE_DISCONF)
			{

				//
				// 下载配置
				//
				try
				{

					string url = disconfCenterFile.RemoteServerUrl;
					filePath = fetcherMgr.downloadFileFromServer(url, fileName, disconfCenterFile.FileDir);

				}
				catch (Exception e)
				{

					//
					// 下载失败了, 尝试使用本地的配置
					//

					LOGGER.error(e.ToString(), e);
					LOGGER.warn("using local properties in class path: " + fileName);

					// change file path
					filePath = fileName;
				}
				LOGGER.debug("download ok.");
			}

			try
			{
				dataMap = FileTypeProcessorUtils.getKvMap(disconfCenterFile.SupportFileTypeEnum, disconfCenterFile.FilePath);
			}
			catch (Exception e)
			{
				LOGGER.error("cannot get kv data for " + filePath, e);
			}

			//
			// 注入到仓库中
			//
			disconfStoreProcessor.inject2Store(fileName, new DisconfValue(null, dataMap));
			LOGGER.debug("inject ok.");

			//
			// 开启disconf才需要进行watch
			//
			if (DisClientConfig.Instance.ENABLE_DISCONF)
			{
				//
				// Watch
				//
				DisConfCommonModel disConfCommonModel = disconfStoreProcessor.getCommonModel(fileName);
				if (watchMgr != null)
				{
					watchMgr.watchPath(this, disConfCommonModel, fileName, DisConfigTypeEnum.FILE, GsonUtils.toJson(disconfCenterFile.KV));
					LOGGER.debug("watch ok.");
				}
				else
				{
					LOGGER.warn("cannot monitor {} because watch mgr is null", fileName);
				}
			}
		}
Beispiel #3
0
		/// <summary>
		/// 转换配置文件
		/// </summary>
		private static DisconfCenterFile transformScanFile(Type disconfFileClass, ISet<Method> methods)
		{

			DisconfCenterFile disconfCenterFile = new DisconfCenterFile();

			//
			// class
			disconfCenterFile.Cls = disconfFileClass;

			DisconfFile disconfFileAnnotation = disconfFileClass.getAnnotation(typeof(DisconfFile));

			//
			// file name
			disconfCenterFile.FileName = disconfFileAnnotation.filename();

			// copy 2 target path
			disconfCenterFile.Copy2TargetDirPath = disconfFileAnnotation.copy2TargetDirPath().Trim();

			// file type
			disconfCenterFile.SupportFileTypeEnum = SupportFileTypeEnum.getByFileName(disconfFileAnnotation.filename());

			//
			// disConfCommonModel
			DisConfCommonModel disConfCommonModel = makeDisConfCommonModel(disconfFileAnnotation.env(), disconfFileAnnotation.version());
			disconfCenterFile.DisConfCommonModel = disConfCommonModel;

			// Remote URL
			string url = DisconfWebPathMgr.getRemoteUrlParameter(DisClientSysConfig.Instance.CONF_SERVER_STORE_ACTION, disConfCommonModel.App, disConfCommonModel.Version, disConfCommonModel.Env, disconfCenterFile.FileName, DisConfigTypeEnum.FILE);
			disconfCenterFile.RemoteServerUrl = url;

			// fields
			Field[] expectedFields = disconfFileClass.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);

			//
			// KEY & VALUE
			//
			IDictionary<string, DisconfCenterFile.FileItemValue> keyMaps = new Dictionary<string, DisconfCenterFile.FileItemValue>();

			foreach (Method method in methods)
			{

				// 获取指定的域
				Field field = MethodUtils.getFieldFromMethod(method, expectedFields, DisConfigTypeEnum.FILE);
				if (field == null)
				{
					continue;
				}

				//
				DisconfFileItem disconfFileItem = method.getAnnotation(typeof(DisconfFileItem));
				string keyName = disconfFileItem.name();

				// access
				field.Accessible = true;

				// get setter method
				Method setterMethod = MethodUtils.getSetterMethodFromField(disconfFileClass, field);

				// static 则直接获取其值
				if (Modifier.isStatic(field.Modifiers))
				{

					try
					{
						DisconfCenterFile.FileItemValue fileItemValue = new DisconfCenterFile.FileItemValue(field.get(null), field, setterMethod);
						keyMaps[keyName] = fileItemValue;

					}
					catch (Exception e)
					{
						LOGGER.error(e.ToString());
					}

				}
				else
				{

					// 非static则为Null, 这里我们没有必要获取其Bean的值
					DisconfCenterFile.FileItemValue fileItemValue = new DisconfCenterFile.FileItemValue(null, field, setterMethod);
					keyMaps[keyName] = fileItemValue;
				}
			}

			// 设置
			disconfCenterFile.KeyMaps = keyMaps;

			return disconfCenterFile;
		}
Beispiel #4
0
		/// <summary>
		/// 为某个配置文件进行注入实例中
		/// </summary>
		private void inject2OneConf(string fileName, DisconfCenterFile disconfCenterFile)
		{

			if (disconfCenterFile == null)
			{
				return;
			}

			try
			{

				//
				// 获取实例
				//

				object @object;
				try
				{

					@object = disconfCenterFile.Object;
					if (@object == null)
					{
						@object = registry.getFirstByType(disconfCenterFile.Cls, false, true);
					}

				}
				catch (Exception e)
				{

					LOGGER.error(e.ToString());
					@object = null;
				}

				// 注入实体中
				disconfStoreProcessor.inject2Instance(@object, fileName);

			}
			catch (Exception e)
			{
				LOGGER.warn(e.ToString(), e);
			}
		}