Exemplo n.º 1
0
        public static uint EvaluateExpressionUint(ConfigDescription param)
        {
            if (param.DefaultValue.Contains('*'))
            {
                uint res     = 1;
                var  splited = param.DefaultValue.Split('*');
                foreach (var token in splited)
                {
                    if (HasHexPrefix(token))
                    {
                        res *= Convert.ToUInt32(token, 16);
                        continue;
                    }

                    res *= uint.Parse(token, CultureInfo.InvariantCulture);
                }
                return(res);
            }

            if (HasHexPrefix(param.DefaultValue))
            {
                return(Convert.ToUInt32(param.DefaultValue, 16));
            }

            return(uint.Parse(param.DefaultValue, CultureInfo.InvariantCulture));
        }
Exemplo n.º 2
0
        string ConvertSectionPropertyGroupType(ConfigDescription configDescription)
        {
            string sectionName = configDescription.SectionName;

            if (!PropertyGroupSectionTypes.ContainsKey(sectionName))
            {
                throw new ArgumentException(String.Format(
                                                StringResources.Error_FabricValidator_InvalidSectionPropertyGroupType, configDescription.Type, sectionName));
            }

            return(PropertyGroupSectionTypes[sectionName]);
        }
Exemplo n.º 3
0
        public static double EvaluateExpressionDouble(ConfigDescription param)
        {
            if (param.DefaultValue.Contains('*'))
            {
                double res     = 1;
                var    splited = param.DefaultValue.Split('*');
                foreach (var token in splited)
                {
                    res *= double.Parse(token, CultureInfo.InvariantCulture);
                }
                return(res);
            }

            return(double.Parse(param.DefaultValue, CultureInfo.InvariantCulture));
        }
Exemplo n.º 4
0
 public static string EvaluateExpressionString(ConfigDescription param)
 {
     if (param.DefaultValue.StartsWith("Common::SecureString(L\"") && param.DefaultValue.EndsWith("\")"))
     {
         // Common::SecureString is a native class and default config values are declared using it.
         // Managed code that parses default config values must strip off the native class name.
         //
         return(param.DefaultValue.Substring(23, param.DefaultValue.Length - 25));
     }
     else if (param.DefaultValue.StartsWith("L\"") && param.DefaultValue.EndsWith("\""))
     {
         if (param.DefaultValue.Length == 3)
         {
             return("");
         }
         else
         {
             return(param.DefaultValue.Substring(2, param.DefaultValue.Length - 3));
         }
     }
     return(param.DefaultValue);
 }
