/// <summary> /// /// </summary> /// <param name="baseDirectory"></param> /// <param name="applicationName"></param> /// <param name="serviceName"></param> /// <param name="hostName"></param> /// <param name="defaultConfiguration">Json configuration sent by the client /// that represents base configuration for the application. It has lower priority. It can be null.</param> /// <param name="defaultParameters">Json configuration sent by the client /// to contain base parameters for the application, it has lower priority. It can be null.</param> /// <returns></returns> internal static ComposedConfiguration GetConfig ( String baseDirectory, String applicationName, String serviceName, String hostName, MissingParametersAction missingParams, JObject defaultConfiguration = null, JObject defaultParameters = null ) { DirectoryInfo baseDir = new DirectoryInfo(baseDirectory); var appDirectory = FileSystem.Instance.RedirectDirectory( Path.Combine(baseDir.FullName, applicationName) ); var systemDirectory = Path.Combine(baseDir.FullName, ".system"); if (!Directory.Exists(systemDirectory)) { Directory.CreateDirectory(systemDirectory); } var baseDirLen = Directory.GetParent(appDirectory).FullName.Length; String baseConfigFileName = Path.Combine(baseDir.FullName, "base.config"); String defaultApplicationBaseConfigFileName = Path.Combine(systemDirectory, applicationName, serviceName, (hostName ?? "") + DateTime.Now.ToString("_yyyyMMdd_hhMMss_fff_") + ".config"); String applicationBaseConfigFileName = Path.Combine(appDirectory, "base.config"); String defaultDirectoryBaseConfigFileName = Path.Combine(appDirectory, "Default", "base.config"); String serviceConfigFileName = Path.Combine(appDirectory, "Default", serviceName + ".config"); //load standard config file List <ConfigFileInfo> configFiles = new List <ConfigFileInfo>(); if (FileSystem.Instance.FileExists(baseConfigFileName)) { configFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(baseConfigFileName), baseConfigFileName.Substring(baseDirLen))); } //check default configuration file if (defaultConfiguration != null) { FileSystem.Instance.WriteFile(defaultApplicationBaseConfigFileName, defaultConfiguration.ToString()); configFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(defaultApplicationBaseConfigFileName), defaultApplicationBaseConfigFileName.Substring(baseDirLen))); _logger.InfoFormat("Default File used. App {0} service {1} host {2} content {3}", applicationName, serviceName, hostName, defaultConfiguration.ToString()); } if (FileSystem.Instance.FileExists(applicationBaseConfigFileName)) { configFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(applicationBaseConfigFileName), applicationBaseConfigFileName.Substring(baseDirLen))); } if (FileSystem.Instance.FileExists(defaultDirectoryBaseConfigFileName)) { configFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(defaultDirectoryBaseConfigFileName), defaultDirectoryBaseConfigFileName.Substring(baseDirLen))); } if (FileSystem.Instance.FileExists(serviceConfigFileName)) { configFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(serviceConfigFileName), serviceConfigFileName.Substring(baseDirLen))); } //load specific machine configuration files if (!String.IsNullOrEmpty(hostName)) { String hostBaseConfigFileName = Path.Combine(appDirectory, hostName, "base.config"); if (FileSystem.Instance.FileExists(hostBaseConfigFileName)) { configFiles.Add(ConfigFileInfo.ForHostSpecific(FileSystem.Instance.GetFileContent(hostBaseConfigFileName), hostBaseConfigFileName.Substring(baseDirLen), hostName)); } String hostConfigFileName = Path.Combine(appDirectory, hostName, serviceName + ".config"); if (FileSystem.Instance.FileExists(hostConfigFileName)) { configFiles.Add(ConfigFileInfo.ForHostSpecific(FileSystem.Instance.GetFileContent(hostConfigFileName), hostBaseConfigFileName.Substring(baseDirLen), hostName)); } } if (configFiles.Count == 0) { throw new ConfigurationErrorsException("There are no valid config at directory: " + baseDirectory); } var baseConfigObject = JsonComposer.ComposeJsonContent(configFiles.ToArray()); String defaultDirectoryBaseParametersFileName = Path.Combine(appDirectory, "Default", "parameters.config"); //then load all parameter files. var parameterObject = GetParameterObject( baseDirectory, applicationName, serviceName, hostName, defaultDirectoryBaseParametersFileName, defaultConfiguration, defaultParameters); //need to return all parameters. baseConfigObject["jarvis-parameters"] = (JObject)JsonConvert.DeserializeObject( JsonConvert.SerializeObject(parameterObject)); //Do the substitution ParameterManager.ReplaceResult replaceResult; ParameterManager parameterManager; if (missingParams == MissingParametersAction.Blank) { parameterManager = new ParameterManager(String.Empty); } else { parameterManager = new ParameterManager(); } while ((replaceResult = parameterManager.ReplaceParameters(baseConfigObject, parameterObject)).HasReplaced) { //do nothing, everything is done by the replace parameters routine } if (replaceResult.MissingParams.Count > 0) { var missingParametersList = replaceResult.MissingParams.Aggregate((s1, s2) => s1 + ", " + s2); switch (missingParams) { case MissingParametersAction.Throw: throw new ConfigurationErrorsException("Missing parameters: " + missingParametersList); case MissingParametersAction.Ignore: baseConfigObject["jarvis-missing-parameters"] = new JArray(replaceResult.MissingParams); _logger.DebugFormat("Missing Parameter List {0} but client ask to ignore", missingParametersList); break; case MissingParametersAction.Blank: baseConfigObject["jarvis-missing-parameters"] = new JArray(replaceResult.MissingParams); break; } } parameterManager.UnescapePercentage(baseConfigObject); //cleanup file if (FileSystem.Instance.FileExists(defaultApplicationBaseConfigFileName)) { FileSystem.Instance.DeleteFile(defaultApplicationBaseConfigFileName); } if (FileSystem.Instance.FileExists(defaultDirectoryBaseParametersFileName)) { FileSystem.Instance.DeleteFile(defaultDirectoryBaseParametersFileName); } return(new ComposedConfiguration(baseConfigObject, replaceResult)); }
public static JObject GetParameterObject ( String baseDirectory, String applicationName, String serviceName, String hostName, String defaultDirectoryBaseParametersFileName, JObject defaultConfiguration = null, JObject defaultParameters = null ) { DirectoryInfo baseDir = new DirectoryInfo(baseDirectory); var appDirectory = FileSystem.Instance.RedirectDirectory( Path.Combine(baseDir.FullName, applicationName) ); var systemDirectory = Path.Combine(baseDir.FullName, ".system"); if (!Directory.Exists(systemDirectory)) { Directory.CreateDirectory(systemDirectory); } var baseDirLen = Directory.GetParent(appDirectory).FullName.Length; //Base file parameters.config String baseParametersFileName = Path.Combine(baseDir.FullName, "parameters.config"); //base file for application, in base directory called parameters.applicationname.config String baseApplicationParametersFileName = Path.Combine(baseDir.FullName, "parameters." + applicationName + ".config"); //parameters.config inside the application directory String applicationBaseParametersFileName = Path.Combine(appDirectory, "parameters.config"); String defaultApplicationBaseParametersFileName = Path.Combine(systemDirectory, applicationName, serviceName, (hostName ?? "") + DateTime.Now.ToString("_yyyyMMdd_hhMMss_fff_") + ".parameters.config"); String serviceParametersFileName = Path.Combine(appDirectory, "Default", serviceName + ".parameters.config"); List <ConfigFileInfo> parametersFiles = new List <ConfigFileInfo>(); //Lower precedence, base parameter file name. if (FileSystem.Instance.FileExists(baseParametersFileName)) { parametersFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(baseParametersFileName), baseParametersFileName.Substring(baseDirLen))); } //check default parameters file passed by the client if (defaultParameters != null) { FileSystem.Instance.WriteFile(defaultApplicationBaseParametersFileName, defaultParameters.ToString()); parametersFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(defaultApplicationBaseParametersFileName), defaultApplicationBaseParametersFileName.Substring(baseDirLen))); _logger.InfoFormat("Default Parameters used. App {0} service {1} host {2} content {3}", applicationName, serviceName, hostName, defaultParameters.ToString()); } //PAY ATTENTION: base application parameters file name should have higher precedence than client //passed parameter file. if (FileSystem.Instance.FileExists(baseApplicationParametersFileName)) { parametersFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(baseApplicationParametersFileName), baseApplicationParametersFileName.Substring(baseDirLen))); } if (FileSystem.Instance.FileExists(applicationBaseParametersFileName)) { parametersFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(applicationBaseParametersFileName), applicationBaseParametersFileName.Substring(baseDirLen))); } if (FileSystem.Instance.FileExists(defaultDirectoryBaseParametersFileName)) { parametersFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(defaultDirectoryBaseParametersFileName), defaultDirectoryBaseParametersFileName.Substring(baseDirLen))); } if (FileSystem.Instance.FileExists(serviceParametersFileName)) { parametersFiles.Add(ConfigFileInfo.ForBase(FileSystem.Instance.GetFileContent(serviceParametersFileName), serviceParametersFileName.Substring(baseDirLen))); } JObject parameterObject; if (parametersFiles.Count > 0) { parameterObject = JsonComposer.ComposeJsonContent(parametersFiles.ToArray()); } else { parameterObject = new JObject(); } //use base parameters JObject sysParams = new JObject(); sysParams.Add("appName", applicationName); sysParams.Add("serviceName", serviceName); sysParams.Add("hostName", hostName); parameterObject.Add("sys", sysParams); return(parameterObject); }