private FarmSettingStore GetWriteSettingStore()
        {
            rrLock.EnterUpgradeableReadLock();

            try
            {
                GetSettingStore();

                if (_settingStore == null) // setting store not yet created
                {
                    rrLock.EnterWriteLock();

                    try
                    {
                        _settingStore = FarmSettingStore.Create(this.farm);
                    }
                    finally
                    {
                        rrLock.ExitWriteLock();
                    }
                }
                return(_settingStore);
            }
            finally
            {
                rrLock.ExitUpgradeableReadLock();
            }
        }
        public void Remove(string key)
        {
            rrLock.EnterWriteLock();
            try
            {
                //intentionally nested, both cases getting a write lock, is safe and ensures no race.
                //Force the reload of the persisted object, which minimizes chances of a failure due
                // to a concurrency failure.

                Reset();
                FarmSettingStore store = GetWriteSettingStore();


                if (store.Settings.ContainsKey(key))
                {
                    store.Settings.Remove(key);
                    store.Update();
                }
            }
            catch (SPUpdatedConcurrencyException)
            {
                Reset();
                throw;
            }
            finally
            {
                rrLock.ExitWriteLock();
            }
        }
        private FarmSettingStore GetSettingStore()
        {
            rrLock.EnterUpgradeableReadLock();

            try
            {
                //Attempt to reload if the settings store is null and a load hasn't been attempted, or if the last load interval is exceeded.
                //in the case of a refresh, then _settingStore will be null and missingSettings will be false.
                if ((_settingStore == null && missingSettings == false) || (DateTime.Now.Subtract(lastLoad).TotalSeconds) > cacheInterval)
                {
                    rrLock.EnterWriteLock();
                    try
                    {
                        //make sure first another thread didn't already load...before trying to load it.
                        if (_settingStore == null)
                        {
                            _settingStore   = FarmSettingStore.Load(farm);
                            lastLoad        = DateTime.Now;
                            missingSettings = (_settingStore == null);
                        }
                    }
                    finally
                    {
                        rrLock.ExitWriteLock();
                    }
                }

                return(_settingStore);
            }
            finally
            {
                rrLock.ExitUpgradeableReadLock();
            }
        }
        /// <summary>
        /// Gets or sets a value based on the key. If the value is not defined in this PropertyBag, it will look in it's
        /// parent property bag.
        /// </summary>
        /// <value></value>
        /// <returns>The config value defined in the property bag. </returns>
        public string this[string key]
        {
            [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            get
            {
                FarmSettingStore store = GetSettingStore();
                if (store == null)
                {
                    return(null);
                }

                rrLock.EnterReadLock();

                try
                {
                    if (!store.Settings.ContainsKey(key))
                    {
                        return(null);
                    }

                    return(store.Settings[key]);
                }
                finally
                {
                    rrLock.ExitReadLock();
                }
            }
            [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            set
            {
                rrLock.EnterWriteLock();

                try
                {
                    //intentionally nested, both cases getting a write lock, is safe and ensures no race.
                    //Force the reload of the persisted object, which minimizes chances of a failure due
                    // to a concurrency failure.
                    Reset();
                    FarmSettingStore store = GetWriteSettingStore();

                    store.Settings[key] = value;
                    store.Update();
                }
                catch (SPUpdatedConcurrencyException)
                {
                    Reset();
                    throw;
                }
                finally
                {
                    rrLock.ExitWriteLock();
                }
            }
        }
        /// <summary>
        /// Forces the property bag to reload settings on next access.
        /// </summary>
        public void Reset()
        {
            rrLock.EnterWriteLock();

            try
            {
                _settingStore = null;
            }
            finally
            {
                rrLock.ExitWriteLock();
            }
        }
示例#6
0
        /// <summary>
        /// Creates a new FarmSettingStore.  This operation cannot be done from a content web application.
        /// </summary>
        /// <param name="farm">The Farm to create the setting store in</param>
        /// <returns></returns>
        public static FarmSettingStore Create(SPFarm farm)
        {
            lock (createlock)
            {
                //lock to prevent duplicate create attempts on a WFE (still a possible race with other WFE's).
                var settingStore = farm.GetChild <FarmSettingStore>(FarmSettingStore.StoreName);

                if (settingStore == null)  // has not been previously saved
                {
                    settingStore = new FarmSettingStore(FarmSettingStore.StoreName, farm);
                    settingStore.Update();
                }
                return(settingStore);
            }
        }
        public bool Contains(string key)
        {
            FarmSettingStore store = GetSettingStore();

            if (store == null)
            {
                return(false);
            }

            rrLock.EnterReadLock();

            try
            {
                return(store.Settings.ContainsKey(key));
            }
            finally
            {
                rrLock.ExitReadLock();
            }
        }
 /// <summary>
 /// For internal use, clears the cached setting store data
 /// </summary>
 public static void ClearCache()
 {
     _settingStore = null;
 }