/// <summary>
        /// Populate the global configuration object with
        /// information from the configuration xml
        /// </summary>
        /// <param name="applicationPrivateData">private data
        /// associated with the endpoint</param>
        /// <param name="configProviderId"></param>
        public void Populate(string applicationPrivateData, string configProviderId)
        {
            if (_isPopulated)
            {
                return;
            }

            lock (_syncObject)
            {
                if (!_isPopulated)
                {
                    _privateData = applicationPrivateData;

                    string[] tokens = configProviderId.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    _configProviderId = tokens.Length > 0 ? tokens[tokens.Length - 1] : configProviderId;
                    _isPopulated      = true;
                    _wfOptions        = LoadConfig(_privateData, this);
                    ResetCaching();
                }
                else
                {
                    Debug.Assert(false, "Populate should only be called once");
                }
            }
        }
        internal static PSWorkflowExecutionOption LoadConfig(string privateData, PSWorkflowConfigurationProvider configuration)
        {
            PSWorkflowExecutionOption pSWorkflowExecutionOption = new PSWorkflowExecutionOption();

            if (!string.IsNullOrEmpty(privateData))
            {
                XmlReaderSettings xmlReaderSetting = new XmlReaderSettings();
                xmlReaderSetting.CheckCharacters = false;
                xmlReaderSetting.IgnoreComments  = true;
                xmlReaderSetting.IgnoreProcessingInstructions = true;
                xmlReaderSetting.MaxCharactersInDocument      = (long)0x2710;
                xmlReaderSetting.XmlResolver      = null;
                xmlReaderSetting.ConformanceLevel = ConformanceLevel.Fragment;
                XmlReaderSettings xmlReaderSetting1 = xmlReaderSetting;
                XmlReader         xmlReader         = XmlReader.Create(new StringReader(privateData), xmlReaderSetting1);
                using (xmlReader)
                {
                    if (xmlReader.ReadToFollowing("PrivateData"))
                    {
                        HashSet <string> strs       = new HashSet <string>();
                        bool             descendant = xmlReader.ReadToDescendant("Param");
                        while (descendant)
                        {
                            if (xmlReader.MoveToAttribute("Name"))
                            {
                                string value = xmlReader.Value;
                                if (xmlReader.MoveToAttribute("Value"))
                                {
                                    if (!strs.Contains(value.ToLower(CultureInfo.InvariantCulture)))
                                    {
                                        string str = xmlReader.Value;
                                        PSWorkflowConfigurationProvider.Update(value, str, pSWorkflowExecutionOption, configuration);
                                        strs.Add(value.ToLower(CultureInfo.InvariantCulture));
                                        descendant = xmlReader.ReadToFollowing("Param");
                                    }
                                    else
                                    {
                                        throw new PSArgumentException(Resources.ParamSpecifiedMoreThanOnce, value);
                                    }
                                }
                                else
                                {
                                    throw new PSArgumentException(Resources.ValueNotSpecifiedForParam);
                                }
                            }
                            else
                            {
                                throw new PSArgumentException(Resources.NameNotSpecifiedForParam);
                            }
                        }
                    }
                }
                return(pSWorkflowExecutionOption);
            }
            else
            {
                return(pSWorkflowExecutionOption);
            }
        }
 public PSWorkflowConfigurationProvider()
 {
     this._syncObject               = new object();
     this._maxInProcRunspaces       = PSWorkflowConfigurationProvider.DefaultMaxInProcRunspaces;
     this.outOfProcessActivityCache = new ConcurrentDictionary <Type, bool>();
     this._wfOptions = new PSWorkflowExecutionOption();
     this.LoadFromDefaults();
 }
        internal static PSWorkflowExecutionOption LoadConfig(string privateData, PSWorkflowConfigurationProvider configuration)
        {
            PSWorkflowExecutionOption target = new PSWorkflowExecutionOption();

            if (String.IsNullOrEmpty(privateData))
            {
                return(target);
            }

            XmlReaderSettings readerSettings = new XmlReaderSettings
            {
                CheckCharacters = false,
                IgnoreComments  = true,
                IgnoreProcessingInstructions = true,
                MaxCharactersInDocument      = 10000,
                XmlResolver      = null,
                ConformanceLevel = ConformanceLevel.Fragment
            };

            using (XmlReader reader = XmlReader.Create(new StringReader(privateData), readerSettings))
            {
                // read the header <PrivateData>
                if (reader.ReadToFollowing(PrivateDataToken))
                {
                    HashSet <string> assignedParams = new HashSet <string>();
                    bool             found          = reader.ReadToDescendant(ParamToken);
                    while (found)
                    {
                        if (!reader.MoveToAttribute(NameToken))
                        {
                            throw new PSArgumentException(Resources.NameNotSpecifiedForParam);
                        }

                        string optionName = reader.Value;

                        if (!reader.MoveToAttribute(ValueToken))
                        {
                            throw new PSArgumentException(Resources.ValueNotSpecifiedForParam);
                        }

                        if (assignedParams.Contains(optionName.ToLower(CultureInfo.InvariantCulture)))
                        {
                            throw new PSArgumentException(Resources.ParamSpecifiedMoreThanOnce, optionName);
                        }

                        string optionValue = reader.Value;
                        Update(optionName, optionValue, target, configuration);

                        assignedParams.Add(optionName.ToLower(CultureInfo.InvariantCulture));
                        found = reader.ReadToFollowing(ParamToken);
                    }
                }
            }
            return(target);
        }
        public void Populate(string applicationPrivateData, string configProviderId)
        {
            string str;

            if (!this._isPopulated)
            {
                lock (this._syncObject)
                {
                    if (!this._isPopulated)
                    {
                        this._privateData = applicationPrivateData;
                        char[] chrArray = new char[1];
                        chrArray[0] = '/';
                        string[] strArrays = configProviderId.Split(chrArray, StringSplitOptions.RemoveEmptyEntries);
                        PSWorkflowConfigurationProvider pSWorkflowConfigurationProvider = this;
                        if ((int)strArrays.Length > 0)
                        {
                            str = strArrays[(int)strArrays.Length - 1];
                        }
                        else
                        {
                            str = configProviderId;
                        }
                        pSWorkflowConfigurationProvider._configProviderId = str;
                        this._isPopulated = true;
                        this._wfOptions   = PSWorkflowConfigurationProvider.LoadConfig(this._privateData, this);
                        this.ResetCaching();
                    }
                }
                return;
            }
            else
            {
                return;
            }
        }
        private static void Update(string optionName, string value, PSWorkflowExecutionOption target, PSWorkflowConfigurationProvider configuration)
        {
            bool   flag  = false;
            bool   flag1 = false;
            string lower = optionName.ToLower(CultureInfo.InvariantCulture);
            string str   = lower;

            if (lower != null)
            {
                if (str == "workflowapplicationpersistunloadtimeoutsec")
                {
                    target.WorkflowApplicationPersistUnloadTimeoutSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "activityprocessidletimeoutsec")
                {
                    target.ActivityProcessIdleTimeoutSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "allowedactivity")
                {
                    char[] chrArray = new char[1];
                    chrArray[0] = ',';
                    string[] strArrays = value.Split(chrArray, StringSplitOptions.RemoveEmptyEntries);
                    PSWorkflowExecutionOption array = target;
                    string[] strArrays1             = strArrays;
                    array.AllowedActivity = strArrays1.Select <string, string>((string activity) => activity.Trim()).ToArray <string>();
                    return;
                }
                else if (str == "enablevalidation")
                {
                    if (!bool.TryParse(value, out flag))
                    {
                        return;
                    }
                    target.EnableValidation = flag;
                    return;
                }
                else if (str == "persistencepath")
                {
                    target.PersistencePath = Environment.ExpandEnvironmentVariables(value);
                    return;
                }
                else if (str == "maxpersistencestoresizegb")
                {
                    target.MaxPersistenceStoreSizeGB = (long)LanguagePrimitives.ConvertTo(value, typeof(long), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "persistwithencryption")
                {
                    if (!bool.TryParse(value, out flag1))
                    {
                        return;
                    }
                    target.PersistWithEncryption = flag1;
                    return;
                }
                else if (str == "remotenodesessionidletimeoutsec")
                {
                    target.RemoteNodeSessionIdleTimeoutSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    if (configuration == null)
                    {
                        return;
                    }
                    configuration._remoteNodeSessionIdleTimeoutSec = target.RemoteNodeSessionIdleTimeoutSec;
                    return;
                }
                else if (str == "maxactivityprocesses")
                {
                    target.MaxActivityProcesses = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "maxconnectedsessions")
                {
                    target.MaxConnectedSessions = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "maxdisconnectedsessions")
                {
                    target.MaxDisconnectedSessions = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "maxrunningworkflows")
                {
                    target.MaxRunningWorkflows        = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    configuration._maxInProcRunspaces = target.MaxRunningWorkflows * 2;
                    return;
                }
                else if (str == "maxsessionsperremotenode")
                {
                    target.MaxSessionsPerRemoteNode = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "maxsessionsperworkflow")
                {
                    target.MaxSessionsPerWorkflow = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "outofprocessactivity")
                {
                    char[] chrArray1 = new char[1];
                    chrArray1[0] = ',';
                    string[] strArrays2 = value.Split(chrArray1, StringSplitOptions.RemoveEmptyEntries);
                    PSWorkflowExecutionOption pSWorkflowExecutionOption = target;
                    string[] strArrays3 = strArrays2;
                    pSWorkflowExecutionOption.OutOfProcessActivity = strArrays3.Select <string, string>((string activity) => activity.Trim()).ToArray <string>();
                    return;
                }
                else if (str == "sessionthrottlelimit")
                {
                    target.SessionThrottleLimit = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "validationcachelimit")
                {
                    if (configuration == null)
                    {
                        return;
                    }
                    configuration._validationCacheLimit = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "compiledassemblycachelimit")
                {
                    if (configuration == null)
                    {
                        return;
                    }
                    configuration._compiledAssemblyCacheLimit = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "outofprocessactivitycachelimit")
                {
                    if (configuration == null)
                    {
                        return;
                    }
                    configuration._outOfProcessActivityCacheLimit = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "workflowshutdowntimeoutmsec")
                {
                    target.WorkflowShutdownTimeoutMSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                else if (str == "maxinprocrunspaces")
                {
                    if (configuration == null)
                    {
                        return;
                    }
                    configuration._maxInProcRunspaces = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                    return;
                }
                return;
            }
        }
 /// <summary>
 /// Default constructor
 /// </summary>
 public PSWorkflowConfigurationProvider()
 {
     _wfOptions = new PSWorkflowExecutionOption();
     LoadFromDefaults();
 }
        /// <summary>
        /// Using optionName and optionValue updates the current object
        /// </summary>
        /// <param name="optionName"></param>
        /// <param name="value"></param>
        ///  <param name="target"></param>
        ///  <param name="configuration"></param>
        private static void Update(string optionName, string value, PSWorkflowExecutionOption target, PSWorkflowConfigurationProvider configuration)
        {
            switch (optionName.ToLower(CultureInfo.InvariantCulture))
            {
            case TokenWorkflowApplicationPersistUnloadTimeoutSec:
                target.WorkflowApplicationPersistUnloadTimeoutSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenWSManPluginReportCompletionOnZeroActiveSessionsWaitIntervalMSec:
                target.WSManPluginReportCompletionOnZeroActiveSessionsWaitIntervalMSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenActivitiesCacheCleanupIntervalMSec:
                target.ActivitiesCacheCleanupIntervalMSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenActivityProcessIdleTimeoutSec:
                target.ActivityProcessIdleTimeoutSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenAllowedActivity:
                string[] activities = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                target.AllowedActivity = activities.Select(activity => activity.Trim()).ToArray();
                break;

            case TokenEnableValidation:
                bool enableValidation;
                if (bool.TryParse(value, out enableValidation))
                {
                    target.EnableValidation = enableValidation;
                }
                break;

            case TokenPersistencePath:
                target.PersistencePath = Environment.ExpandEnvironmentVariables(value);
                break;

            case TokenMaxPersistenceStoreSizeGB:
                target.MaxPersistenceStoreSizeGB = (long)LanguagePrimitives.ConvertTo(value, typeof(long), CultureInfo.InvariantCulture);
                break;

            case TokenPersistWithEncryption:
                bool persistWithEncryption;
                if (bool.TryParse(value, out persistWithEncryption))
                {
                    target.PersistWithEncryption = persistWithEncryption;
                }
                break;

            case TokenRemoteNodeSessionIdleTimeoutSec:
                target.RemoteNodeSessionIdleTimeoutSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                if (configuration != null)
                {
                    configuration._remoteNodeSessionIdleTimeoutSec = target.RemoteNodeSessionIdleTimeoutSec;
                }
                break;

            case TokenMaxActivityProcesses:
                target.MaxActivityProcesses = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenMaxConnectedSessions:
                target.MaxConnectedSessions = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenMaxDisconnectedSessions:
                target.MaxDisconnectedSessions = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenMaxRunningWorkflows:
                target.MaxRunningWorkflows        = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                configuration._maxInProcRunspaces = target.MaxRunningWorkflows * 2;
                break;

            case TokenMaxSessionsPerRemoteNode:
                target.MaxSessionsPerRemoteNode = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenMaxSessionsPerWorkflow:
                target.MaxSessionsPerWorkflow = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenOutOfProcessActivity:
                string[] outofProcActivities = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                target.OutOfProcessActivity = outofProcActivities.Select(activity => activity.Trim()).ToArray();
                break;

            case TokenSessionThrottleLimit:
            {
                target.SessionThrottleLimit = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
            }
            break;

            case TokenValidationCacheLimit:
                if (configuration != null)
                {
                    configuration._validationCacheLimit = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                }
                break;

            case TokenCompiledAssemblyCacheLimit:
                if (configuration != null)
                {
                    configuration._compiledAssemblyCacheLimit = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                }
                break;

            case TokenOutOfProcessActivityCacheLimit:
                if (configuration != null)
                {
                    configuration._outOfProcessActivityCacheLimit = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                }
                break;

            case TokenWorkflowShutdownTimeoutMSec:
                target.WorkflowShutdownTimeoutMSec = (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                break;

            case TokenMaxInProcRunspaces:
            {
                if (configuration != null)
                {
                    configuration._maxInProcRunspaces =
                        (int)LanguagePrimitives.ConvertTo(value, typeof(int), CultureInfo.InvariantCulture);
                }
            }
            break;
            }
        }