Exemplo n.º 5
0
        private static Dictionary <string, Dictionary <string, ConfigDescription> > ReadConfigurations()
        {
            Assembly   currentAssembly = typeof(WindowsFabricSettings).GetTypeInfo().Assembly;
            TextReader reader          = new StreamReader(currentAssembly.GetManifestResourceStream(FabricValidatorConstants.ConfigurationsFileName));
            var        configurations  = new Dictionary <string, Dictionary <string, ConfigDescription> >(StringComparer.OrdinalIgnoreCase);
            string     line;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.StartsWith("#", StringComparison.Ordinal))
                {
                    continue;
                }

                string[]          configurationInfo = line.Split(',');
                ConfigDescription configDesc        = new ConfigDescription()
                {
                    SectionName   = configurationInfo[0].Trim(),
                    SettingName   = configurationInfo[1].Trim(),
                    Type          = configurationInfo[2].Trim(),
                    DefaultValue  = configurationInfo[3].Trim(),
                    UpgradePolicy = (UpgradePolicy)Enum.Parse(typeof(UpgradePolicy), configurationInfo[4].Trim()),
                    SettingType   = configurationInfo[5].Trim()
                };

                try
                {
                    if (String.Equals(configDesc.Type, TimeSpanType, StringComparison.OrdinalIgnoreCase) ||
                        String.Equals(configDesc.Type, CommonTimeSpanType, StringComparison.OrdinalIgnoreCase))
                    {
                        configDesc.DefaultValue = EvaluateExpressionTimeSpan(configDesc).TotalSeconds.ToString(CultureInfo.InvariantCulture);
                    }
                    else if (String.Equals(configDesc.Type, IntType, StringComparison.OrdinalIgnoreCase))
                    {
                        configDesc.DefaultValue = EvaluateExpressionInt(configDesc).ToString(CultureInfo.InvariantCulture);
                    }
                    else if (String.Equals(configDesc.Type, UIntType, StringComparison.OrdinalIgnoreCase))
                    {
                        configDesc.DefaultValue = EvaluateExpressionUint(configDesc).ToString(CultureInfo.InvariantCulture);
                    }
                    else if (String.Equals(configDesc.Type, DoubleType, StringComparison.OrdinalIgnoreCase))
                    {
                        configDesc.DefaultValue = EvaluateExpressionDouble(configDesc).ToString(CultureInfo.InvariantCulture);
                    }
                    else if (String.Equals(configDesc.Type, BoolType, StringComparison.OrdinalIgnoreCase))
                    {
                        // no-op
                    }
                    else if (String.Equals(configDesc.Type, StringType, StringComparison.OrdinalIgnoreCase) || String.Equals(configDesc.Type, SecureStringType, StringComparison.OrdinalIgnoreCase))
                    {
                        configDesc.DefaultValue = EvaluateExpressionString(configDesc);
                    }
                }
                catch (Exception e)
                {
                    throw new ArgumentException(String.Format(StringResources.Warning_ThrowErrorWithParameterName2, configDesc.SectionName, configDesc.SettingName, e));
                }

                if (!configurations.ContainsKey(configDesc.SectionName))
                {
                    configurations.Add(configDesc.SectionName, new Dictionary <string, ConfigDescription>(StringComparer.OrdinalIgnoreCase));
                }

                if (configurations[configDesc.SectionName].ContainsKey(configDesc.SettingName))
                {
                    throw new ArgumentException(string.Format(StringResources.Error_FabricValidator_DuplicateParameterConfiguration,
                                                              configDesc.SettingName,
                                                              configDesc.SectionName));
                }

                configurations[configDesc.SectionName].Add(
                    configDesc.SettingName,
                    configDesc);
            }

            return(configurations);
        }
