Example #1
0
        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();
            }
        }
Example #2
0
        /// <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>
        /// 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);
            }
        }
Example #4
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;
            }
        }