Exemplo n.º 6
0
        public static TimeSpan EvaluateExpressionTimeSpan(ConfigDescription param)
        {
            // The default value can be in the following formats:
            // 1. TimeSpan::Zero or TimeSpan::MinValue or TimeSpan::MaxValue
            // 2. TimeSpan::FromSeconds(x) or FromMinutes(x) or FromTicks(x) or FromMilliseconds(x)

            // 'x' can either be a decimal/integer or an expression consisting of a product of multiple decimals/integers

            // Case 1 - TimeSpan::Zero
            if (
                String.Equals(param.DefaultValue, FabricValidatorConstants.DefaultValues.TimeSpanZero,
                              StringComparison.OrdinalIgnoreCase) ||
                String.Equals(param.DefaultValue, FabricValidatorConstants.DefaultValues.CommonTimeSpanZero,
                              StringComparison.OrdinalIgnoreCase))
            {
                return(TimeSpan.Zero);
            }
            // Case 2 - TimeSpan::MinValue
            if (
                String.Equals(param.DefaultValue, FabricValidatorConstants.DefaultValues.TimeSpanMinValue,
                              StringComparison.OrdinalIgnoreCase) ||
                String.Equals(param.DefaultValue, FabricValidatorConstants.DefaultValues.CommonTimeSpanMinValue,
                              StringComparison.OrdinalIgnoreCase))
            {
                return(TimeSpan.MinValue);
            }
            // Case 3 - TimeSpan::MaxValue
            if (
                String.Equals(param.DefaultValue, FabricValidatorConstants.DefaultValues.TimeSpanMaxValue,
                              StringComparison.OrdinalIgnoreCase) ||
                String.Equals(param.DefaultValue, FabricValidatorConstants.DefaultValues.CommonTimeSpanMaxValue,
                              StringComparison.OrdinalIgnoreCase))
            {
                return(TimeSpan.MaxValue);
            }

            // Other cases involve parsing the double value from the format - TimeSpan::From*(x)
            var expression = param.DefaultValue.Substring(
                param.DefaultValue.IndexOf('(') + 1,
                param.DefaultValue.IndexOf(')') - param.DefaultValue.IndexOf('(') - 1);

            // If the timespan is in Ticks, the only possible value is long
            if (
                param.DefaultValue.StartsWith(FabricValidatorConstants.DefaultValues.CommonTimeSpanFromTicks,
                                              StringComparison.OrdinalIgnoreCase) ||
                param.DefaultValue.StartsWith(FabricValidatorConstants.DefaultValues.TimeSpanFromTicks,
                                              StringComparison.OrdinalIgnoreCase))
            {
                long result = 1;


                // We allow for the string to be a product of ints/decimals
                foreach (string token in expression.Split('*'))
                {
                    result *= long.Parse(token, CultureInfo.InvariantCulture);
                }

                // unit is ticks
                return(TimeSpan.FromTicks(result));
            }
            else
            {
                // If the timespan is in seconds,milliseconds or minutes, they can be represented as doubles
                double result = 1;

                // We allow for the string to be a product of ints/decimals
                foreach (string token in expression.Split('*'))
                {
                    result *= double.Parse(token, CultureInfo.InvariantCulture);
                }

                if (
                    param.DefaultValue.StartsWith(
                        FabricValidatorConstants.DefaultValues.CommonTimeSpanFromSeconds,
                        StringComparison.OrdinalIgnoreCase) ||
                    param.DefaultValue.StartsWith(FabricValidatorConstants.DefaultValues.TimeSpanFromSeconds,
                                                  StringComparison.OrdinalIgnoreCase))
                {
                    // unit is seconds
                    return(TimeSpan.FromSeconds(result));
                }
                else if (
                    param.DefaultValue.StartsWith(
                        FabricValidatorConstants.DefaultValues.CommonTimeSpanFromMinutes,
                        StringComparison.OrdinalIgnoreCase) ||
                    param.DefaultValue.StartsWith(
                        FabricValidatorConstants.DefaultValues.TimeSpanFromMinutes,
                        StringComparison.OrdinalIgnoreCase))
                {
                    // unit is minutes
                    return(TimeSpan.FromMinutes(result));
                }
                else if (
                    param.DefaultValue.StartsWith(
                        FabricValidatorConstants.DefaultValues.CommonTimeSpanFromHours,
                        StringComparison.OrdinalIgnoreCase) ||
                    param.DefaultValue.StartsWith(
                        FabricValidatorConstants.DefaultValues.TimeSpanFromHours,
                        StringComparison.OrdinalIgnoreCase))
                {
                    // unit is hours
                    return(TimeSpan.FromHours(result));
                }
                else if (
                    param.DefaultValue.StartsWith(
                        FabricValidatorConstants.DefaultValues.CommonTimeSpanFromMilliseconds,
                        StringComparison.OrdinalIgnoreCase) ||
                    param.DefaultValue.StartsWith(
                        FabricValidatorConstants.DefaultValues.TimeSpanFromMilliseconds,
                        StringComparison.OrdinalIgnoreCase))
                {
                    // unit is milliseconds
                    return(TimeSpan.FromMilliseconds(result));
                }
                else
                {
                    throw new ArgumentException(string.Format(StringResources.Error_FabricValidator_InvalidValue, param.SectionName, param.SettingName, param.Type, param.DefaultValue));
                }
            }
        }